diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2000-03-23 08:37:35 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2000-03-23 08:37:35 +0000 |
commit | e68f544351359c62da09401ae2382ce33aabf741 (patch) | |
tree | b4724e5397cf5da5b554ab5795842a93145a88be /string.c | |
parent | b54db8568cae6f5be8b98977a82d11df2d5c8c73 (diff) | |
download | ruby-e68f544351359c62da09401ae2382ce33aabf741.tar.gz ruby-e68f544351359c62da09401ae2382ce33aabf741.tar.xz ruby-e68f544351359c62da09401ae2382ce33aabf741.zip |
2000-03-23
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@648 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r-- | string.c | 13 |
1 files changed, 11 insertions, 2 deletions
@@ -886,12 +886,16 @@ rb_str_replace(str, beg, len, val) long beg; long len; { + if (RSTRING(str)->len < beg + len) { + len = RSTRING(str)->len - beg; + } + if (len < RSTRING(val)->len) { /* expand string */ REALLOC_N(RSTRING(str)->ptr, char, RSTRING(str)->len+RSTRING(val)->len-len+1); } - if (len != RSTRING(val)->len) { + if (RSTRING(val)->len != len) { memmove(RSTRING(str)->ptr + beg + RSTRING(val)->len, RSTRING(str)->ptr + beg + len, RSTRING(str)->len - (beg + len)); @@ -899,7 +903,9 @@ rb_str_replace(str, beg, len, val) if (RSTRING(str)->len < beg && len < 0) { MEMZERO(RSTRING(str)->ptr + RSTRING(str)->len, char, -len); } - memmove(RSTRING(str)->ptr+beg, RSTRING(val)->ptr, RSTRING(val)->len); + if (RSTRING(val)->len > 0) { + memmove(RSTRING(str)->ptr+beg, RSTRING(val)->ptr, RSTRING(val)->len); + } RSTRING(str)->len += RSTRING(val)->len - len; RSTRING(str)->ptr[RSTRING(str)->len] = '\0'; } @@ -1017,6 +1023,9 @@ rb_str_slice_bang(argc, argv, str) if (pos < 0) { pos = RSTRING(str)->len + pos; } + if (pos < 0 || RSTRING(str)->len <= pos) { + rb_raise(rb_eIndexError, "index %d out of string", pos); + } arg2 = rb_str_substr(str, pos, len); rb_str_replace(str, pos, len, rb_str_new(0,0)); return arg2; |