# New Ticket Created by Vasily Chekalkin
# Please include the string: [perl #56214]
# in the subject line of all future correspondence about this issue.
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=56214 >
Hello.
Almost trivial implementation of Str.index attached.
S29-str/index.t passing.
--
Bacek.
diff --git a/languages/perl6/src/classes/Str.pir b/languages/perl6/src/classes/Str.pir
index 6195989..828e374 100644
--- a/languages/perl6/src/classes/Str.pir
+++ b/languages/perl6/src/classes/Str.pir
@@ -222,6 +222,32 @@ as the Perl 6 C<Str> class.
.return (retv)
.end
+.sub 'index' :method
+ .param string x
+ .param int start :optional
+ .param int has_start :opt_flag
+ .local pmc retv
+ .local string s
+ .local int pos
+
+ s = self
+ retv = new 'Int'
+
+ $I0 = length x
+ unless $I0 goto check_length
+ pos = index s, x, start
+ goto done
+ check_length:
+ pos = start
+ $I0 = length s
+ if $I0 > pos goto done
+ pos = $I0
+
+ done:
+ retv = pos
+ .return (retv)
+.end
+
=item perl()
Returns a Perl representation of the Str.
@@ -452,6 +478,26 @@ This function is mostly identical to the C library sprintf function.
.return s.'sprintf'(args :flat)
.end
+=item index
+
+ our StrPos multi method index( Str $string: Str $substring, StrPos $pos = StrPos(0) ) is export
+
+index searches for the first occurrence of $substring in $string, starting at $pos.
+
+The value returned is always a StrPos object. If the substring is found, then the StrPos represents the position of the first character of the substring. If the substring is not found, a bare StrPos containing no position is returned. This prototype StrPos evaluates to false because it's really a kind of undef. Do not evaluate as a number, because instead of returning -1 it will return 0 and issue a warning.
+
+=cut
+
+.sub 'index'
+ .param string str
+ .param string subs
+ .param int pos :optional
+ .local pmc s
+ s = new 'Perl6Str'
+ s = str
+ .return s.'index'(subs, pos)
+.end
+
=back
=head2 TODO Functions