/*
* Document-method: iconv
* call-seq: iconv(str, start=0, length=-1)
*
* Converts string and returns the result.
* * If +str+ is a String, converts <tt>str[start, length]</tt> and returns the converted string.
* * If +str+ is +nil+, places converter itself into initial shift state and
* just returns a string containing the byte sequence to change the output
* buffer to its initial shift state.
* * Otherwise, raises an exception.
*
* === Parameters
*
* str:: string to be converted, or nil
* start:: starting offset
* length:: conversion length; nil or -1 means whole the string from start
*
* === Exceptions
*
* * IconvIllegalSequence
* * IconvInvalidCharacter
* * IconvOutOfRange
*
* === Examples
*
* See the Iconv documentation.
*/
static VALUE
iconv_iconv
(argc, argv, self)
int argc;
VALUE *argv;
VALUE self;
{
VALUE str, n1, n2;
VALUE cd = check_iconv(self);
long start = 0, length = 0, slen = 0;
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, start, length, NULL);
}