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

ruby-changes:5355

From: knu <ko1@a...>
Date: Fri, 6 Jun 2008 17:06:50 +0900 (JST)
Subject: [ruby-changes:5355] Ruby:r16856 (ruby_1_8_7): * ext/iconv/iconv.c (iconv_iconv): fix for length argument and now

knu	2008-06-06 17:03:49 +0900 (Fri, 06 Jun 2008)

  New Revision: 16856

  Added directories:
    branches/ruby_1_8_7/test/iconv/
  Modified files:
    branches/ruby_1_8_7/ChangeLog
    branches/ruby_1_8_7/ext/iconv/iconv.c
    branches/ruby_1_8_7/version.h

  Log:
    * ext/iconv/iconv.c (iconv_iconv): fix for length argument and now
      allows range.  [ruby-core:17092] [ruby-core:17115]

  Added: branches/ruby_1_8_7/test/iconv/

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8_7/ext/iconv/iconv.c?r1=16856&r2=16855&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8_7/ChangeLog?r1=16856&r2=16855&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8_7/test/iconv
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8_7/version.h?r1=16856&r2=16855&diff_format=u

Index: ruby_1_8_7/ext/iconv/iconv.c
===================================================================
--- ruby_1_8_7/ext/iconv/iconv.c	(revision 16855)
+++ ruby_1_8_7/ext/iconv/iconv.c	(revision 16856)
@@ -101,7 +101,7 @@
 static VALUE iconv_free _((VALUE cd));
 static VALUE iconv_try _((iconv_t cd, const char **inptr, size_t *inlen, char **outptr, size_t *outlen));
 static VALUE rb_str_derive _((VALUE str, const char* ptr, int len));
-static VALUE iconv_convert _((iconv_t cd, VALUE str, int start, int length, struct iconv_env_t* env));
+static VALUE iconv_convert _((iconv_t cd, VALUE str, long start, long length, struct iconv_env_t* env));
 static VALUE iconv_s_allocate _((VALUE klass));
 static VALUE iconv_initialize _((VALUE self, VALUE to, VALUE from));
 static VALUE iconv_s_open _((VALUE self, VALUE to, VALUE from));
