1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* string.c (str_nth): need not to raise out-of-range exception.

* test/ruby/test_m17n.rb (TestM17N::test_str_aref_len): removed
  debug print.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14287 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2007-12-18 05:44:30 +00:00
parent 874b367bdc
commit a3a6ba9584
3 changed files with 17 additions and 13 deletions

View file

@ -1,3 +1,10 @@
Tue Dec 18 14:39:05 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
* string.c (str_nth): need not to raise out-of-range exception.
* test/ruby/test_m17n.rb (TestM17N::test_str_aref_len): removed
debug print.
Tue Dec 18 14:05:23 2007 Nobuyoshi Nakada <nobu@ruby-lang.org> Tue Dec 18 14:05:23 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
* enc/depend: get rid of target expanded as empty for nmake. * enc/depend: get rid of target expanded as empty for nmake.

View file

@ -746,26 +746,22 @@ rb_str_s_try_convert(VALUE dummy, VALUE str)
} }
static char* static char*
str_nth(const char *p, const char *e, int nth, rb_encoding *enc, int asc) str_nth(char *p, char *e, int nth, rb_encoding *enc, int asc)
{ {
if (asc) if (asc)
p += nth; p += nth;
else else
p = rb_enc_nth(p, e, nth, enc); p = rb_enc_nth(p, e, nth, enc);
if (!p) { if (!p) return 0;
rb_raise(rb_eArgError, "invalid mbstring sequence"); if (p > e) return e;
} return p;
if (p > e) {
rb_raise(rb_eIndexError, "index out of range");
}
return (char*)p;
} }
static int static int
str_offset(const char *p, const char *e, int nth, rb_encoding *enc, int asc) str_offset(char *p, char *e, int nth, rb_encoding *enc, int asc)
{ {
const char *pp = str_nth(p, e, nth, enc, asc); const char *pp = str_nth(p, e, nth, enc, asc);
if (!pp) return e - p;
return pp - p; return pp - p;
} }
@ -1456,8 +1452,7 @@ rb_str_index(VALUE str, VALUE sub, long offset)
if (len - offset < slen) return -1; if (len - offset < slen) return -1;
s = RSTRING_PTR(str); s = RSTRING_PTR(str);
if (offset) { if (offset) {
s = str_nth(s, RSTRING_END(str), offset, enc, IS_7BIT(str)); offset = str_offset(s, RSTRING_END(str), offset, enc, IS_7BIT(str));
offset = s - RSTRING_PTR(str);
} }
if (slen == 0) return offset; if (slen == 0) return offset;
/* need proceed one character at a time */ /* need proceed one character at a time */
@ -1575,6 +1570,7 @@ rb_str_rindex(VALUE str, VALUE sub, long pos)
t = RSTRING_PTR(sub); t = RSTRING_PTR(sub);
for (;;) { for (;;) {
s = str_nth(sbeg, e, pos, enc, asc); s = str_nth(sbeg, e, pos, enc, asc);
if (!s) return -1;
if (memcmp(s, t, slen) == 0) { if (memcmp(s, t, slen) == 0) {
return pos; return pos;
} }
@ -2139,7 +2135,9 @@ rb_str_splice(VALUE str, long beg, long len, VALUE val)
len = slen - beg; len = slen - beg;
} }
p = str_nth(RSTRING_PTR(str), RSTRING_END(str), beg, enc, asc); p = str_nth(RSTRING_PTR(str), RSTRING_END(str), beg, enc, asc);
if (!p) p = RSTRING_END(str);
e = str_nth(p, RSTRING_END(str), len, enc, asc); e = str_nth(p, RSTRING_END(str), len, enc, asc);
if (!e) e = RSTRING_END(str);
/* error check */ /* error check */
beg = p - RSTRING_PTR(str); /* physical position */ beg = p - RSTRING_PTR(str); /* physical position */
len = e - p; /* physical length */ len = e - p; /* physical length */

View file

@ -695,7 +695,6 @@ class TestM17N < Test::Unit::TestCase
STRINGS.each {|s| STRINGS.each {|s|
t = '' t = ''
0.step(s.length-1, 2) {|i| 0.step(s.length-1, 2) {|i|
p [s,s.encoding,i]
t << s[i,2] t << s[i,2]
} }
assert_equal(t, s) assert_equal(t, s)