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

ruby-changes:18017

From: yugui <ko1@a...>
Date: Thu, 2 Dec 2010 17:07:05 +0900 (JST)
Subject: [ruby-changes:18017] Ruby:r30038 (ruby_1_9_2): merges r29446 and r29448 from trunk into ruby_1_9_2.

yugui	2010-12-02 17:06:46 +0900 (Thu, 02 Dec 2010)

  New Revision: 30038

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

  Log:
    merges r29446 and r29448 from trunk into ruby_1_9_2.
    --
    * numeric.c (rb_enc_uint_chr): split from int_chr.
    
    * numeric.c (int_chr): use rb_enc_uint_chr.
    
    * include/ruby/encoding.h (rb_enc_uint_chr): added.
    --
    * io.c (rb_io_ungetc): use unsigned int for GB18030.

  Modified files:
    branches/ruby_1_9_2/ChangeLog
    branches/ruby_1_9_2/include/ruby/encoding.h
    branches/ruby_1_9_2/io.c
    branches/ruby_1_9_2/numeric.c
    branches/ruby_1_9_2/test/ruby/test_io_m17n.rb
    branches/ruby_1_9_2/version.h

Index: ruby_1_9_2/include/ruby/encoding.h
===================================================================
--- ruby_1_9_2/include/ruby/encoding.h	(revision 30037)
+++ ruby_1_9_2/include/ruby/encoding.h	(revision 30038)
@@ -98,6 +98,7 @@
 char* rb_enc_nth(const char*, const char*, long, rb_encoding*);
 VALUE rb_obj_encoding(VALUE);
 VALUE rb_enc_str_buf_cat(VALUE str, const char *ptr, long len, rb_encoding *enc);
+VALUE rb_enc_uint_chr(unsigned int code, rb_encoding *enc);
 
 VALUE rb_external_str_new_with_enc(const char *ptr, long len, rb_encoding *);
 VALUE rb_str_export_to_enc(VALUE, rb_encoding *);
Index: ruby_1_9_2/ChangeLog
===================================================================
--- ruby_1_9_2/ChangeLog	(revision 30037)
+++ ruby_1_9_2/ChangeLog	(revision 30038)
@@ -1,3 +1,15 @@
+Tue Oct 12 15:36:09 2010  NARUSE, Yui  <naruse@r...>
+
+	* io.c (rb_io_ungetc): use unsigned int for GB18030.
+
+Tue Oct 12 15:10:31 2010  NARUSE, Yui  <naruse@r...>
+
+	* numeric.c (rb_enc_uint_chr): split from int_chr.
+
+	* numeric.c (int_chr): use rb_enc_uint_chr.
+
+	* include/ruby/encoding.h (rb_enc_uint_chr): added.
+
 Tue Oct 12 14:04:41 2010  NARUSE, Yui  <naruse@r...>
 
 	* numeric.c (int_chr): a codepoint of Ruby M17N must be 32bit
Index: ruby_1_9_2/io.c
===================================================================
--- ruby_1_9_2/io.c	(revision 30037)
+++ ruby_1_9_2/io.c	(revision 30038)
@@ -3193,12 +3193,13 @@
     rb_io_check_char_readable(fptr);
     if (NIL_P(c)) return Qnil;
     if (FIXNUM_P(c)) {
-	int cc = FIX2INT(c);
-	rb_encoding *enc = io_read_encoding(fptr);
-	char buf[16];
-
-	c = rb_str_new(buf, rb_enc_mbcput(cc, buf, enc));
+	c = rb_enc_uint_chr(FIX2UINT(c), io_read_encoding(fptr));
     }
+#if SIZEOF_LONG > SIZEOF_INT
+    else if (TYPE(c) == T_BIGNUM) {
+	c = rb_enc_uint_chr(NUM2UINT(c), io_read_encoding(fptr));
+    }
+#endif
     else {
 	SafeStringValue(c);
     }
