aboutsummaryrefslogtreecommitdiffstats
path: root/string.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-07-22 14:44:20 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-07-22 14:44:20 +0000
commit61d807ccfae5500d1471f1074a6671ac9f191c88 (patch)
treeb75fea84c7de9f49cdcbeace800fce81421b5f32 /string.c
parent7389ac15be042f242135f464530b66ca4419b933 (diff)
downloadruby-61d807ccfae5500d1471f1074a6671ac9f191c88.tar.gz
string.c: fix coderange of reverse
* string.c (rb_str_reverse): reversed string is not a substring, and should not set coderange of the original string. [ruby-dev:49189] [Bug #11387] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51344 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r--string.c21
1 files changed, 8 insertions, 13 deletions
diff --git a/string.c b/string.c
index 56960574ec..110d11199e 100644
--- a/string.c
+++ b/string.c
@@ -4802,13 +4802,14 @@ rb_str_reverse(VALUE str)
rb_encoding *enc;
VALUE rev;
char *s, *e, *p;
- int single = 1;
+ int cr;
if (RSTRING_LEN(str) <= 1) return rb_str_dup(str);
enc = STR_ENC_GET(str);
rev = rb_str_new_with_class(str, 0, RSTRING_LEN(str));
s = RSTRING_PTR(str); e = RSTRING_END(str);
p = RSTRING_END(rev);
+ cr = ENC_CODERANGE(str);
if (RSTRING_LEN(str) > 1) {
if (single_byte_optimizable(str)) {
@@ -4816,21 +4817,22 @@ rb_str_reverse(VALUE str)
*--p = *s++;
}
}
- else if (ENC_CODERANGE(str) == ENC_CODERANGE_VALID) {
+ else if (cr == ENC_CODERANGE_VALID) {
while (s < e) {
int clen = rb_enc_fast_mbclen(s, e, enc);
- if (clen > 1 || (*s & 0x80)) single = 0;
p -= clen;
memcpy(p, s, clen);
s += clen;
}
}
else {
+ cr = rb_enc_asciicompat(enc) ?
+ ENC_CODERANGE_7BIT : ENC_CODERANGE_VALID;
while (s < e) {
int clen = rb_enc_mbclen(s, e, enc);
- if (clen > 1 || (*s & 0x80)) single = 0;
+ if (clen > 1 || (*s & 0x80)) cr = ENC_CODERANGE_UNKNOWN;
p -= clen;
memcpy(p, s, clen);
s += clen;
@@ -4839,15 +4841,8 @@ rb_str_reverse(VALUE str)
}
STR_SET_LEN(rev, RSTRING_LEN(str));
OBJ_INFECT(rev, str);
- if (ENC_CODERANGE(str) == ENC_CODERANGE_UNKNOWN) {
- if (single) {
- ENC_CODERANGE_SET(str, ENC_CODERANGE_7BIT);
- }
- else {
- ENC_CODERANGE_SET(str, ENC_CODERANGE_VALID);
- }
- }
- rb_enc_cr_str_copy_for_substr(rev, str);
+ str_enc_copy(rev, str);
+ ENC_CODERANGE_SET(rev, cr);
return rev;
}