mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* transcode.c (make_encoding): new function.
(make_encobj): new function. (econv_s_asciicompat_encoding): use make_encoding. (rb_econv_open_exc): use SUPPLEMENTAL_CONVERSION. (econv_convpath): use encoding object in the result. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19288 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
c25f22fb79
commit
10ddce3833
3 changed files with 55 additions and 25 deletions
|
@ -1,3 +1,11 @@
|
|||
Thu Sep 11 02:25:34 2008 Tanaka Akira <akr@fsij.org>
|
||||
|
||||
* transcode.c (make_encoding): new function.
|
||||
(make_encobj): new function.
|
||||
(econv_s_asciicompat_encoding): use make_encoding.
|
||||
(rb_econv_open_exc): use SUPPLEMENTAL_CONVERSION.
|
||||
(econv_convpath): use encoding object in the result.
|
||||
|
||||
Thu Sep 11 02:14:38 2008 Tanaka Akira <akr@fsij.org>
|
||||
|
||||
* transcode.c (econv_convpath): new method.
|
||||
|
|
|
@ -809,21 +809,28 @@ class TestEncodingConverter < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_convpath
|
||||
eucjp = Encoding::EUC_JP
|
||||
utf8 = Encoding::UTF_8
|
||||
utf16be = Encoding::UTF_16BE
|
||||
utf16le = Encoding::UTF_16LE
|
||||
iso88591 = Encoding::ISO_8859_1
|
||||
iso2022jp = Encoding::ISO_2022_JP
|
||||
siso2022jp = Encoding::STATELESS_ISO_2022_JP
|
||||
assert_equal([], Encoding::Converter.new("", "").convpath)
|
||||
assert_equal([["EUC-JP", "UTF-8"], ["UTF-8", "ISO-8859-1"]],
|
||||
Encoding::Converter.new("EUC-JP", "ISO-8859-1").convpath)
|
||||
assert_equal([["EUC-JP", "stateless-ISO-2022-JP"], ["stateless-ISO-2022-JP", "ISO-2022-JP"]],
|
||||
Encoding::Converter.new("EUC-JP", "ISO-2022-JP").convpath)
|
||||
assert_equal([["ISO-2022-JP", "stateless-ISO-2022-JP"],
|
||||
["stateless-ISO-2022-JP", "EUC-JP"],
|
||||
["EUC-JP", "UTF-8"],
|
||||
["UTF-8", "ISO-8859-1"]],
|
||||
Encoding::Converter.new("ISO-2022-JP", "ISO-8859-1").convpath)
|
||||
assert_equal(["universal_newline", ["UTF-8", "UTF-16BE"]],
|
||||
Encoding::Converter.new("UTF-8", "UTF-16BE", universal_newline: true).convpath)
|
||||
assert_equal([["UTF-16BE", "UTF-8"], "universal_newline"],
|
||||
Encoding::Converter.new("UTF-16BE", "UTF-8", universal_newline: true).convpath)
|
||||
assert_equal([["UTF-16BE", "UTF-8"], "universal_newline", ["UTF-8", "UTF-16LE"]],
|
||||
Encoding::Converter.new("UTF-16BE", "UTF-16LE", universal_newline: true).convpath)
|
||||
assert_equal([[eucjp, utf8], [utf8, iso88591]],
|
||||
Encoding::Converter.new(eucjp, iso88591).convpath)
|
||||
assert_equal([[eucjp, siso2022jp], [siso2022jp, iso2022jp]],
|
||||
Encoding::Converter.new(eucjp, iso2022jp).convpath)
|
||||
assert_equal([[iso2022jp, siso2022jp],
|
||||
[siso2022jp, eucjp],
|
||||
[eucjp, utf8],
|
||||
[utf8, iso88591]],
|
||||
Encoding::Converter.new(iso2022jp, iso88591).convpath)
|
||||
assert_equal(["universal_newline", [utf8, utf16be]],
|
||||
Encoding::Converter.new(utf8, utf16be, universal_newline: true).convpath)
|
||||
assert_equal([[utf16be, utf8], "universal_newline"],
|
||||
Encoding::Converter.new(utf16be, utf8, universal_newline: true).convpath)
|
||||
assert_equal([[utf16be, utf8], "universal_newline", [utf8, utf16le]],
|
||||
Encoding::Converter.new(utf16be, utf16le, universal_newline: true).convpath)
|
||||
end
|
||||
end
|
||||
|
|
35
transcode.c
35
transcode.c
|
@ -2560,6 +2560,22 @@ make_dummy_encoding(const char *name)
|
|||
return enc;
|
||||
}
|
||||
|
||||
static rb_encoding *
|
||||
make_encoding(const char *name)
|
||||
{
|
||||
rb_encoding *enc;
|
||||
enc = rb_enc_find(name);
|
||||
if (!enc)
|
||||
enc = make_dummy_encoding(name);
|
||||
return enc;
|
||||
}
|
||||
|
||||
static VALUE
|
||||
make_encobj(const char *name)
|
||||
{
|
||||
return rb_enc_from_encoding(make_encoding(name));
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* Encoding::Converter.asciicompat_encoding(string) => encoding or nil
|
||||
|
@ -2592,10 +2608,7 @@ econv_s_asciicompat_encoding(VALUE klass, VALUE arg)
|
|||
if (result_name == NULL)
|
||||
return Qnil;
|
||||
|
||||
result_enc = rb_enc_find(result_name);
|
||||
|
||||
if (!result_enc)
|
||||
result_enc = make_dummy_encoding(result_name);
|
||||
result_enc = make_encoding(result_name);
|
||||
|
||||
return rb_enc_from_encoding(result_enc);
|
||||
}
|
||||
|
@ -2695,7 +2708,7 @@ econv_init(int argc, VALUE *argv, VALUE self)
|
|||
rb_exc_raise(rb_econv_open_exc(sname, dname, ecflags));
|
||||
}
|
||||
|
||||
if (*sname && *dname) { /* check "" to "universal_newline" */
|
||||
if (!SUPPLEMENTAL_CONVERSION(sname, dname)) {
|
||||
if (!senc)
|
||||
senc = make_dummy_encoding(sname);
|
||||
if (!denc)
|
||||
|
@ -2795,14 +2808,16 @@ econv_destination_encoding(VALUE self)
|
|||
*
|
||||
* ec = Encoding::Converter.new("ISo-8859-1", "EUC-JP", crlf_newline: true)
|
||||
* p ec.convpath
|
||||
* #=> [["ISO-8859-1", "UTF-8"], ["UTF-8", "EUC-JP"], "crlf_newline"]
|
||||
* #=> [[#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>],
|
||||
* # [#<Encoding:UTF-8>, #<Encoding:EUC-JP>],
|
||||
* # "crlf_newline"]
|
||||
*
|
||||
* A element of the array is a pair of string or a string.
|
||||
* A element of the array is a pair of encodings or a string.
|
||||
* The pair means encoding conversion.
|
||||
* The string means decorator.
|
||||
*
|
||||
* In the above example, ["ISO-8859-1", "UTF-8"] means a converter from
|
||||
* ISO-8859-1 to UTF-8.
|
||||
* In the above example, [#<Encoding:ISO-8859-1>, #<Encoding:UTF-8>] means
|
||||
* a converter from ISO-8859-1 to UTF-8.
|
||||
* "crlf_newline" means newline converter from LF to CRLF.
|
||||
*/
|
||||
static VALUE
|
||||
|
@ -2819,7 +2834,7 @@ econv_convpath(VALUE self)
|
|||
if (SUPPLEMENTAL_CONVERSION(tr->src_encoding, tr->dst_encoding))
|
||||
v = rb_str_new_cstr(tr->dst_encoding);
|
||||
else
|
||||
v = rb_assoc_new(rb_str_new_cstr(tr->src_encoding), rb_str_new_cstr(tr->dst_encoding));
|
||||
v = rb_assoc_new(make_encobj(tr->src_encoding), make_encobj(tr->dst_encoding));
|
||||
rb_ary_push(result, v);
|
||||
}
|
||||
return result;
|
||||
|
|
Loading…
Reference in a new issue