From a9b15a4e0cf2f26e05cf0c6761daf7f04f2cf196 Mon Sep 17 00:00:00 2001 From: duerst Date: Sun, 20 Jan 2008 11:00:24 +0000 Subject: [PATCH] Sun Jan 20 20:00:20 2008 Martin Duerst * transcode.c, enc/trans/utf_16_32.c, test/ruby/test_transcode.rb: added UTF-16LE conversions. * fixed changelog for last commit git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15144 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 9 ++- enc/trans/utf_16_32.c | 142 +++++++++++++++++++++++++++++++++++- test/ruby/test_transcode.rb | 95 ++++++++++++------------ transcode.c | 1 + 4 files changed, 200 insertions(+), 47 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4f4bdd62bc..adbcfb87e2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,11 @@ -Sun Jan 20 15:08:08 2008 Martin Duerst +Sun Jan 20 20:00:20 2008 Martin Duerst + + * transcode.c, enc/trans/utf_16_32.c, test/ruby/test_transcode.rb: + added UTF-16LE conversions. + + * fixed changelog for last commit + +Sun Jan 20 17:54:00 2008 Martin Duerst * added changelog for last commit diff --git a/enc/trans/utf_16_32.c b/enc/trans/utf_16_32.c index c0f5a7ab70..1eb04c1519 100644 --- a/enc/trans/utf_16_32.c +++ b/enc/trans/utf_16_32.c @@ -58,11 +58,69 @@ fun_so_to_utf_16be(const unsigned char* s, unsigned char* o) return 4; } } + +static int +fun_so_from_utf_16le(const unsigned char* s, unsigned char* o) +{ + if (!s[1] && s[0]<0x80) { + o[0] = s[0]; + return 1; + } + else if (s[1]<0x08) { + o[0] = 0xC0 | (s[1]<<2) | (s[0]>>6); + o[1] = 0x80 | s[0]&0x3F; + return 2; + } + else if ((s[1]&0xF8)!=0xD8) { + o[0] = 0xE0 | s[1]>>4; + o[1] = 0x80 | ((s[1]&0x0F)<<2) | (s[0]>>6); + o[2] = 0x80 | s[0]&0x3F; + return 3; + } + else { + unsigned int u = (((s[1]&0x03)<<2)|(s[0]>>6)) + 1; + o[0] = 0xF0 | u>>2; + o[1] = 0x80 | ((u&0x03)<<4) | (s[0]>>2)&0x0F; + o[2] = 0x80 | ((s[0]&0x03)<<4) | ((s[3]&0x03)<<2) | (s[2]>>6); + o[3] = 0x80 | s[2]&0x3F; + return 4; + } +} + +static int +fun_so_to_utf_16le(const unsigned char* s, unsigned char* o) +{ + if (!(s[0]&0x80)) { + o[1] = 0x00; + o[0] = s[0]; + return 2; + } + else if ((s[0]&0xE0)==0xC0) { + o[1] = (s[0]>>2)&0x07; + o[0] = ((s[0]&0x03)<<6) | s[1]&0x3F; + return 2; + } + else if ((s[0]&0xF0)==0xE0) { + o[1] = (s[0]<<4) | (s[1]>>2)^0x20; + o[0] = (s[1]<<6) | s[2]^0x80; + return 2; + } + else { + int w = (((s[0]&0x07)<<2) | (s[1]>>4)&0x03) - 1; + o[1] = 0xD8 | (w>>2); + o[0] = (w<<6) | ((s[1]&0x0F)<<2) | ((s[2]>>4)-8); + o[3] = 0xDC | ((s[2]>>2)&0x03); + o[2] = (s[2]<<6) | (s[3]&~0x80); + return 4; + } +} static const unsigned char from_UTF_16BE_00_offsets[256] = { /* used by from_UTF_16BE_00 */ /* used by from_UTF_16BE_D8 */ /* used by from_UTF_16BE_D8_00_00 */ + /* used by from_UTF_16LE */ + /* used by from_UTF_16LE_00_D8 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -108,6 +166,8 @@ from_UTF_16BE_D8_00_00 = { static const unsigned char from_UTF_16BE_D8_00_offsets[256] = { + /* used by from_UTF_16BE_D8_00 */ + /* used by from_UTF_16LE_00_D8_00 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -147,6 +207,8 @@ from_UTF_16BE_D8 = { static const unsigned char from_UTF_16BE_offsets[256] = { + /* used by from_UTF_16BE */ + /* used by from_UTF_16LE_00 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -200,6 +262,13 @@ to_UTF_16BE_82 = { /* used as to_UTF_16BE_F0_90 */ /* used as to_UTF_16BE_F1_80 */ /* used as to_UTF_16BE_F4_80 */ + /* used as to_UTF_16LE */ + /* used as to_UTF_16LE_E0 */ + /* used as to_UTF_16LE_E1 */ + /* used as to_UTF_16LE_ED */ + /* used as to_UTF_16LE_F0_90 */ + /* used as to_UTF_16LE_F1_80 */ + /* used as to_UTF_16LE_F4_80 */ to_UTF_16BE_82_offsets, from_UTF_16BE_00_infos }; @@ -219,6 +288,8 @@ to_UTF_16BE_E0_infos[2] = { }; static const BYTE_LOOKUP to_UTF_16BE_E0 = { + /* used as to_UTF_16BE */ + /* used as to_UTF_16LE */ to_UTF_16BE_E0_offsets, to_UTF_16BE_E0_infos }; @@ -233,6 +304,10 @@ to_UTF_16BE_E1 = { /* used as to_UTF_16BE_F0 */ /* used as to_UTF_16BE_F1 */ /* used as to_UTF_16BE_F4 */ + /* used as to_UTF_16LE */ + /* used as to_UTF_16LE_F0 */ + /* used as to_UTF_16LE_F1 */ + /* used as to_UTF_16LE_F4 */ to_UTF_16BE_82_offsets, to_UTF_16BE_E1_infos }; @@ -243,6 +318,8 @@ to_UTF_16BE_ED_infos[2] = { }; static const BYTE_LOOKUP to_UTF_16BE_ED = { + /* used as to_UTF_16BE */ + /* used as to_UTF_16LE */ to_UTF_16BE_E0_offsets, to_UTF_16BE_ED_infos }; @@ -262,6 +339,8 @@ to_UTF_16BE_F0_infos[2] = { }; static const BYTE_LOOKUP to_UTF_16BE_F0 = { + /* used as to_UTF_16BE */ + /* used as to_UTF_16LE */ to_UTF_16BE_F0_offsets, to_UTF_16BE_F0_infos }; @@ -272,6 +351,8 @@ to_UTF_16BE_F1_infos[1] = { }; static const BYTE_LOOKUP to_UTF_16BE_F1 = { + /* used as to_UTF_16BE */ + /* used as to_UTF_16LE */ to_UTF_16BE_82_offsets, to_UTF_16BE_F1_infos }; @@ -282,6 +363,8 @@ to_UTF_16BE_F4_infos[2] = { }; static const BYTE_LOOKUP to_UTF_16BE_F4 = { + /* used as to_UTF_16BE */ + /* used as to_UTF_16LE */ to_UTF_16BE_F0_offsets, to_UTF_16BE_F4_infos }; @@ -313,6 +396,8 @@ to_UTF_16BE_infos[9] = { }; static const BYTE_LOOKUP to_UTF_16BE = { + /* used as to_UTF_16BE */ + /* used as to_UTF_16LE */ to_UTF_16BE_offsets, to_UTF_16BE_infos }; @@ -323,10 +408,65 @@ rb_to_UTF_16BE = { NULL, NULL, NULL, NULL, NULL, &fun_so_to_utf_16be }; +static const struct byte_lookup* const +from_UTF_16LE_00_D8_00_infos[2] = { + INVALID, FUNso, +}; +static const BYTE_LOOKUP +from_UTF_16LE_00_D8_00 = { + from_UTF_16BE_D8_00_offsets, + from_UTF_16LE_00_D8_00_infos +}; + +static const struct byte_lookup* const +from_UTF_16LE_00_D8_infos[1] = { + &from_UTF_16LE_00_D8_00, +}; +static const BYTE_LOOKUP +from_UTF_16LE_00_D8 = { + from_UTF_16BE_00_offsets, + from_UTF_16LE_00_D8_infos +}; + +static const struct byte_lookup* const +from_UTF_16LE_00_infos[3] = { + FUNso, &from_UTF_16LE_00_D8, + INVALID, +}; +static const BYTE_LOOKUP +from_UTF_16LE_00 = { + from_UTF_16BE_offsets, + from_UTF_16LE_00_infos +}; + +static const struct byte_lookup* const +from_UTF_16LE_infos[1] = { + &from_UTF_16LE_00, +}; +static const BYTE_LOOKUP +from_UTF_16LE = { + from_UTF_16BE_00_offsets, + from_UTF_16LE_infos +}; + +static rb_transcoder +rb_from_UTF_16LE = { + "UTF-16LE", "UTF-8", &from_UTF_16LE, 4, 0, + NULL, NULL, NULL, NULL, NULL, &fun_so_from_utf_16le +}; + +static rb_transcoder +rb_to_UTF_16LE = { + "UTF-8", "UTF-16LE", &to_UTF_16BE, 4, 1, + NULL, NULL, NULL, NULL, NULL, &fun_so_to_utf_16le +}; + void Init_utf_16_32(void) { rb_register_transcoder(&rb_from_UTF_16BE); rb_register_transcoder(&rb_to_UTF_16BE); + rb_register_transcoder(&rb_from_UTF_16LE); + rb_register_transcoder(&rb_to_UTF_16LE); } -/* Footprint (bytes): gross: 3420, saved: 1992, net: 1428 */ +/* Footprint (bytes): gross: 6036, saved: 4548, net: 1488 */ diff --git a/test/ruby/test_transcode.rb b/test/ruby/test_transcode.rb index cf5600926d..cbb942f45f 100644 --- a/test/ruby/test_transcode.rb +++ b/test/ruby/test_transcode.rb @@ -125,52 +125,57 @@ class TestTranscode < Test::Unit::TestCase assert_equal(test_start, test_start.encode('UTF-8',enc).encode(enc).force_encoding('ASCII-8BIT')) end end - - def test_utf_16be - check_both_ways("abc".force_encoding('UTF-8'), "\x00a\x00b\x00c", 'utf-16be') - check_both_ways("\u00E9", "\x00\xE9", 'utf-16be'); - check_both_ways("\u00E9\u0070\u00E9\u0065", "\x00\xE9\x00\x70\x00\xE9\x00\x65", 'utf-16be') # épée - check_both_ways("\u677E\u672C\u884C\u5F18", "\x67\x7E\x67\x2C\x88\x4C\x5F\x18", 'utf-16be') # 松本行弘 - check_both_ways("\u9752\u5C71\u5B66\u9662\u5927\u5B66", "\x97\x52\x5C\x71\x5B\x66\x96\x62\x59\x27\x5B\x66", - 'utf-16be') # 青山学院大学 - check_both_ways("Martin D\u00FCrst", "\x00M\x00a\x00r\x00t\x00i\x00n\x00 \x00D\x00\xFC\x00r\x00s\x00t", - 'utf-16be') # Martin Dürst + + def check_utf_16_both_ways(utf8, raw) + copy = raw.dup + 0.step(copy.length-1, 2) { |i| copy[i+1], copy[i] = copy[i], copy[i+1] } + check_both_ways(utf8, raw, 'utf-16be') + check_both_ways(utf8, copy, 'utf-16le') + end + + def test_utf_16 + check_utf_16_both_ways("abc", "\x00a\x00b\x00c") + check_utf_16_both_ways("\u00E9", "\x00\xE9"); + check_utf_16_both_ways("\u00E9\u0070\u00E9\u0065", "\x00\xE9\x00\x70\x00\xE9\x00\x65") # épée + check_utf_16_both_ways("\u677E\u672C\u884C\u5F18", "\x67\x7E\x67\x2C\x88\x4C\x5F\x18") # 松本行弘 + check_utf_16_both_ways("\u9752\u5C71\u5B66\u9662\u5927\u5B66", "\x97\x52\x5C\x71\x5B\x66\x96\x62\x59\x27\x5B\x66") # 青山学院大学 + check_utf_16_both_ways("Martin D\u00FCrst", "\x00M\x00a\x00r\x00t\x00i\x00n\x00 \x00D\x00\xFC\x00r\x00s\x00t") # Martin Dürst # BMP - check_both_ways("\u0000", "\x00\x00", 'utf-16be') - check_both_ways("\u007F", "\x00\x7F", 'utf-16be') - check_both_ways("\u0080", "\x00\x80", 'utf-16be') - check_both_ways("\u0555", "\x05\x55", 'utf-16be') - check_both_ways("\u04AA", "\x04\xAA", 'utf-16be') - check_both_ways("\u0333", "\x03\x33", 'utf-16be') - check_both_ways("\u04CC", "\x04\xCC", 'utf-16be') - check_both_ways("\u00F0", "\x00\xF0", 'utf-16be') - check_both_ways("\u070F", "\x07\x0F", 'utf-16be') - check_both_ways("\u07FF", "\x07\xFF", 'utf-16be') - check_both_ways("\u0800", "\x08\x00", 'utf-16be') - check_both_ways("\uD7FF", "\xD7\xFF", 'utf-16be') - check_both_ways("\uE000", "\xE0\x00", 'utf-16be') - check_both_ways("\uFFFF", "\xFF\xFF", 'utf-16be') - check_both_ways("\u5555", "\x55\x55", 'utf-16be') - check_both_ways("\uAAAA", "\xAA\xAA", 'utf-16be') - check_both_ways("\u3333", "\x33\x33", 'utf-16be') - check_both_ways("\uCCCC", "\xCC\xCC", 'utf-16be') - check_both_ways("\uF0F0", "\xF0\xF0", 'utf-16be') - check_both_ways("\u0F0F", "\x0F\x0F", 'utf-16be') - check_both_ways("\uFF00", "\xFF\x00", 'utf-16be') - check_both_ways("\u00FF", "\x00\xFF", 'utf-16be') + check_utf_16_both_ways("\u0000", "\x00\x00") + check_utf_16_both_ways("\u007F", "\x00\x7F") + check_utf_16_both_ways("\u0080", "\x00\x80") + check_utf_16_both_ways("\u0555", "\x05\x55") + check_utf_16_both_ways("\u04AA", "\x04\xAA") + check_utf_16_both_ways("\u0333", "\x03\x33") + check_utf_16_both_ways("\u04CC", "\x04\xCC") + check_utf_16_both_ways("\u00F0", "\x00\xF0") + check_utf_16_both_ways("\u070F", "\x07\x0F") + check_utf_16_both_ways("\u07FF", "\x07\xFF") + check_utf_16_both_ways("\u0800", "\x08\x00") + check_utf_16_both_ways("\uD7FF", "\xD7\xFF") + check_utf_16_both_ways("\uE000", "\xE0\x00") + check_utf_16_both_ways("\uFFFF", "\xFF\xFF") + check_utf_16_both_ways("\u5555", "\x55\x55") + check_utf_16_both_ways("\uAAAA", "\xAA\xAA") + check_utf_16_both_ways("\u3333", "\x33\x33") + check_utf_16_both_ways("\uCCCC", "\xCC\xCC") + check_utf_16_both_ways("\uF0F0", "\xF0\xF0") + check_utf_16_both_ways("\u0F0F", "\x0F\x0F") + check_utf_16_both_ways("\uFF00", "\xFF\x00") + check_utf_16_both_ways("\u00FF", "\x00\xFF") # outer planes - check_both_ways("\u{10000}", "\xD8\x00\xDC\x00", 'utf-16be') - check_both_ways("\u{FFFFF}", "\xDB\xBF\xDF\xFF", 'utf-16be') - check_both_ways("\u{100000}", "\xDB\xC0\xDC\x00", 'utf-16be') - check_both_ways("\u{10FFFF}", "\xDB\xFF\xDF\xFF", 'utf-16be') - check_both_ways("\u{105555}", "\xDB\xD5\xDD\x55", 'utf-16be') - check_both_ways("\u{55555}", "\xD9\x15\xDD\x55", 'utf-16be') - check_both_ways("\u{AAAAA}", "\xDA\x6A\xDE\xAA", 'utf-16be') - check_both_ways("\u{33333}", "\xD8\x8C\xDF\x33", 'utf-16be') - check_both_ways("\u{CCCCC}", "\xDA\xF3\xDC\xCC", 'utf-16be') - check_both_ways("\u{8F0F0}", "\xD9\xFC\xDC\xF0", 'utf-16be') - check_both_ways("\u{F0F0F}", "\xDB\x83\xDF\x0F", 'utf-16be') - check_both_ways("\u{8FF00}", "\xD9\xFF\xDF\x00", 'utf-16be') - check_both_ways("\u{F00FF}", "\xDB\x80\xDC\xFF", 'utf-16be') + check_utf_16_both_ways("\u{10000}", "\xD8\x00\xDC\x00") + check_utf_16_both_ways("\u{FFFFF}", "\xDB\xBF\xDF\xFF") + check_utf_16_both_ways("\u{100000}", "\xDB\xC0\xDC\x00") + check_utf_16_both_ways("\u{10FFFF}", "\xDB\xFF\xDF\xFF") + check_utf_16_both_ways("\u{105555}", "\xDB\xD5\xDD\x55") + check_utf_16_both_ways("\u{55555}", "\xD9\x15\xDD\x55") + check_utf_16_both_ways("\u{AAAAA}", "\xDA\x6A\xDE\xAA") + check_utf_16_both_ways("\u{33333}", "\xD8\x8C\xDF\x33") + check_utf_16_both_ways("\u{CCCCC}", "\xDA\xF3\xDC\xCC") + check_utf_16_both_ways("\u{8F0F0}", "\xD9\xFC\xDC\xF0") + check_utf_16_both_ways("\u{F0F0F}", "\xDB\x83\xDF\x0F") + check_utf_16_both_ways("\u{8FF00}", "\xD9\xFF\xDF\x00") + check_utf_16_both_ways("\u{F00FF}", "\xDB\x80\xDC\xFF") end end diff --git a/transcode.c b/transcode.c index 1a6efc6100..86d5c6ad0e 100644 --- a/transcode.c +++ b/transcode.c @@ -105,6 +105,7 @@ init_transcoder_table(void) rb_declare_transcoder("EUC-JP", "UTF-8", "japanese"); rb_declare_transcoder("ISO-2022-JP", "UTF-8", "japanese"); rb_declare_transcoder("UTF-16BE", "UTF-8", "utf_16_32"); + rb_declare_transcoder("UTF-16LE", "UTF-8", "utf_16_32"); } #define encoding_equal(enc1, enc2) (STRCASECMP(enc1, enc2) == 0)