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

ruby-changes:33932

From: nobu <ko1@a...>
Date: Mon, 19 May 2014 16:30:12 +0900 (JST)
Subject: [ruby-changes:33932] nobu:r46013 (trunk): string.c: byte offset

nobu	2014-05-19 16:29:51 +0900 (Mon, 19 May 2014)

  New Revision: 46013

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

  Log:
    string.c: byte offset
    
    * string.c (rb_pat_search): advance by byte offset but not by char
      offset.  [ruby-core:62669] [Bug #9849]

  Modified files:
    trunk/ChangeLog
    trunk/string.c
    trunk/test/ruby/test_string.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 46012)
+++ ChangeLog	(revision 46013)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon May 19 16:29:48 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* string.c (rb_pat_search): advance by byte offset but not by char
+	  offset.  [ruby-core:62669] [Bug #9849]
+
 Mon May 19 14:06:18 2014  Shota Fukumori  <her@s...>
 
 	* bin/testrb: Removed. Forgot to remove in r45971.
Index: string.c
===================================================================
--- string.c	(revision 46012)
+++ string.c	(revision 46013)
@@ -2569,8 +2569,10 @@ rb_str_casecmp(VALUE str1, VALUE str2) https://github.com/ruby/ruby/blob/trunk/string.c#L2569
     return INT2FIX(-1);
 }
 
+#define rb_str_index(str, sub, offset) rb_strseq_index(str, sub, offset, 0)
+
 static long
-rb_str_index(VALUE str, VALUE sub, long offset)
+rb_strseq_index(VALUE str, VALUE sub, long offset, int in_byte)
 {
     const char *s, *sptr, *e;
     long pos, len, slen;
@@ -2580,8 +2582,8 @@ rb_str_index(VALUE str, VALUE sub, long https://github.com/ruby/ruby/blob/trunk/string.c#L2582
     enc = rb_enc_check(str, sub);
     if (is_broken_string(sub)) return -1;
 
-    len = single_byte ? RSTRING_LEN(str) : str_strlen(str, enc); /* rb_enc_check */
-    slen = str_strlen(sub, enc); /* rb_enc_check */
+    len = (in_byte || single_byte) ? RSTRING_LEN(str) : str_strlen(str, enc); /* rb_enc_check */
+    slen = in_byte ? RSTRING_LEN(sub) : str_strlen(sub, enc); /* rb_enc_check */
     if (offset < 0) {
 	offset += len;
 	if (offset < 0) return -1;
@@ -2591,7 +2593,7 @@ rb_str_index(VALUE str, VALUE sub, long https://github.com/ruby/ruby/blob/trunk/string.c#L2593
     s = RSTRING_PTR(str);
     e = RSTRING_END(str);
     if (offset) {
-	offset = str_offset(s, e, offset, enc, single_byte);
+	if (!in_byte) offset = str_offset(s, e, offset, enc, single_byte);
 	s += offset;
     }
     if (slen == 0) return offset;
@@ -3873,7 +3875,7 @@ static long https://github.com/ruby/ruby/blob/trunk/string.c#L3875
 rb_pat_search(VALUE pat, VALUE str, long pos, int set_backref_str)
 {
     if (BUILTIN_TYPE(pat) == T_STRING) {
-	pos = rb_str_index(str, pat, pos);
+	pos = rb_strseq_index(str, pat, pos, 1);
 	if (set_backref_str) {
 	    if (pos >= 0) {
 		VALUE match;
Index: test/ruby/test_string.rb
===================================================================
--- test/ruby/test_string.rb	(revision 46012)
+++ test/ruby/test_string.rb	(revision 46013)
@@ -834,6 +834,9 @@ class TestString < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_string.rb#L834
     assert_equal Encoding::UTF_8, a.gsub(/world/, c).encoding
 
     assert_equal S("a\u{e9}apos&lt;"), S("a\u{e9}'&lt;").gsub("'", "apos")
+
+    bug9849 = '[ruby-core:62669] [Bug #9849]'
+    assert_equal S("\u{3042 3042 3042}!foo!"), S("\u{3042 3042 3042}/foo/").gsub("/", "!"), bug9849
   end
 
   def test_gsub!

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

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