@@ -170,7 +170,7 @@
 	}
 	if (cd == (iconv_t)-1) {
 	    int inval = errno == EINVAL;
-	    char *s = inval ? "invalid encoding " : "iconv";
+	    const char *s = inval ? "invalid encoding " : "iconv";
 	    volatile VALUE msg = rb_str_new(0, strlen(s) + RSTRING(to)->len +
 					    RSTRING(from)->len + 8);
 
@@ -362,13 +362,13 @@
 static VALUE
 iconv_convert
 #ifdef HAVE_PROTOTYPES
-    (iconv_t cd, VALUE str, int start, int length, struct iconv_env_t* env)
+    (iconv_t cd, VALUE str, long start, long length, struct iconv_env_t* env)
 #else /* HAVE_PROTOTYPES */
     (cd, str, start, length, env)
     iconv_t cd;
     VALUE str;
-    int start;
-    int length;
+    long start;
+    long length;
     struct iconv_env_t *env;
 #endif /* HAVE_PROTOTYPES */
 {
@@ -417,17 +417,9 @@
 	slen = RSTRING(str)->len;
 	inptr = RSTRING(str)->ptr;
 
-	if (start < 0 ? (start += slen) < 0 : start >= slen)
-	    length = 0;
-	else if (length < 0 && (length += slen + 1) < 0)
-	    length = 0;
-	else if ((length -= start) < 0)
-	    length = 0;
-	else {
-	    inptr += start;
-	    if (length > slen)
-		length = slen;
-	}
+	inptr += start;
+	if (length < 0 || length > start + slen)
+	    length = slen - start;
     }
     instart = inptr;
     inlen = length;
@@ -757,14 +749,22 @@
 {
     VALUE str, n1, n2;
     VALUE cd = check_iconv(self);
+    long start = 0, length = 0, slen = 0;
 
-    n1 = n2 = Qnil;
     rb_scan_args(argc, argv, "12", &str, &n1, &n2);
+    if (!NIL_P(str)) slen = RSTRING_LEN(StringValue(str));
+    if (argc != 2 || !RTEST(rb_range_beg_len(n1, &start, &length, slen, 0))) {
+	if (NIL_P(n1) || ((start = NUM2LONG(n1)) < 0 ? (start += slen) >= 0 : start < slen)) {
+	    if (NIL_P(n2)) {
+		length = -1;
+	    }
+	    else if ((length = NUM2LONG(n2)) >= slen - start) {
+		length = slen - start;
+	    }
+	}
+    }
 
-    return iconv_convert(VALUE2ICONV(cd), str,
-			 NIL_P(n1) ? 0 : NUM2INT(n1),
-			 NIL_P(n2) ? -1 : NUM2INT(n2),
-			 NULL);
+    return iconv_convert(VALUE2ICONV(cd), str, start, length, NULL);
 }
 
 /*
@@ -828,7 +828,7 @@
     VALUE self;
 #endif /* HAVE_PROTOTYPES */
 {
-    char *cname = rb_class2name(CLASS_OF(self));
+    const char *cname = rb_class2name(CLASS_OF(self));
     VALUE success = rb_attr_get(self, rb_success);
     VALUE failed = rb_attr_get(self, rb_failed);
     VALUE str = rb_str_buf_cat2(rb_str_new2("#<"), cname);
Index: ruby_1_8_7/ChangeLog
===================================================================
--- ruby_1_8_7/ChangeLog	(revision 16855)
+++ ruby_1_8_7/ChangeLog	(revision 16856)
@@ -1,3 +1,8 @@
+Fri Jun  6 16:58:23 2008  Nobuyoshi Nakada  <nobu@r...>
+
+	* ext/iconv/iconv.c (iconv_iconv): fix for length argument and now
+	  allows range.  [ruby-core:17092] [ruby-core:17115]
+
 Wed Jun  4 17:22:30 2008  Akinori MUSHA  <knu@i...>
 
 	* NEWS: Fix typos and move misplaced entries.
Index: ruby_1_8_7/version.h
===================================================================
--- ruby_1_8_7/version.h	(revision 16855)
+++ ruby_1_8_7/version.h	(revision 16856)
@@ -1,15 +1,15 @@
 #define RUBY_VERSION "1.8.7"
-#define RUBY_RELEASE_DATE "2008-06-03"
+#define RUBY_RELEASE_DATE "2008-06-06"
 #define RUBY_VERSION_CODE 187
-#define RUBY_RELEASE_CODE 20080603
-#define RUBY_PATCHLEVEL 4
+#define RUBY_RELEASE_CODE 20080606
+#define RUBY_PATCHLEVEL 5
 
 #define RUBY_VERSION_MAJOR 1
 #define RUBY_VERSION_MINOR 8
 #define RUBY_VERSION_TEENY 7
 #define RUBY_RELEASE_YEAR 2008
 #define RUBY_RELEASE_MONTH 6
-#define RUBY_RELEASE_DAY 3
+#define RUBY_RELEASE_DAY 6
 
 #ifdef RUBY_EXTERN
 RUBY_EXTERN const char ruby_version[];
Index: ruby_1_8_7/test/iconv/test_basic.rb
===================================================================
--- ruby_1_8_7/test/iconv/test_basic.rb	(revision 0)
+++ ruby_1_8_7/test/iconv/test_basic.rb	(revision 16856)
@@ -0,0 +1,49 @@
+require File.join(File.dirname(__FILE__), "utils.rb")
+
+TestIconv.testcase(:Basic) do
+  def test_euc2sjis
+    iconv = Iconv.open('SHIFT_JIS', 'EUC-JP')
+    str = iconv.iconv(EUCJ_STR)
+    str << iconv.iconv(nil)
+    assert_equal(SJIS_STR, str)
+    iconv.close
+  end
+
+  def test_close
+    iconv = Iconv.new('Shift_JIS', 'EUC-JP')
+    output = ""
+    begin
+      output += iconv.iconv(EUCJ_STR)
+      output += iconv.iconv(nil)
+    ensure
+      assert_respond_to(iconv, :close)
+      assert_equal("", iconv.close)
+      assert_equal(SJIS_STR, output)
+    end
+  end
+
+  def test_open_without_block
+    assert_respond_to(Iconv, :open)
+    iconv = Iconv.open('SHIFT_JIS', 'EUC-JP')
+    str = iconv.iconv(EUCJ_STR)
+    str << iconv.iconv(nil)
+    assert_equal(SJIS_STR, str )
+    iconv.close
+  end
+
+  def test_open_with_block
+    input = "#{EUCJ_STR}\n"*2
+    output = ""
+    Iconv.open("Shift_JIS", "EUC-JP") do |cd|
+      input.each_line do |s|
+        output << cd.iconv(s)
+      end
+      output << cd.iconv(nil)
+    end
+    assert_equal("#{SJIS_STR}\n"*2, output)
+  end
+
+  def test_unknown_encoding
+    assert_raise(Iconv::InvalidEncoding) { Iconv.iconv("utf-8", "X-UKNOWN", "heh") }
+  end
+end

Property changes on: ruby_1_8_7/test/iconv/test_basic.rb
___________________________________________________________________
Name: svn:eol-style
   + LF

Index: ruby_1_8_7/test/iconv/utils.rb
===================================================================
--- ruby_1_8_7/test/iconv/utils.rb	(revision 0)
+++ ruby_1_8_7/test/iconv/utils.rb	(revision 16856)
@@ -0,0 +1,36 @@
+begin
+  require 'iconv'
+rescue LoadError
+else
+  require 'test/unit'
+end
+
+module TestIconv
+  if defined?(::Iconv)
+    def self.testcase(name, &block)
+      const_set(name, klass = Class.new(::Test::Unit::TestCase))
+      klass.name
+      klass.__send__(:include, self)
+      klass.class_eval(&block)
+    end
+  else
+    def self.testcase(name)
+    end
+  end
+end
+
+module TestIconv
+  if defined?(::Encoding) and String.method_defined?(:force_encoding)
+    def self.encode(str, enc)
+      str.force_encoding(enc)
+    end
+  else
+    def self.encode(str, enc)
+      str
+    end
+  end
+
+  ASCII = "ascii"
+  EUCJ_STR = encode("\xa4\xa2\xa4\xa4\xa4\xa6\xa4\xa8\xa4\xaa", "EUC-JP").freeze
+  SJIS_STR = encode("\x82\xa0\x82\xa2\x82\xa4\x82\xa6\x82\xa8", "Shift_JIS").freeze
+end if defined?(::Iconv)

Property changes on: ruby_1_8_7/test/iconv/utils.rb
___________________________________________________________________
Name: svn:eol-style
   + LF

Index: ruby_1_8_7/test/iconv/test_option.rb
===================================================================
--- ruby_1_8_7/test/iconv/test_option.rb	(revision 0)
+++ ruby_1_8_7/test/iconv/test_option.rb	(revision 16856)
@@ -0,0 +1,31 @@
+require File.join(File.dirname(__FILE__), "utils.rb")
+
+TestIconv.testcase(:Option) do
+  def test_ignore_option
+    iconv = Iconv.new('SHIFT_JIS', 'EUC-JP//ignore')
+    str = iconv.iconv(EUCJ_STR)
+    str << iconv.iconv(nil)
+    assert_equal(SJIS_STR, str)
+    iconv.close
+
+    iconv = Iconv.new('SHIFT_JIS//IGNORE', 'EUC-JP//ignore')
+    str = iconv.iconv(EUCJ_STR)
+    str << iconv.iconv(nil)
+    assert_equal(SJIS_STR, str)
+    iconv.close
+  end
+
+  def test_translit_option
+    iconv = Iconv.new('SHIFT_JIS', 'EUC-JP//ignore')
+    str = iconv.iconv(EUCJ_STR)
+    str << iconv.iconv(nil)
+    assert_equal(SJIS_STR, str)
+    iconv.close
+
+    iconv = Iconv.new('SHIFT_JIS//TRANSLIT', 'EUC-JP//translit//ignore')
+    str = iconv.iconv(EUCJ_STR)
+    str << iconv.iconv(nil)
+    assert_equal(SJIS_STR, str)
+    iconv.close
+  end
+end

Property changes on: ruby_1_8_7/test/iconv/test_option.rb
___________________________________________________________________
Name: svn:eol-style
   + LF

Index: ruby_1_8_7/test/iconv/test_partial.rb
===================================================================
--- ruby_1_8_7/test/iconv/test_partial.rb	(revision 0)
+++ ruby_1_8_7/test/iconv/test_partial.rb	(revision 16856)
@@ -0,0 +1,41 @@
+require File.join(File.dirname(__FILE__), "utils.rb")
+
+TestIconv.testcase(:Partial) do
+  def test_partial_ascii
+    c = Iconv.open(ASCII, ASCII)
+    ref = '[ruby-core:17092]'
+  rescue
+    return
+  else
+    assert_equal("abc", c.iconv("abc"))
+    assert_equal("c",   c.iconv("abc", 2),     "#{ref}: with start")
+    assert_equal("c",   c.iconv("abc", 2, 1),  "#{ref}: with start, length")
+    assert_equal("c",   c.iconv("abc", 2, 5),  "#{ref}: with start, longer length")
+    assert_equal("bc",  c.iconv("abc", -2),    "#{ref}: with nagative start")
+    assert_equal("b",   c.iconv("abc", -2, 1), "#{ref}: with nagative start, length")
+    assert_equal("bc",  c.iconv("abc", -2, 5), "#{ref}: with nagative start, longer length")
+    assert_equal("",    c.iconv("abc", 5),     "#{ref}: with OOB")
+    assert_equal("",    c.iconv("abc", 5, 2),  "#{ref}: with OOB, length")
+  ensure
+    c.close if c
+  end
+
+  def test_partial_euc2sjis
+    c = Iconv.open('SHIFT_JIS', 'EUC-JP')
+  rescue
+    return
+  else
+    assert_equal(SJIS_STR[0, 2],   c.iconv(EUCJ_STR, 0, 2))
+    assert_equal(SJIS_STR,         c.iconv(EUCJ_STR, 0, 20))
+    assert_equal(SJIS_STR[2..-1],  c.iconv(EUCJ_STR, 2))
+    assert_equal(SJIS_STR[2, 2],   c.iconv(EUCJ_STR, 2, 2))
+    assert_equal(SJIS_STR[2..-1],  c.iconv(EUCJ_STR, 2, 20))
+    assert_equal(SJIS_STR[-4..-1], c.iconv(EUCJ_STR, -4))
+    assert_equal(SJIS_STR[-4, 2],  c.iconv(EUCJ_STR, -4, 2))
+    assert_equal(SJIS_STR[-4..-1], c.iconv(EUCJ_STR, -4, 20))
+    assert_equal("",               c.iconv(EUCJ_STR, 20))
+    assert_equal("",               c.iconv(EUCJ_STR, 20, 2))
+  ensure
+    c.close
+  end
+end

Property changes on: ruby_1_8_7/test/iconv/test_partial.rb
___________________________________________________________________
Name: svn:eol-style
   + LF


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

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