Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of Str.index. #141

Closed
p6rt opened this issue Jun 22, 2008 · 9 comments
Closed

Implementation of Str.index. #141

p6rt opened this issue Jun 22, 2008 · 9 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Jun 22, 2008

Migrated from rt.perl.org#56214 (status was 'resolved')

Searchable as RT56214$

@p6rt
Copy link
Author

p6rt commented Jun 22, 2008

From @bacek

Hello.

Almost trivial implementation of Str.index attached.

S29-str/index.t passing.

--
Bacek.

@p6rt
Copy link
Author

p6rt commented Jun 22, 2008

From @bacek

index.diff
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

@p6rt
Copy link
Author

p6rt commented Jul 1, 2008

From @bacek

Revorked patch after Pmichaud's review.

@p6rt
Copy link
Author

p6rt commented Jul 1, 2008

From @bacek

index2.diff
diff --git a/languages/perl6/src/classes/Str.pir b/languages/perl6/src/classes/Str.pir
index 275b21a..4973672 100644
--- a/languages/perl6/src/classes/Str.pir
+++ b/languages/perl6/src/classes/Str.pir
@@ -27,7 +27,7 @@ as the Perl 6 C<Str> class.
     p6meta.'register'('String', 'parent'=>strproto, 'protoobject'=>strproto)
 
     $P0 = get_hll_namespace ['Str']
-    '!EXPORT'('sprintf', 'from'=>$P0)
+    '!EXPORT'('sprintf index', 'from'=>$P0)
 .end
 
 
@@ -216,6 +216,59 @@ as the Perl 6 C<Str> class.
     .return(retv)
 .end
 
+=item index
+
+ our StrPos multi method index( Str $string: Str $substring, StrPos $pos = StrPos(0) ) is export
+
+C<index> searches for the first occurrence of C<$substring> in C<$string>,
+starting at C<$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' :method
+    .param string substring
+    .param int pos     :optional
+    .param int has_pos :opt_flag
+    .local pmc retv
+    .local string s
+    
+    s = self
+
+    # This check is redundant (at least with current parrot).
+    # 'int pos' initialised to 0 if it was omited in invokation.
+    if has_pos goto check_substring
+    pos = 0
+
+  check_substring:
+    unless substring goto check_length
+    pos = index s, substring, pos
+    goto check_res
+  check_length:
+    $I0 = length s
+    if $I0 > pos goto done
+    pos = $I0
+
+  check_res:
+    # According to spec we should return StrPos wich yield false in boolean context.
+    # parrot return's -1 from index
+    if pos >= 0 goto done
+
+    # It should be bare StrPos.
+    retv = new 'Failure'
+    .return (retv)
+
+  done:
+    retv = new 'Int'
+    retv  = pos
+    .return (retv)
+.end
 
 =item perl()
 

@p6rt
Copy link
Author

p6rt commented Jul 1, 2008

@bacek - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Jul 1, 2008

From @bacek

Revorked patch after Pmichaud's review.

@p6rt
Copy link
Author

p6rt commented Jul 1, 2008

From @bacek

index2.diff
diff --git a/languages/perl6/src/classes/Str.pir b/languages/perl6/src/classes/Str.pir
index 275b21a..4973672 100644
--- a/languages/perl6/src/classes/Str.pir
+++ b/languages/perl6/src/classes/Str.pir
@@ -27,7 +27,7 @@ as the Perl 6 C<Str> class.
     p6meta.'register'('String', 'parent'=>strproto, 'protoobject'=>strproto)
 
     $P0 = get_hll_namespace ['Str']
-    '!EXPORT'('sprintf', 'from'=>$P0)
+    '!EXPORT'('sprintf index', 'from'=>$P0)
 .end
 
 
@@ -216,6 +216,59 @@ as the Perl 6 C<Str> class.
     .return(retv)
 .end
 
+=item index
+
+ our StrPos multi method index( Str $string: Str $substring, StrPos $pos = StrPos(0) ) is export
+
+C<index> searches for the first occurrence of C<$substring> in C<$string>,
+starting at C<$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' :method
+    .param string substring
+    .param int pos     :optional
+    .param int has_pos :opt_flag
+    .local pmc retv
+    .local string s
+    
+    s = self
+
+    # This check is redundant (at least with current parrot).
+    # 'int pos' initialised to 0 if it was omited in invokation.
+    if has_pos goto check_substring
+    pos = 0
+
+  check_substring:
+    unless substring goto check_length
+    pos = index s, substring, pos
+    goto check_res
+  check_length:
+    $I0 = length s
+    if $I0 > pos goto done
+    pos = $I0
+
+  check_res:
+    # According to spec we should return StrPos wich yield false in boolean context.
+    # parrot return's -1 from index
+    if pos >= 0 goto done
+
+    # It should be bare StrPos.
+    retv = new 'Failure'
+    .return (retv)
+
+  done:
+    retv = new 'Int'
+    retv  = pos
+    .return (retv)
+.end
 
 =item perl()
 

@p6rt
Copy link
Author

p6rt commented Jul 3, 2008

From @moritz

Patch applied as r29006, thank you very much for your contribution.

Is is not perfect wrt to StrPos as the return value, but I think that's
worth working on when we have different Unicode levels.

@p6rt
Copy link
Author

p6rt commented Jul 3, 2008

@moritz - Status changed from 'open' to 'resolved'

@p6rt p6rt closed this as completed Jul 3, 2008
@p6rt p6rt added the patch label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant