summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
Diffstat (limited to 'string.c')
-rw-r--r--string.c13
1 files changed, 9 insertions, 4 deletions
diff --git a/string.c b/string.c
index 1b6c7e4a96..bce4ea78c7 100644
--- a/string.c
+++ b/string.c
@@ -541,9 +541,10 @@ rb_str_substr(VALUE str, long beg, long len)
if (len < 0) {
len = 0;
}
- if (len == 0) return rb_str_new5(str,0,0);
-
- if (len > sizeof(struct RString)/2 &&
+ if (len == 0) {
+ str2 = rb_str_new5(str,0,0);
+ }
+ else if (len > sizeof(struct RString)/2 &&
beg + len == RSTRING(str)->len && !FL_TEST(str, STR_ASSOC)) {
str2 = rb_str_new3(rb_str_new4(str));
RSTRING(str2)->ptr += RSTRING(str2)->len - len;
@@ -1433,13 +1434,17 @@ rb_str_aref(VALUE str, VALUE indx)
/* check if indx is Range */
{
long beg, len;
+ VALUE tmp;
+
switch (rb_range_beg_len(indx, &beg, &len, RSTRING(str)->len, 0)) {
case Qfalse:
break;
case Qnil:
return Qnil;
default:
- return rb_str_substr(str, beg, len);
+ tmp = rb_str_substr(str, beg, len);
+ OBJ_INFECT(tmp, indx);
+ return tmp;
}
}
idx = NUM2LONG(indx);