summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-03-01 07:02:08 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-03-01 07:02:08 +0000
commit6016591e1cc54e48d58662180cc548c03334e1c5 (patch)
treeccca4b39098ccc785c863cb9115e5ef60d6cfe7d /string.c
parentf83651ac30c7c776dee8a6a401c654757cb8d1c2 (diff)
* string.c (rb_str_byteslice): the resulted encoding should keep
original encoding. this also fixes the encoding when the result shares internal string. [ruby-core:35376] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30994 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r--string.c9
1 files changed, 4 insertions, 5 deletions
diff --git a/string.c b/string.c
index 1a34bf148d..87273de5f1 100644
--- a/string.c
+++ b/string.c
@@ -4014,6 +4014,7 @@ str_byte_substr(VALUE str, long beg, long len)
}
else {
str2 = rb_str_new5(str, p, len);
+ rb_enc_cr_str_copy_for_substr(str2, str);
OBJ_INFECT(str2, str);
}
@@ -4030,14 +4031,13 @@ str_byte_aref(VALUE str, VALUE indx)
num_index:
str = str_byte_substr(str, idx, 1);
- if (!NIL_P(str) && RSTRING_LEN(str) == 0) return Qnil;
+ if (NIL_P(str) || RSTRING_LEN(str) == 0) return Qnil;
return str;
default:
/* check if indx is Range */
{
long beg, len = RSTRING_LEN(str);
- VALUE tmp;
switch (rb_range_beg_len(indx, &beg, &len, len, 0)) {
case Qfalse:
@@ -4045,8 +4045,7 @@ str_byte_aref(VALUE str, VALUE indx)
case Qnil:
return Qnil;
default:
- tmp = str_byte_substr(str, beg, len);
- return tmp;
+ return str_byte_substr(str, beg, len);
}
}
idx = NUM2LONG(indx);
@@ -4069,7 +4068,7 @@ str_byte_aref(VALUE str, VALUE indx)
* an offset is negative, it is counted from the end of <i>str</i>. Returns
* <code>nil</code> if the initial offset falls outside the string, the length
* is negative, or the beginning of the range is greater than the end.
- * The encoding of th3 resulted string is always ASCII-8BIT.
+ * The encoding of the resulted string keeps original encoding.
*
* "hello".byteslice(1) #=> "e"
* "hello".byteslice(-1) #=> "o"