Index: ruby_1_9_2/numeric.c
===================================================================
--- ruby_1_9_2/numeric.c	(revision 30037)
+++ ruby_1_9_2/numeric.c	(revision 30038)
@@ -2038,6 +2038,19 @@
     return rb_funcall(num, '-', 1, INT2FIX(1));
 }
 
+VALUE
+rb_enc_uint_chr(unsigned int code, rb_encoding *enc)
+{
+    int n;
+    VALUE str;
+    if ((n = rb_enc_codelen(code, enc)) <= 0) {
+	rb_raise(rb_eRangeError, "%d out of char range", code);
+    }
+    str = rb_enc_str_new(0, n, enc);
+    rb_enc_mbcput(code, RSTRING_PTR(str), enc);
+    return str;
+}
+
 /*
  *  call-seq:
  *     int.chr([encoding])  ->  string
@@ -2054,16 +2067,14 @@
 int_chr(int argc, VALUE *argv, VALUE num)
 {
     char c;
-    int n;
-    uint32_t i = NUM2UINT(num);
+    unsigned int i = NUM2UINT(num);
     rb_encoding *enc;
-    VALUE str;
 
     switch (argc) {
       case 0:
 	if (i < 0) {
 	  out_of_range:
-	    rb_raise(rb_eRangeError, "%"PRIdVALUE " out of char range", i);
+	    rb_raise(rb_eRangeError, "%d out of char range", i);
 	}
 	if (0xff < i) {
 	    enc = rb_default_internal_encoding();
@@ -2086,13 +2097,7 @@
     enc = rb_to_encoding(argv[0]);
     if (!enc) enc = rb_ascii8bit_encoding();
   decode:
-#if SIZEOF_INT < SIZEOF_VALUE
-    if (i > UINT_MAX) goto out_of_range;
-#endif
-    if (i < 0 || (n = rb_enc_codelen(i, enc)) <= 0) goto out_of_range;
-    str = rb_enc_str_new(0, n, enc);
-    rb_enc_mbcput(i, RSTRING_PTR(str), enc);
-    return str;
+    return rb_enc_uint_chr(i, enc);
 }
 
 /*
Index: ruby_1_9_2/version.h
===================================================================
--- ruby_1_9_2/version.h	(revision 30037)
+++ ruby_1_9_2/version.h	(revision 30038)
@@ -1,5 +1,5 @@
 #define RUBY_VERSION "1.9.2"
-#define RUBY_PATCHLEVEL 68
+#define RUBY_PATCHLEVEL 69
 #define RUBY_VERSION_MAJOR 1
 #define RUBY_VERSION_MINOR 9
 #define RUBY_VERSION_TEENY 1
Index: ruby_1_9_2/test/ruby/test_io_m17n.rb
===================================================================
--- ruby_1_9_2/test/ruby/test_io_m17n.rb	(revision 30037)
+++ ruby_1_9_2/test/ruby/test_io_m17n.rb	(revision 30038)
@@ -418,6 +418,30 @@
     }
   end
 
+  def test_ungetc_int
+    with_tmpdir {
+      generate_file('tmp', "A")
+      s = open("tmp", "r:GB18030") {|f|
+        f.ungetc(0x8431A439)
+        f.read
+      }
+      assert_equal(Encoding::GB18030, s.encoding)
+      assert_str_equal(0x8431A439.chr("GB18030")+"A", s)
+    }
+  end
+
+  def test_ungetc_str
+    with_tmpdir {
+      generate_file('tmp', "A")
+      s = open("tmp", "r:GB18030") {|f|
+        f.ungetc(0x8431A439.chr("GB18030"))
+        f.read
+      }
+      assert_equal(Encoding::GB18030, s.encoding)
+      assert_str_equal(0x8431A439.chr("GB18030")+"A", s)
+    }
+  end
+
   def test_ungetc_stateful_conversion
     with_tmpdir {
       src = "before \e$B\x23\x30\x23\x31\e(B after".force_encoding("iso-2022-jp")

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

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