Skip to content

Commit 51954bd

Browse files
committed
merge revision(s) 51344: [Backport ruby#11387]
* string.c (rb_str_reverse): reversed string is not a substring, and should not set coderange of the original string. [ruby-dev:49189] [Bug ruby#11387] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_1@51600 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent a61756e commit 51954bd

4 files changed

Lines changed: 21 additions & 15 deletions

File tree

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Mon Aug 17 16:44:01 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
2+
3+
* string.c (rb_str_reverse): reversed string is not a substring,
4+
and should not set coderange of the original string.
5+
[ruby-dev:49189] [Bug #11387]
6+
17
Mon Aug 17 16:24:10 2015 Tanaka Akira <akr@fsij.org>
28

39
* lib/time.rb (strptime): Support %s.%N.

string.c

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4544,35 +4544,37 @@ rb_str_reverse(VALUE str)
45444544
rb_encoding *enc;
45454545
VALUE rev;
45464546
char *s, *e, *p;
4547-
int single = 1;
4547+
int cr;
45484548

45494549
if (RSTRING_LEN(str) <= 1) return rb_str_dup(str);
45504550
enc = STR_ENC_GET(str);
45514551
rev = rb_str_new5(str, 0, RSTRING_LEN(str));
45524552
s = RSTRING_PTR(str); e = RSTRING_END(str);
45534553
p = RSTRING_END(rev);
4554+
cr = ENC_CODERANGE(str);
45544555

45554556
if (RSTRING_LEN(str) > 1) {
45564557
if (single_byte_optimizable(str)) {
45574558
while (s < e) {
45584559
*--p = *s++;
45594560
}
45604561
}
4561-
else if (ENC_CODERANGE(str) == ENC_CODERANGE_VALID) {
4562+
else if (cr == ENC_CODERANGE_VALID) {
45624563
while (s < e) {
45634564
int clen = rb_enc_fast_mbclen(s, e, enc);
45644565

4565-
if (clen > 1 || (*s & 0x80)) single = 0;
45664566
p -= clen;
45674567
memcpy(p, s, clen);
45684568
s += clen;
45694569
}
45704570
}
45714571
else {
4572+
cr = rb_enc_asciicompat(enc) ?
4573+
ENC_CODERANGE_7BIT : ENC_CODERANGE_VALID;
45724574
while (s < e) {
45734575
int clen = rb_enc_mbclen(s, e, enc);
45744576

4575-
if (clen > 1 || (*s & 0x80)) single = 0;
4577+
if (clen > 1 || (*s & 0x80)) cr = ENC_CODERANGE_UNKNOWN;
45764578
p -= clen;
45774579
memcpy(p, s, clen);
45784580
s += clen;
@@ -4581,15 +4583,8 @@ rb_str_reverse(VALUE str)
45814583
}
45824584
STR_SET_LEN(rev, RSTRING_LEN(str));
45834585
OBJ_INFECT(rev, str);
4584-
if (ENC_CODERANGE(str) == ENC_CODERANGE_UNKNOWN) {
4585-
if (single) {
4586-
ENC_CODERANGE_SET(str, ENC_CODERANGE_7BIT);
4587-
}
4588-
else {
4589-
ENC_CODERANGE_SET(str, ENC_CODERANGE_VALID);
4590-
}
4591-
}
4592-
rb_enc_cr_str_copy_for_substr(rev, str);
4586+
str_enc_copy(rev, str);
4587+
ENC_CODERANGE_SET(rev, cr);
45934588

45944589
return rev;
45954590
}

test/ruby/test_m17n.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,12 @@ def test_casecmp
11321132
end
11331133

11341134
def test_reverse
1135-
assert_equal(u("\xf0jihgfedcba"), u("abcdefghij\xf0").reverse)
1135+
bug11387 = '[ruby-dev:49189] [Bug #11387]'
1136+
s1 = u("abcdefghij\xf0")
1137+
s2 = s1.reverse
1138+
assert_not_predicate(s1, :valid_encoding?, bug11387)
1139+
assert_equal(u("\xf0jihgfedcba"), s2)
1140+
assert_not_predicate(s2, :valid_encoding?, bug11387)
11361141
end
11371142

11381143
def test_reverse_bang

version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#define RUBY_VERSION "2.1.7"
22
#define RUBY_RELEASE_DATE "2015-08-17"
3-
#define RUBY_PATCHLEVEL 383
3+
#define RUBY_PATCHLEVEL 384
44

55
#define RUBY_RELEASE_YEAR 2015
66
#define RUBY_RELEASE_MONTH 8

0 commit comments

Comments
 (0)