Navigation Menu

Skip to content

Commit

Permalink
* ext/iconv/iconv.c (check_iconv): check if Iconv instance.
Browse files Browse the repository at this point in the history
* ext/iconv/iconv.c (iconv_convert): stringify argument.

* ext/iconv/iconv.c (iconv_failure_initialize): limit
  inspect message.  [ruby-dev:20785]

* ext/iconv/iconv.c (rb_str_derive): share with original
  string if possible.  [ruby-dev:20785]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4118 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed Jul 23, 2003
1 parent 66910f1 commit 0770898
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 34 deletions.
14 changes: 14 additions & 0 deletions ChangeLog
@@ -1,3 +1,17 @@
Wed Jul 23 10:11:15 2003 Nobuyoshi Nakada <nobu.nokada@softhome.net>

* ext/iconv/iconv.c (check_iconv): check if Iconv instance.

* ext/iconv/iconv.c (iconv_convert): stringify argument.

Tue Jul 22 19:16:40 2003 Tanaka Akira <akr@m17n.org>

* ext/iconv/iconv.c (iconv_failure_initialize): limit
inspect message. [ruby-dev:20785]

* ext/iconv/iconv.c (rb_str_derive): share with original
string if possible. [ruby-dev:20785]

Tue Jul 22 17:22:34 2003 Yukihiro Matsumoto <matz@ruby-lang.org>

* variable.c (rb_mod_const_missing): new method. [ruby-core:00441]
Expand Down
93 changes: 59 additions & 34 deletions ext/iconv/iconv.c
Expand Up @@ -121,7 +121,7 @@ map_charset
static iconv_t
iconv_create
#ifdef HAVE_PROTOTYPES
(VALUE to, VALUE from)
(VALUE to, VALUE from)
#else /* HAVE_PROTOTYPES */
(to, from)
VALUE to;
Expand Down Expand Up @@ -155,7 +155,7 @@ iconv_create
static void
iconv_dfree
#ifdef HAVE_PROTOTYPES
(void *cd)
(void *cd)
#else /* HAVE_PROTOTYPES */
(cd)
void *cd;
Expand All @@ -169,7 +169,7 @@ iconv_dfree
static VALUE
iconv_free
#ifdef HAVE_PROTOTYPES
(VALUE cd)
(VALUE cd)
#else /* HAVE_PROTOTYPES */
(cd)
VALUE cd;
Expand All @@ -180,10 +180,26 @@ iconv_free
return Qnil;
}

static VALUE
check_iconv
#ifdef HAVE_PROTOTYPES
(VALUE obj)
#else /* HAVE_PROTOTYPES */
(obj)
VALUE obj;
#endif /* HAVE_PROTOTYPES */
{
Check_Type(obj, T_DATA);
if (RDATA(obj)->dfree != ICONV_FREE) {
rb_raise(rb_eArgError, "Iconv expected (%s)", rb_class2name(CLASS_OF(obj)));
}
return (VALUE)DATA_PTR(obj);
}

static VALUE
iconv_try
#ifdef HAVE_PROTOTYPES
(iconv_t cd, const char **inptr, size_t *inlen, char **outptr, size_t *outlen)
(iconv_t cd, const char **inptr, size_t *inlen, char **outptr, size_t *outlen)
#else /* HAVE_PROTOTYPES */
(cd, inptr, inlen, outptr, outlen)
iconv_t cd;
Expand Down Expand Up @@ -218,10 +234,12 @@ iconv_try
#define iconv_fail(error, success, failed, env) \
rb_exc_raise(iconv_failure_initialize(error, success, failed, env))

#define FAILED_MAXLEN 16

