summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--ext/iconv/iconv.c48
2 files changed, 33 insertions, 20 deletions
diff --git a/ChangeLog b/ChangeLog
index 0d215218e6..df7b5f4c1d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Wed Jun 4 17:52:18 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * 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@iDaemons.org>
* enumerator.c (enumerator_with_index, enumerator_with_memo): Fix
diff --git a/ext/iconv/iconv.c b/ext/iconv/iconv.c
index 9f4c23b435..3aaf08fbd1 100644
--- a/ext/iconv/iconv.c
+++ b/ext/iconv/iconv.c
@@ -110,7 +110,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, 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 @@ 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, 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 @@ iconv_convert(iconv_t cd, VALUE str, int start, int length, int toidx, struct ic
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 @@ iconv_iconv(int argc, VALUE *argv, VALUE self)
{
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 @@ iconv_failure_failed(VALUE self)
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);