summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2000-03-23 08:37:35 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2000-03-23 08:37:35 +0000
commit688169fd83b24564b653c03977c168cea50ccd35 (patch)
treeb4724e5397cf5da5b554ab5795842a93145a88be /string.c
parent5c13dd59db1ee6c04cdac4ce2ee97d5934115439 (diff)
2000-03-23
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@648 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r--string.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/string.c b/string.c
index 7759b5e004..7184080b1b 100644
--- a/string.c
+++ b/string.c
@@ -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;