static VALUE
iconv_failure_initialize
#ifdef HAVE_PROTOTYPES
(VALUE error, VALUE success, VALUE failed, struct iconv_env_t* env)
(VALUE error, VALUE success, VALUE failed, struct iconv_env_t* env)
#else /* HAVE_PROTOTYPES */
(error, success, failed, env)
VALUE error;
Expand All @@ -230,8 +248,16 @@ iconv_failure_initialize
struct iconv_env_t *env;
#endif /* HAVE_PROTOTYPES */
{
if (NIL_P(rb_attr_get(error, rb_mesg)))
rb_ivar_set(error, rb_mesg, rb_inspect(failed));
if (NIL_P(rb_attr_get(error, rb_mesg))) {
if (TYPE(failed) != T_STRING || RSTRING(failed)->len < FAILED_MAXLEN) {
rb_ivar_set(error, rb_mesg, rb_inspect(failed));
}
else {
VALUE mesg = rb_inspect(rb_str_substr(failed, 0, FAILED_MAXLEN));
rb_str_cat2(mesg, "...");
rb_ivar_set(error, rb_mesg, mesg);
}
}
if (env) {
success = rb_funcall3(env->ret, rb_inserter, 1, &success);
if (env->argc > 0) {
Expand All @@ -247,7 +273,7 @@ iconv_failure_initialize
static VALUE
rb_str_derive
#ifdef HAVE_PROTOTYPES
(VALUE str, const char* ptr, int len)
(VALUE str, const char* ptr, int len)
#else /* HAVE_PROTOTYPES */
(str, ptr, len)
VALUE str;
Expand All @@ -261,15 +287,18 @@ rb_str_derive
return rb_str_new(ptr, len);
if (RSTRING(str)->ptr == ptr && RSTRING(str)->len == len)
return str;
ret = rb_str_new(ptr, len);
if (RSTRING(str)->ptr + RSTRING(str)->len == ptr + len)
ret = rb_str_substr(str, ptr - RSTRING(str)->ptr, len);
else
ret = rb_str_new(ptr, len);
OBJ_INFECT(ret, str);
return ret;
}

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, int start, int length, struct iconv_env_t* env)
#else /* HAVE_PROTOTYPES */
(cd, str, start, length, env)
iconv_t cd;
Expand Down Expand Up @@ -307,7 +336,7 @@ iconv_convert
else {
int slen;

Check_Type(str, T_STRING);
StringValue(str);
slen = RSTRING(str)->len;
inptr = RSTRING(str)->ptr;

Expand Down Expand Up @@ -408,7 +437,7 @@ iconv_convert
static VALUE
iconv_s_allocate
#ifdef HAVE_PROTOTYPES
(VALUE klass)
(VALUE klass)
#else /* HAVE_PROTOTYPES */
(klass)
VALUE klass;
Expand All @@ -420,15 +449,15 @@ iconv_s_allocate
static VALUE
iconv_initialize
#ifdef HAVE_PROTOTYPES
(VALUE self, VALUE to, VALUE from)
(VALUE self, VALUE to, VALUE from)
#else /* HAVE_PROTOTYPES */
(self, to, from)
VALUE self;
VALUE to;
VALUE from;
#endif /* HAVE_PROTOTYPES */
{
iconv_free((VALUE)(DATA_PTR(self)));
iconv_free(check_iconv(self));
DATA_PTR(self) = NULL;
DATA_PTR(self) = (void *)ICONV2VALUE(iconv_create(to, from));
return self;
Expand All @@ -437,7 +466,7 @@ iconv_initialize
static VALUE
iconv_s_open
#ifdef HAVE_PROTOTYPES
(VALUE self, VALUE to, VALUE from)
(VALUE self, VALUE to, VALUE from)
#else /* HAVE_PROTOTYPES */
(self, to, from)
VALUE self;
Expand All @@ -447,12 +476,12 @@ iconv_s_open
{
VALUE cd = ICONV2VALUE(iconv_create(to, from));

self = Data_Wrap_Struct(self, NULL, ICONV_FREE, (void *)cd);
if (rb_block_given_p()) {
self = Data_Wrap_Struct(self, NULL, NULL, (void *)cd);
return rb_ensure(rb_yield, self, (VALUE(*)())iconv_finish, self);
}
else {
return Data_Wrap_Struct(self, NULL, ICONV_FREE, (void *)cd);
return self;
}
}

Expand All @@ -475,7 +504,7 @@ iconv_s_open
static VALUE
iconv_s_convert
#ifdef HAVE_PROTOTYPES
(struct iconv_env_t* env)
(struct iconv_env_t* env)
#else /* HAVE_PROTOTYPES */
(env)
struct iconv_env_t *env;
Expand All @@ -500,7 +529,7 @@ iconv_s_convert
static VALUE
iconv_s_iconv
#ifdef HAVE_PROTOTYPES
(int argc, VALUE *argv, VALUE self)
(int argc, VALUE *argv, VALUE self)
#else /* HAVE_PROTOTYPES */
(argc, argv, self)
int argc;
Expand Down Expand Up @@ -558,7 +587,7 @@ iconv_s_conv
static VALUE
iconv_init_state
#ifdef HAVE_PROTOTYPES
(VALUE cd)
(VALUE cd)
#else /* HAVE_PROTOTYPES */
(cd)
VALUE cd;
Expand All @@ -570,17 +599,14 @@ iconv_init_state
static VALUE
iconv_finish
#ifdef HAVE_PROTOTYPES
(VALUE self)
(VALUE self)
#else /* HAVE_PROTOTYPES */
(self)
VALUE self;
#endif /* HAVE_PROTOTYPES */
{
VALUE cd;

Check_Type(self, T_DATA);
VALUE cd = check_iconv(self);

cd = (VALUE)DATA_PTR(self);
if (!cd) return Qnil;
DATA_PTR(self) = NULL;

Expand Down Expand Up @@ -615,7 +641,7 @@ iconv_finish
static VALUE
iconv_iconv
#ifdef HAVE_PROTOTYPES
(int argc, VALUE *argv, VALUE self)
(int argc, VALUE *argv, VALUE self)
#else /* HAVE_PROTOTYPES */
(argc, argv, self)
int argc;
Expand All @@ -624,13 +650,12 @@ iconv_iconv
#endif /* HAVE_PROTOTYPES */
{
VALUE str, n1, n2;

Check_Type(self, T_DATA);
VALUE cd = check_iconv(self);

n1 = n2 = Qnil;
rb_scan_args(argc, argv, "12", &str, &n1, &n2);

return iconv_convert(VALUE2ICONV(DATA_PTR(self)), str,
return iconv_convert(VALUE2ICONV(cd), str,
NIL_P(n1) ? 0 : NUM2INT(n1),
NIL_P(n2) ? -1 : NUM2INT(n1),
NULL);
Expand Down Expand Up @@ -668,7 +693,7 @@ iconv_failure_success
VALUE self;
#endif /* HAVE_PROTOTYPES */
{
return rb_ivar_get(self, rb_success);
return rb_attr_get(self, rb_success);
}

/*
Expand All @@ -687,7 +712,7 @@ iconv_failure_failed
VALUE self;
#endif /* HAVE_PROTOTYPES */
{
return rb_ivar_get(self, rb_failed);
return rb_attr_get(self, rb_failed);
}

/*
Expand All @@ -699,15 +724,15 @@ iconv_failure_failed
static VALUE
iconv_failure_inspect
#ifdef HAVE_PROTOTYPES
(VALUE self)
(VALUE self)
#else /* HAVE_PROTOTYPES */
(self)
VALUE self;
#endif /* HAVE_PROTOTYPES */
{
char *cname = rb_class2name(CLASS_OF(self));
VALUE success = iconv_failure_success(self);
VALUE failed = iconv_failure_failed(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);
str = rb_str_buf_cat(str, ": ", 2);
str = rb_str_buf_append(str, rb_inspect(success));
Expand Down

0 comments on commit 0770898

Please sign in to comment.