ruby-changes:5310
From: nobu <ko1@a...>
Date: Wed, 4 Jun 2008 17:52:53 +0900 (JST)
Subject: [ruby-changes:5310] Ruby:r16809 (trunk, ruby_1_8): * ext/iconv/iconv.c (iconv_iconv): fix for length argument and now
nobu 2008-06-04 17:52:25 +0900 (Wed, 04 Jun 2008)
New Revision: 16809
Modified files:
branches/ruby_1_8/ChangeLog
branches/ruby_1_8/ext/iconv/iconv.c
trunk/ChangeLog
trunk/ext/iconv/iconv.c
Log:
* ext/iconv/iconv.c (iconv_iconv): fix for length argument and now
allows range. [ruby-core:17092]
http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ext/iconv/iconv.c?r1=16809&r2=16808&diff_format=u
http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ChangeLog?r1=16809&r2=16808&diff_format=u
http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=16809&r2=16808&diff_format=u
http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/ruby_1_8/ext/iconv/iconv.c?r1=16809&r2=16808&diff_format=u
Index: ChangeLog
===================================================================
--- ChangeLog (revision 16808)
+++ ChangeLog (revision 16809)
@@ -1,3 +1,8 @@
+Wed Jun 4 17:52:18 2008 Nobuyoshi Nakada <nobu@r...>
+
+ * ext/iconv/iconv.c (iconv_iconv): fix for length argument and now
+ allows range. [ruby-core:17092]
+
Wed Jun 4 15:45:41 2008 Akinori MUSHA <knu@i...>
* enumerator.c (enumerator_with_index, enumerator_with_memo): Fix
Index: ext/iconv/iconv.c
===================================================================
--- ext/iconv/iconv.c (revision 16808)
+++ ext/iconv/iconv.c (revision 16809)
@@ -110,7 +110,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, int toidx,
+static VALUE iconv_convert _((iconv_t cd, VALUE str, long start, long length, int toidx,
struct iconv_env_t* env));
static VALUE iconv_s_allocate _((VALUE klass));
static VALUE iconv_initialize _((int argc, VALUE *argv, VALUE self));
@@ -327,7 +327,7 @@
}
static VALUE
-iconv_convert(iconv_t cd, VALUE str, int start, int length, int toidx, struct iconv_env_t* env)
+iconv_convert(iconv_t cd, VALUE str, long start, long length, int toidx, struct iconv_env_t* env)
{
VALUE ret = Qfalse;
VALUE error = Qfalse;
@@ -374,17 +374,9 @@
slen = RSTRING_LEN(str);
inptr = RSTRING_PTR(str);
- 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;
@@ -828,16 +820,32 @@
{
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)) {
+ VALUE n = rb_str_length(StringValue(str));
+ slen = NUM2LONG(n);
+ }
+ 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)) {
+ length = NIL_P(n2) ? -1 : NUM2LONG(n2);
+ }
+ }
+ if (start > 0 || length > 0) {
+ rb_encoding *enc = rb_enc_get(str);
+ const char *s = RSTRING_PTR(str), *e = s + RSTRING_LEN(str);
+ const char *ps = s;
+ if (start > 0) {
+ start = (ps = rb_enc_nth(s, e, start, enc)) - s;
+ }
+ if (length > 0) {
+ length = rb_enc_nth(ps, e, length, enc) - ps;
+ }
+ }
- str = iconv_convert(VALUE2ICONV(cd), str,
- NIL_P(n1) ? 0 : NUM2INT(n1),
- NIL_P(n2) ? -1 : NUM2INT(n2),
- ENCODING_GET(self),
- NULL);
- return str;
+ return iconv_convert(VALUE2ICONV(cd), str, start, length, ENCODING_GET(self), NULL);
}
/*
@@ -1034,7 +1042,7 @@
static VALUE
iconv_failure_inspect(VALUE self)
{
- 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/ext/iconv/iconv.c
===================================================================
--- ruby_1_8/ext/iconv/iconv.c (revision 16808)
+++ ruby_1_8/ext/iconv/iconv.c (revision 16809)
@@ -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,17 @@
{
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 = NUM2LONG(n2);
+ }
+ }
- 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 +823,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/ChangeLog
===================================================================
--- ruby_1_8/ChangeLog (revision 16808)
+++ ruby_1_8/ChangeLog (revision 16809)
@@ -1,3 +1,8 @@
+Wed Jun 4 17:52:18 2008 Nobuyoshi Nakada <nobu@r...>
+
+ * ext/iconv/iconv.c (iconv_iconv): fix for length argument and now
+ allows range. [ruby-core:17092]
+
Wed Jun 4 13:06:58 2008 Nobuyoshi Nakada <nobu@r...>
* configure.in (CFLAGS, CXXFLAGS): include additional flags to
--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/