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

ruby-changes:23673

From: naruse <ko1@a...>
Date: Sun, 20 May 2012 22:32:31 +0900 (JST)
Subject: [ruby-changes:23673] naruse:r35724 (trunk): * enc/shift_jis.c (code_to_mbclen): return

naruse	2012-05-20 22:32:16 +0900 (Sun, 20 May 2012)

  New Revision: 35724

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

  Log:
    * enc/shift_jis.c (code_to_mbclen): return
      ONIGERR_INVALID_CODE_POINT_VALUE if the code is invalid.
    
    * enc/shift_jis.c (tr_next): increment character until the code
      is a valid character. [ruby-dev:45652] [Bug #6450]

  Modified files:
    trunk/ChangeLog
    trunk/enc/shift_jis.c
    trunk/string.c
    trunk/test/ruby/test_m17n_comb.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 35723)
+++ ChangeLog	(revision 35724)
@@ -1,3 +1,11 @@
+Sun May 20 21:36:39 2012  NARUSE, Yui  <naruse@r...>
+
+	* enc/shift_jis.c (code_to_mbclen): return
+	  ONIGERR_INVALID_CODE_POINT_VALUE if the code is invalid.
+
+	* enc/shift_jis.c (tr_next): increment character until the code
+	  is a valid character. [ruby-dev:45652] [Bug #6450]
+
 Sun May 20 12:25:10 2012  Nobuyoshi Nakada  <nobu@r...>
 
 	* Makefile.in (LIBRUBY_SO): link EXTSOLIBS too.
Index: enc/shift_jis.c
===================================================================
--- enc/shift_jis.c	(revision 35723)
+++ enc/shift_jis.c	(revision 35724)
@@ -230,6 +230,9 @@
       return ONIGERR_INVALID_CODE_POINT_VALUE;
   }
   else if (code <= 0xffff) {
+    int low = code & 0xff;
+    if (low < 0x40 || low == 0x7f || 0xfc < low)
+      return ONIGERR_INVALID_CODE_POINT_VALUE;
     return 2;
   }
   else
Index: string.c
===================================================================
--- string.c	(revision 35723)
+++ string.c	(revision 35724)
@@ -4967,6 +4967,7 @@
 
     for (;;) {
 	if (!t->gen) {
+nextpart:
 	    if (t->p == t->pend) return -1;
 	    if (rb_enc_ascget(t->p, t->pend, &n, enc) == '\\' && t->p + n < t->pend) {
 		t->p += n;
@@ -4995,12 +4996,20 @@
 	    }
 	    return t->now;
 	}
-	else if (++t->now < t->max) {
-	    return t->now;
-	}
 	else {
-	    t->gen = 0;
-	    return t->max;
+	    while (ONIGENC_CODE_TO_MBCLEN(enc, ++t->now) <= 0) {
+		if (t->now == t->max) {
+		    t->gen = 0;
+		    goto nextpart;
+		}
+	    }
+	    if (t->now < t->max) {
+		return t->now;
+	    }
+	    else {
+		t->gen = 0;
+		return t->max;
+	    }
 	}
     }
 }
Index: test/ruby/test_m17n_comb.rb
===================================================================
--- test/ruby/test_m17n_comb.rb	(revision 35723)
+++ test/ruby/test_m17n_comb.rb	(revision 35724)
@@ -1236,6 +1236,20 @@
     }
   end
 
+  def test_tr_sjis
+    expected = "\x83}\x83~\x83\x80\x83\x81\x83\x82".force_encoding(Encoding::SJIS)
+    source = "\xCF\xD0\xD1\xD2\xD3".force_encoding(Encoding::SJIS)
+    from = "\xCF-\xD3".force_encoding(Encoding::SJIS)
+    to = "\x83}-\x83\x82".force_encoding(Encoding::SJIS)
+    assert_equal(expected, source.tr(from, to))
+
+    expected = "\x84}\x84~\x84\x80\x84\x81\x84\x82".force_encoding(Encoding::SJIS)
+    source = "\x84M\x84N\x84O\x84P\x84Q".force_encoding(Encoding::SJIS)
+    from = "\x84@-\x84`".force_encoding(Encoding::SJIS)
+    to = "\x84p-\x84\x91".force_encoding(Encoding::SJIS)
+    assert_equal(expected, source.tr(from, to))
+  end
+
   def test_tr_s
     combination(STRINGS, STRINGS, STRINGS) {|s1, s2, s3|
       desc = "#{encdump s1}.tr_s(#{encdump s2}, #{encdump s3})"

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

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