Skip to content

Commit def5eab

Browse files
committed
merge revision(s) r46896,r46897,r46898: [Backport ruby#10078]
* string.c (rb_str_count): fix wrong single-byte optimization. 7bit ascii can be a trailing byte in Shift_JIS. [ruby-dev:48442] [Bug ruby#10078] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_1@47255 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent f3274f5 commit def5eab

File tree

5 files changed

+43
-15
lines changed

5 files changed

+43
-15
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Sat Aug 23 02:22:02 2014 Nobuyoshi Nakada <[email protected]>
2+
3+
* string.c (rb_str_count): fix wrong single-byte optimization.
4+
7bit ascii can be a trailing byte in Shift_JIS.
5+
[ruby-dev:48442] [Bug #10078]
6+
17
Thu Aug 21 01:44:46 2014 Tanaka Akira <[email protected]>
28

39
* gc.c (mark_current_machine_context): Call SET_STACK_END.

string.c

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6059,21 +6059,25 @@ rb_str_count(int argc, VALUE *argv, VALUE str)
60596059
{
60606060
char table[TR_TABLE_SIZE];
60616061
rb_encoding *enc = 0;
6062-
VALUE del = 0, nodel = 0;
6062+
VALUE del = 0, nodel = 0, tstr;
60636063
char *s, *send;
60646064
int i;
60656065
int ascompat;
60666066

60676067
rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
6068-
for (i=0; i<argc; i++) {
6069-
VALUE tstr = argv[i];
6070-
unsigned char c;
60716068

6072-
StringValue(tstr);
6073-
enc = rb_enc_check(str, tstr);
6074-
if (argc == 1 && RSTRING_LEN(tstr) == 1 && rb_enc_asciicompat(enc) &&
6075-
(c = RSTRING_PTR(tstr)[0]) < 0x80 && !is_broken_string(str)) {
6069+
tstr = argv[0];
6070+
StringValue(tstr);
6071+
enc = rb_enc_check(str, tstr);
6072+
if (argc == 1) {
6073+
const char *ptstr;
6074+
if (RSTRING_LEN(tstr) == 1 && rb_enc_asciicompat(enc) &&
6075+
(ptstr = RSTRING_PTR(tstr),
6076+
ONIGENC_IS_ALLOWED_REVERSE_MATCH(enc, (const unsigned char *)ptstr, (const unsigned char *)ptstr+1)) &&
6077+
!is_broken_string(str)) {
60766078
int n = 0;
6079+
int clen;
6080+
unsigned char c = rb_enc_codepoint_len(ptstr, ptstr+1, &clen, enc);
60776081

60786082
s = RSTRING_PTR(str);
60796083
if (!s || RSTRING_LEN(str) == 0) return INT2FIX(0);
@@ -6083,7 +6087,14 @@ rb_str_count(int argc, VALUE *argv, VALUE str)
60836087
}
60846088
return INT2NUM(n);
60856089
}
6086-
tr_setup_table(tstr, table, i==0, &del, &nodel, enc);
6090+
}
6091+
6092+
tr_setup_table(tstr, table, TRUE, &del, &nodel, enc);
6093+
for (i=1; i<argc; i++) {
6094+
tstr = argv[i];
6095+
StringValue(tstr);
6096+
enc = rb_enc_check(str, tstr);
6097+
tr_setup_table(tstr, table, FALSE, &del, &nodel, enc);
60876098
}
60886099

60896100
s = RSTRING_PTR(str);

test/ruby/test_m17n.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,11 @@ def test_count
10371037
assert_raise(Encoding::CompatibilityError){s.count(a("\xa3\xb0"))}
10381038
end
10391039

1040+
def test_count_sjis_trailing_byte
1041+
bug10078 = '[ruby-dev:48442] [Bug #10078]'
1042+
assert_equal(0, s("\x98\x61").count("a"), bug10078)
1043+
end
1044+
10401045
def test_delete
10411046
assert_equal(1, e("\xa1\xa2").delete("z").length)
10421047
s = e("\xa3\xb0\xa3\xb1\xa3\xb2\xa3\xb3\xa3\xb4")

test/ruby/test_m17n_comb.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def encdumpargs(args)
8787
r
8888
end
8989

90-
def assert_enccall(recv, meth, *args, &block)
90+
def encdumpcall(recv, meth, *args, &block)
9191
desc = ''
9292
if String === recv
9393
desc << encdump(recv)
@@ -110,6 +110,11 @@ def assert_enccall(recv, meth, *args, &block)
110110
if block
111111
desc << ' {}'
112112
end
113+
desc
114+
end
115+
116+
def assert_enccall(recv, meth, *args, &block)
117+
desc = encdumpcall(recv, meth, *args, &block)
113118
result = nil
114119
assert_nothing_raised(desc) {
115120
result = recv.send(meth, *args, &block)
@@ -709,12 +714,13 @@ def test_str_dup
709714

710715
def test_str_count
711716
combination(STRINGS, STRINGS) {|s1, s2|
717+
desc = proc {encdumpcall(s1, :count, s2)}
712718
if !s1.valid_encoding? || !s2.valid_encoding?
713-
assert_raise(ArgumentError, Encoding::CompatibilityError) { s1.count(s2) }
719+
assert_raise(ArgumentError, Encoding::CompatibilityError, desc) { s1.count(s2) }
714720
next
715721
end
716722
if !s1.ascii_only? && !s2.ascii_only? && s1.encoding != s2.encoding
717-
assert_raise(Encoding::CompatibilityError) { s1.count(s2) }
723+
assert_raise(Encoding::CompatibilityError, desc) { s1.count(s2) }
718724
next
719725
end
720726
n = enccall(s1, :count, s2)

version.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#define RUBY_VERSION "2.1.2"
2-
#define RUBY_RELEASE_DATE "2014-08-21"
3-
#define RUBY_PATCHLEVEL 205
2+
#define RUBY_RELEASE_DATE "2014-08-23"
3+
#define RUBY_PATCHLEVEL 206
44

55
#define RUBY_RELEASE_YEAR 2014
66
#define RUBY_RELEASE_MONTH 8
7-
#define RUBY_RELEASE_DAY 21
7+
#define RUBY_RELEASE_DAY 23
88

99
#include "ruby/version.h"
1010

0 commit comments

Comments
 (0)