summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-06-06 08:03:49 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-06-06 08:03:49 +0000
commit70bde7a3b5347e6e68a6cb82714ef03304c13bb1 (patch)
tree4d2e32c21515e2d2bbd0ee41bc8a14997d107f93
parent6727093dbeaa4d3f4c7282b92992ed5f88addb57 (diff)
* ext/iconv/iconv.c (iconv_iconv): fix for length argument and now
allows range. [ruby-core:17092] [ruby-core:17115] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8_7@16856 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--ext/iconv/iconv.c44
-rw-r--r--test/iconv/test_basic.rb49
-rw-r--r--test/iconv/test_option.rb31
-rw-r--r--test/iconv/test_partial.rb41
-rw-r--r--test/iconv/utils.rb36
-rw-r--r--version.h8
7 files changed, 188 insertions, 26 deletions
diff --git a/ChangeLog b/ChangeLog
index 7bec47aa53..aa0ae8c70d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Fri Jun 6 16:58:23 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * 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@iDaemons.org>
* NEWS: Fix typos and move misplaced entries.
diff --git a/ext/iconv/iconv.c b/ext/iconv/iconv.c
index fe02df1ce7..d989b97df2 100644
--- a/ext/iconv/iconv.c
+++ b/ext/iconv/iconv.c
@@ -101,7 +101,7 @@ static void iconv_dfree _((void *cd));
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 @@ iconv_create
}
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 @@ rb_str_derive
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 @@ iconv_convert
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 @@ iconv_iconv
{
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 @@ iconv_failure_inspect
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);
diff --git a/test/iconv/test_basic.rb b/test/iconv/test_basic.rb
new file mode 100644
index 0000000000..12f30e4a0e
--- /dev/null
+++ b/test/iconv/test_basic.rb
@@ -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
diff --git a/test/iconv/test_option.rb b/test/iconv/test_option.rb
new file mode 100644
index 0000000000..f879dbaf66
--- /dev/null
+++ b/test/iconv/test_option.rb
@@ -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
diff --git a/test/iconv/test_partial.rb b/test/iconv/test_partial.rb
new file mode 100644
index 0000000000..8e973b6df9
--- /dev/null
+++ b/test/iconv/test_partial.rb
@@ -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
diff --git a/test/iconv/utils.rb b/test/iconv/utils.rb
new file mode 100644
index 0000000000..99dce20837
--- /dev/null
+++ b/test/iconv/utils.rb
@@ -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)
diff --git a/version.h b/version.h
index bf0c6ba1e5..015891dc0f 100644
--- a/version.h
+++ b/version.h
@@ -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[];