On Wed, Mar 16, 2005 at 06:44:47PM +0100, Thomas Sandlaß wrote:
: Markus Laire wrote:
: >What should index("Hello", "", 999) return in perl6?
: 
: Since the first thing that needs definition is how does
: the empty string match, it could be e.g. any(0.."Hello".elems).

If the position of the first attempt is specified, the null string
should match right there.  It's an infinite loop if they don't add 1
next time (implying our position type needs to be able to add and
subtract in whatever the current units are).

One nit, .elems is not defined on strings.  .chars works under whatever
the current Unicode regime is (default graphemes).

: As described in A12 string positions are much more these days
: than simple ints. There is a class StrPosition or Pos[Str]
: or Pos of Str or some such. So one signature could be:
: 
: multi sub index( Str       $searched,
:                  Str       $substr,
:                  Pos[Str] ?$pos where { 0 <= $_ < $searched.elems }
:                )
:   returns  Undef[Pos[Str]]
:          ^ Pos[Str] where { 0 <= $_ < $searched.elems }
: {
:   ...
: }
: 
: 
: 
: >In perl5 that returns 5, but IMHO -1 would be right result.
: 
: This is because of the usage of "". Otherwise it's -1.

Well, that, and Perl 5 is being "kind" in rounding 999 down to 5 on
the assumption that the programmer was specifically using 999 to mean
"end of string".  It's kind of stupid to assume that for index, but
rindex is another matter.

Larry

Reply via email to