summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-09-15 05:17:18 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-09-15 05:17:18 +0000
commit28aa3a401c1bd18965b2bab0e76eb8a2e36f111d (patch)
treee18dfd3a94d6402c244fecf188984b342aaa8bed
parentfdd0b16f380c51419ef47973c6ce637964759dc4 (diff)
downloadruby-28aa3a401c1bd18965b2bab0e76eb8a2e36f111d.tar.gz
ruby-28aa3a401c1bd18965b2bab0e76eb8a2e36f111d.tar.xz
ruby-28aa3a401c1bd18965b2bab0e76eb8a2e36f111d.zip
* encoding.c (rb_enc_compatible): accepst other than strings and
regexps. [ruby-core:18595] * encoding.c (rb_enc_get_index): works files and encodings. git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@19357 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog7
-rw-r--r--encoding.c44
-rw-r--r--test/ruby/test_m17n.rb7
3 files changed, 40 insertions, 18 deletions
diff --git a/ChangeLog b/ChangeLog
index 2567ae6b3..89492428a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+Mon Sep 15 13:53:33 2008 NARUSE, Yui <naruse@ruby-lang.org>
+
+ * encoding.c (rb_enc_compatible): accepst other than strings and
+ regexps. [ruby-core:18595]
+
+ * encoding.c (rb_enc_get_index): works files and encodings.
+
Mon Sep 15 13:17:21 2008 Tadayoshi Funaba <tadf@dotrb.org>
* complex.c (nucomp_eql_p): new.
diff --git a/encoding.c b/encoding.c
index 3752d1c73..61dc93836 100644
--- a/encoding.c
+++ b/encoding.c
@@ -545,14 +545,30 @@ rb_id_encoding(void)
int
rb_enc_get_index(VALUE obj)
{
- int i;
-
- i = ENCODING_GET_INLINED(obj);
- if (i == ENCODING_INLINE_MAX) {
- VALUE iv;
+ int i = -1;
+ VALUE tmp;
- iv = rb_ivar_get(obj, rb_id_encoding());
- i = NUM2INT(iv);
+ switch (BUILTIN_TYPE(obj)) {
+ case T_STRING:
+ case T_REGEXP:
+ i = ENCODING_GET_INLINED(obj);
+ if (i == ENCODING_INLINE_MAX) {
+ VALUE iv;
+
+ iv = rb_ivar_get(obj, rb_id_encoding());
+ i = NUM2INT(iv);
+ }
+ break;
+ case T_FILE:
+ tmp = rb_funcall(obj, rb_intern("internal_encoding"), 0, 0);
+ if (NIL_P(tmp)) obj = rb_funcall(obj, rb_intern("external_encoding"), 0, 0);
+ else obj = tmp;
+ if (NIL_P(obj)) break;
+ case T_DATA:
+ if (RDATA(obj)->dmark == enc_mark) {
+ i = enc_check_encoding(obj);
+ }
+ break;
}
return i;
}
@@ -631,9 +647,11 @@ rb_enc_compatible(VALUE str1, VALUE str2)
if (!rb_enc_asciicompat(enc1) || !rb_enc_asciicompat(enc2)) {
return 0;
}
- if (BUILTIN_TYPE(str2) == T_REGEXP && idx2 == ENCINDEX_US_ASCII)
+
+ /* objects whose encoding is the same of contents */
+ if (BUILTIN_TYPE(str2) != T_STRING && idx2 == ENCINDEX_US_ASCII)
return enc1;
- if (BUILTIN_TYPE(str1) == T_REGEXP && idx1 == ENCINDEX_US_ASCII)
+ if (BUILTIN_TYPE(str1) != T_STRING && idx1 == ENCINDEX_US_ASCII)
return enc2;
if (BUILTIN_TYPE(str1) != T_STRING) {
@@ -880,14 +898,6 @@ enc_compatible_p(VALUE klass, VALUE str1, VALUE str2)
{
rb_encoding *enc;
- if (SPECIAL_CONST_P(str1) || TYPE(str1) != T_STRING && TYPE(str1) != T_REGEXP) {
- rb_raise(rb_eTypeError, "wrong argument type %s (expected String or Regexp)",
- rb_obj_classname(str1));
- }
- if (SPECIAL_CONST_P(str2) || TYPE(str2) != T_STRING && TYPE(str2) != T_REGEXP) {
- rb_raise(rb_eTypeError, "wrong argument type %s (expected String or Regexp)",
- rb_obj_classname(str2));
- }
if (!enc_capable(str1)) return Qnil;
if (!enc_capable(str2)) return Qnil;
enc = rb_enc_compatible(str1, str2);
diff --git a/test/ruby/test_m17n.rb b/test/ruby/test_m17n.rb
index a87822753..855653fa8 100644
--- a/test/ruby/test_m17n.rb
+++ b/test/ruby/test_m17n.rb
@@ -1280,7 +1280,12 @@ class TestM17N < Test::Unit::TestCase
end
def test_compatible
- assert_raise(TypeError) {Encoding.compatible?("",0)}
+ assert_nil Encoding.compatible?("",0)
+ assert_equal(Encoding::UTF_8, Encoding.compatible?(Encoding::UTF_8, Encoding::UTF_8))
+ assert_equal(Encoding::UTF_8, Encoding.compatible?(Encoding::UTF_8, Encoding::US_ASCII))
+ assert_equal(Encoding::ASCII_8BIT,
+ Encoding.compatible?(Encoding::ASCII_8BIT, Encoding::US_ASCII))
+ assert_nil Encoding.compatible?(Encoding::UTF_8, Encoding::ASCII_8BIT)
end
def test_force_encoding