Commit a4386f0e
a4386f0eb4e0Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified
Description
Merge pull request #1061 from neidiom/fix/str-rindex-position
Fix String#rindex with position argument
Details
- Commit
- a4386f0eb4e0
- Parent
- b91e46e3c64b
- Merge parent
- 34b0cc719aa0
lib/sp_runtime.hmodifiedView file
@@ -1350,6 +1350,11 @@
13501350
matches" loop is O(N) total rather than O(N^2). */13511351
static mrb_int sp_str_index_from(const char*s,const char*sub,mrb_int start){mrb_int cl=sp_str_length(s);if(start<0)start+=cl;if(start<0)start=0;if(start>cl)return -1;size_t boff=sp_utf8_byte_offset(s,start);const char*f=strstr(s+boff,sub);if(!f)return -1;mrb_int n=start;const char*p=s+boff;while(p<f){p+=sp_utf8_advance(p);n++;}return n;}13521352
static mrb_int sp_str_rindex(const char*s,const char*sub){if(!sub)sp_raise_cls("TypeError","no implicit conversion of nil into String");size_t sl=strlen(sub);if(sl==0)return sp_str_length(s);const char*last=NULL;const char*p=s;while((p=strstr(p,sub))){last=p;p++;}if(!last)return -1;mrb_int n=0;const char*q=s;while(q<last){q+=sp_utf8_advance(q);n++;}return n;}1353+
/* `s.rindex(sub, pos)` — find the rightmost occurrence of sub at or1354+
before codepoint index pos. Walks all matches and keeps the last1355+
one whose codepoint index <= pos. */1356+
static mrb_int sp_str_rindex_from(const char*s,const char*sub,mrb_int pos){if(!sub)sp_raise_cls("TypeError","no implicit conversion of nil into String");mrb_int cl=sp_str_length(s);if(pos<0)pos=cl+pos;if(pos<0)return SP_INT_NIL;size_t sl=strlen(sub);if(sl==0){if(pos>=cl)return cl;return pos;}const char*p=s;mrb_int best=-1;while((p=strstr(p,sub))!=NULL){mrb_int cur_n=0;const char*r=s;while(r<p){r+=sp_utf8_advance(r);cur_n++;}if(cur_n>pos)break;best=cur_n;p++;}return best<0?SP_INT_NIL:best;}1357+
13531358
static char sp_char_cache[256][3];13541359
static int sp_char_cache_init = 0;13551360
/* start/len are codepoint indices/counts. */spinel_codegen.rbmodifiedView file
Loading diff…
test/str_rindex_position.rbaddedView file
Loading diff…
test/str_rindex_position.rb.expectedaddedView file
Loading diff…