[前][次][番号順一覧][スレッド一覧]

ruby-changes:34813

From: nobu <ko1@a...>
Date: Tue, 22 Jul 2014 12:56:38 +0900 (JST)
Subject: [ruby-changes:34813] nobu:r46896 (trunk): string.c: fix wrong single-byte optimization

nobu	2014-07-22 12:56:26 +0900 (Tue, 22 Jul 2014)

  New Revision: 46896

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=46896

  Log:
    string.c: fix wrong single-byte optimization
    
    * string.c (rb_str_count): fix wrong single-byte optimization.
      7bit ascii can be a trailing byte in Shift_JIS.
      [ruby-dev:48442] [Bug #10078]

  Modified files:
    trunk/ChangeLog
    trunk/string.c
    trunk/test/ruby/test_m17n.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 46895)
+++ ChangeLog	(revision 46896)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Jul 22 12:56:24 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* string.c (rb_str_count): fix wrong single-byte optimization.
+	  7bit ascii can be a trailing byte in Shift_JIS.
+	  [ruby-dev:48442] [Bug #10078]
+
 Tue Jul 22 01:48:38 2014  Eric Wong  <e@8...>
 
 	* include/ruby/io.h (rb_io_buffer_t): fix packing on gcc
Index: string.c
===================================================================
--- string.c	(revision 46895)
+++ string.c	(revision 46896)
@@ -6075,13 +6075,15 @@ rb_str_count(int argc, VALUE *argv, VALU https://github.com/ruby/ruby/blob/trunk/string.c#L6075
     rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
     for (i=0; i<argc; i++) {
 	VALUE tstr = argv[i];
-	unsigned char c;
+	const unsigned char *utstr;
 
 	StringValue(tstr);
 	enc = rb_enc_check(str, tstr);
 	if (argc == 1 && RSTRING_LEN(tstr) == 1 && rb_enc_asciicompat(enc) &&
-	    (c = RSTRING_PTR(tstr)[0]) < 0x80 && !is_broken_string(str)) {
+	    (utstr = (const OnigUChar *)RSTRING_PTR(tstr), ONIGENC_IS_ALLOWED_REVERSE_MATCH(enc, utstr, utstr+1)) &&
+	    !is_broken_string(str)) {
 	    int n = 0;
+	    unsigned char c = utstr[0];
 
 	    s = RSTRING_PTR(str);
 	    if (!s || RSTRING_LEN(str) == 0) return INT2FIX(0);
Index: test/ruby/test_m17n.rb
===================================================================
--- test/ruby/test_m17n.rb	(revision 46895)
+++ test/ruby/test_m17n.rb	(revision 46896)
@@ -1037,6 +1037,11 @@ class TestM17N < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_m17n.rb#L1037
     assert_raise(Encoding::CompatibilityError){s.count(a("\xa3\xb0"))}
   end
 
+  def test_count_sjis_trailing_byte
+    bug10078 = '[ruby-dev:48442] [Bug #10078]'
+    assert_equal(0, s("\x98\x61").count("a"), bug10078)
+  end
+
   def test_delete
     assert_equal(1, e("\xa1\xa2").delete("z").length)
     s = e("\xa3\xb0\xa3\xb1\xa3\xb2\xa3\xb3\xa3\xb4")

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]