1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* encoding.c (enc_check_encoding): returns index now.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13751 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2007-10-22 02:03:10 +00:00
parent 00befb4b3f
commit 3cb10b32bb
2 changed files with 18 additions and 13 deletions

View file

@ -1,4 +1,6 @@
Mon Oct 22 10:57:07 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
Mon Oct 22 11:03:09 2007 Nobuyoshi Nakada <nobu@ruby-lang.org>
* encoding.c (enc_check_encoding): returns index now.
* encoding.c (rb_enc_compatible): check if two objects have compatible
encodings.

View file

@ -56,36 +56,39 @@ rb_enc_from_encoding(rb_encoding *encoding)
return enc;
}
static rb_encoding *
static int
enc_check_encoding(VALUE obj)
{
int index;
if (SPECIAL_CONST_P(obj) || BUILTIN_TYPE(obj) != T_DATA ||
RDATA(obj)->dmark != enc_mark) {
return 0;
return -1;
}
return RDATA(obj)->data;
index = rb_enc_get_index(obj);
if (rb_enc_from_index(index) != RDATA(obj)->data)
return -1;
return index;
}
static rb_encoding *
enc_get_encoding(VALUE obj)
{
rb_encoding *enc = enc_check_encoding(obj);
if (!enc) {
if (enc_check_encoding(obj) < 0) {
rb_raise(rb_eTypeError, "wrong argument type %s (expected Encoding)",
rb_obj_classname(obj));
}
return enc;
return RDATA(obj)->data;
}
int
rb_to_encoding_index(VALUE enc)
{
rb_encoding *encoding;
int idx;
if (NIL_P(enc)) return 0;
encoding = enc_check_encoding(enc);
if (encoding) {
return rb_enc_to_index(encoding);
idx = enc_check_encoding(enc);
if (idx >= 0) {
return index;
}
else {
return rb_enc_find_index(StringValueCStr(enc));
@ -99,8 +102,8 @@ rb_to_encoding(VALUE enc)
int idx;
if (NIL_P(enc)) return rb_enc_from_index(0);
encoding = enc_check_encoding(enc);
if (encoding) return encoding;
idx = enc_check_encoding(enc);
if (idx >= 0) return RDATA(obj)->data;
if ((idx = rb_enc_find_index(StringValueCStr(enc))) < 0) {
rb_raise(rb_eArgError, "unknown encoding name - %s", RSTRING_PTR(enc));
}