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

ruby-changes:33568

From: naruse <ko1@a...>
Date: Mon, 21 Apr 2014 19:14:52 +0900 (JST)
Subject: [ruby-changes:33568] naruse:r45649 (trunk): * ext/-test-/string/coderange.c: add Bug::String.new#coderange_scan

naruse	2014-04-21 19:14:46 +0900 (Mon, 21 Apr 2014)

  New Revision: 45649

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

  Log:
    * ext/-test-/string/coderange.c: add Bug::String.new#coderange_scan
      to explicitly scan coderange.

  Added files:
    trunk/test/-ext-/string/test_coderange.rb
  Modified files:
    trunk/ChangeLog
    trunk/ext/-test-/string/coderange.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 45648)
+++ ChangeLog	(revision 45649)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Mon Apr 21 18:55:21 2014  NARUSE, Yui  <naruse@r...>
+
+	* ext/-test-/string/coderange.c: add Bug::String.new#coderange_scan
+	  to explicitly scan coderange.
+
 Mon Apr 21 18:19:35 2014  NARUSE, Yui  <naruse@r...>
 
 	* string.c (coderange_scan): remove unused logic.
Index: ext/-test-/string/coderange.c
===================================================================
--- ext/-test-/string/coderange.c	(revision 45648)
+++ ext/-test-/string/coderange.c	(revision 45649)
@@ -2,10 +2,11 @@ https://github.com/ruby/ruby/blob/trunk/ext/-test-/string/coderange.c#L2
 #include "ruby/encoding.h"
 
 static VALUE sym_7bit, sym_valid, sym_unknown, sym_broken;
+
 static VALUE
-str_coderange(VALUE str)
+coderange_int2sym(int coderange)
 {
-    switch (ENC_CODERANGE(str)) {
+    switch (coderange) {
       case ENC_CODERANGE_7BIT:
 	return sym_7bit;
       case ENC_CODERANGE_VALID:
@@ -19,6 +20,21 @@ str_coderange(VALUE str) https://github.com/ruby/ruby/blob/trunk/ext/-test-/string/coderange.c#L20
     UNREACHABLE;
 }
 
+/* return coderange without scan */
+static VALUE
+str_coderange(VALUE str)
+{
+    return coderange_int2sym(ENC_CODERANGE(str));
+}
+
+/* scan coderange and return the result */
+static VALUE
+str_coderange_scan(VALUE str)
+{
+    ENC_CODERANGE_SET(str, ENC_CODERANGE_UNKNOWN);
+    return coderange_int2sym(rb_enc_str_coderange(str));
+}
+
 void
 Init_coderange(VALUE klass)
 {
@@ -27,4 +43,5 @@ Init_coderange(VALUE klass) https://github.com/ruby/ruby/blob/trunk/ext/-test-/string/coderange.c#L43
     sym_unknown = ID2SYM(rb_intern("unknown"));
     sym_broken = ID2SYM(rb_intern("broken"));
     rb_define_method(klass, "coderange", str_coderange, 0);
+    rb_define_method(klass, "coderange_scan", str_coderange_scan, 0);
 }
Index: test/-ext-/string/test_coderange.rb
===================================================================
--- test/-ext-/string/test_coderange.rb	(revision 0)
+++ test/-ext-/string/test_coderange.rb	(revision 45649)
@@ -0,0 +1,59 @@ https://github.com/ruby/ruby/blob/trunk/test/-ext-/string/test_coderange.rb#L1
+# coding: ascii-8bit
+require 'test/unit'
+require "-test-/string/string"
+require "rbconfig/sizeof"
+
+class Test_StringCoderange < Test::Unit::TestCase
+  def setup
+    @sizeof_voidp = RbConfig::SIZEOF["void*"]
+    @a8 = Encoding::ASCII_8BIT
+    @a7 = Encoding::US_ASCII
+    @u8 = Encoding::UTF_8
+  end
+
+  def test_ascii8bit
+    enc = @a8
+    str = "a"
+    str.force_encoding(enc)
+    assert_equal :"7bit", Bug::String.new(str).coderange_scan
+
+    str = "a\xBE".force_encoding(enc)
+    assert_equal :valid, Bug::String.new(str).coderange_scan
+  end
+
+  def test_usascii
+    enc = @a7
+    str = "a"
+    str.force_encoding(enc)
+    assert_equal :"7bit", Bug::String.new(str).coderange_scan
+
+    str = "a" * (@sizeof_voidp * 2)
+    str << "\xBE"
+    str.force_encoding(enc)
+    assert_equal :broken, Bug::String.new(str).coderange_scan
+  end
+
+  def test_utf8
+    enc = @u8
+    str = "a"
+    str.force_encoding(enc)
+    assert_equal :"7bit", Bug::String.new(str).coderange_scan
+
+    str = "a" * (@sizeof_voidp * 3)
+    str << "aa\xC2\x80"
+    str.force_encoding(enc)
+    assert_equal :valid, Bug::String.new(str).coderange_scan
+
+    str = "a" * (@sizeof_voidp * 2)
+    str << "\xC2\x80"
+    str << "a" * (@sizeof_voidp * 2)
+    str.force_encoding(enc)
+    assert_equal :valid, Bug::String.new(str).coderange_scan
+
+    str = "a" * (@sizeof_voidp * 2)
+    str << "\xC1\x80"
+    str << "a" * (@sizeof_voidp * 2)
+    str.force_encoding(enc)
+    assert_equal :broken, Bug::String.new(str).coderange_scan
+  end
+end

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

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