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.rindex. #277

Closed
p6rt opened this issue Aug 24, 2008 · 6 comments
Closed

Implementation of Str.rindex. #277

p6rt opened this issue Aug 24, 2008 · 6 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Aug 24, 2008

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

Searchable as RT58310$

@p6rt
Copy link
Author

p6rt commented Aug 24, 2008

From @bacek

Hello.

Trivial implementation of Str.rindex (depends on #​58308)

--
Bacek

@p6rt
Copy link
Author

p6rt commented Aug 24, 2008

From @bacek

rakudo_str_rindex.diff
commit bbd7144bee1648f4516d61a98b48177b1370fe78
Author: Vasily Chekalkin <bacek@icebolt.(none)>
Date:   Sun Aug 24 20:51:41 2008 +1000

    Implement rindex

diff --git a/languages/perl6/src/builtins/any-str.pir b/languages/perl6/src/builtins/any-str.pir
index 77bb24c..84a62c6 100644
--- a/languages/perl6/src/builtins/any-str.pir
+++ b/languages/perl6/src/builtins/any-str.pir
@@ -21,7 +21,7 @@ the size of that file down and to emphasize their generic,
 .namespace []
 .sub 'onload' :anon :init :load
     $P0 = get_hll_namespace ['Any']
-    '!EXPORT'('chars index substr', 'from'=>$P0)
+    '!EXPORT'('chars index rindex substr', 'from'=>$P0)
 .end
 
 
@@ -75,6 +75,44 @@ the size of that file down and to emphasize their generic,
     .return ($P0)
 .end
 
+=item rindex()
+
+=cut
+
+.namespace ['Any']
+.sub 'rindex' :method :multi(_)
+    .param string substring
+    .param int pos             :optional
+    .param int has_pos         :opt_flag
+    .local pmc retv
+
+    if has_pos goto have_pos
+    pos = 0
+  have_pos:
+
+    .local string s
+    s = self
+
+  check_substring:
+    if substring goto substring_search
+    $I0 = length s
+    if pos < $I0 goto done
+    pos = $I0
+    goto done
+
+  substring_search:
+    pos = rindex s, substring, pos
+    if pos < 0 goto fail
+
+  done:
+    $P0 = new 'Int'
+    $P0 = pos
+    .return ($P0)
+
+  fail:
+    $P0 = new 'Failure'
+    .return ($P0)
+.end
 =item substr()
 
 =cut

@p6rt
Copy link
Author

p6rt commented Aug 24, 2008

From @bacek

Fixed version of rindex. Now all non-fudged tests from S29-str/rindex.t
passes.

--
Bacek

@p6rt
Copy link
Author

p6rt commented Aug 24, 2008

From @bacek

rakudo_str_rindex2.diff
diff --git a/languages/perl6/src/builtins/any-str.pir b/languages/perl6/src/builtins/any-str.pir
index 77bb24c..317656b 100644
--- a/languages/perl6/src/builtins/any-str.pir
+++ b/languages/perl6/src/builtins/any-str.pir
@@ -21,7 +21,7 @@ the size of that file down and to emphasize their generic,
 .namespace []
 .sub 'onload' :anon :init :load
     $P0 = get_hll_namespace ['Any']
-    '!EXPORT'('chars index substr', 'from'=>$P0)
+    '!EXPORT'('chars index rindex substr', 'from'=>$P0)
 .end
 
 
@@ -75,6 +75,48 @@ the size of that file down and to emphasize their generic,
     .return ($P0)
 .end
 
+=item rindex()
+
+=cut
+
+.namespace ['Any']
+.sub 'rindex' :method :multi(_)
+    .param string substring
+    .param int pos             :optional
+    .param int has_pos         :opt_flag
+    .local pmc retv
+
+    .local string s
+    s = self
+
+  check_substring:
+    if substring goto substring_search
+
+    # we do not have substring return pos or length
+    if has_pos goto have_pos
+    pos = length s
+    goto done
+  have_pos:
+    $I0 = length s
+    if pos < $I0 goto done
+    pos = $I0
+    goto done
+
+
+
+  substring_search:
+    pos = rindex s, substring, pos
+    if pos < 0 goto fail
+
+  done:
+    $P0 = new 'Int'
+    $P0 = pos
+    .return ($P0)
+
+  fail:
+    $P0 = new 'Failure'
+    .return ($P0)
+.end
 =item substr()
 
 =cut

@p6rt
Copy link
Author

p6rt commented Aug 24, 2008

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

@p6rt
Copy link
Author

p6rt commented Sep 9, 2008

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

@p6rt p6rt closed this as completed Sep 9, 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