ruby-changes:7714
From: nobu <ko1@a...>
Date: Mon, 8 Sep 2008 08:10:54 +0900 (JST)
Subject: [ruby-changes:7714] Ruby:r19235 (mvm): * enc/trans: ignore escape.c.
nobu 2008-09-08 08:10:46 +0900 (Mon, 08 Sep 2008) New Revision: 19235 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=19235 Log: * enc/trans: ignore escape.c. Modified directories: branches/mvm/enc/trans/ trunk/enc/trans/ Modified files: branches/mvm/enc/trans/escape.trans branches/mvm/enc/trans/iso2022.trans Property changes on: enc/trans ___________________________________________________________________ Name: svn:ignore - single_byte.c korean.c utf_16_32.c japanese.c japanese_euc.c japanese_sjis.c iso2022.c newline.c + single_byte.c korean.c utf_16_32.c japanese.c japanese_euc.c japanese_sjis.c iso2022.c newline.c escape.c Index: mvm/enc/trans/escape.trans =================================================================== --- mvm/enc/trans/escape.trans (revision 19234) +++ mvm/enc/trans/escape.trans (revision 19235) @@ -1,93 +1,42 @@ #include "transcode_data.h" -static int -fun_so_escape_html_chref(void *statep, const unsigned char *s, size_t l, unsigned char *o) -{ - switch (*s) { - case '&': - o[0] = '&'; - o[1] = 'a'; - o[2] = 'm'; - o[3] = 'p'; - o[4] = ';'; - return 5; +<% + def hexstr(str) + str.unpack("H*")[0] + end - case '<': - o[0] = '&'; - o[1] = 'l'; - o[2] = 't'; - o[3] = ';'; - return 4; + transcode_tblgen("", "amp-escaped", [ + ["{00-25,27-FF}", :nomap], + ["26", hexstr("&")] + ]) - case '>': - o[0] = '&'; - o[1] = 'g'; - o[2] = 't'; - o[3] = ';'; - return 4; + transcode_tblgen("", "xml-text-escaped", [ + ["{00-25,27-3B,3D,3F-FF}", :nomap], + ["26", hexstr("&")], + ["3C", hexstr("<")], + ["3E", hexstr(">")] + ]) - case '"': - o[0] = '&'; - o[1] = 'q'; - o[2] = 'u'; - o[3] = 'o'; - o[4] = 't'; - o[5] = ';'; - return 6; + transcode_tblgen("", "xml-attr-content-escaped", [ + ["{00-21,23-25,27-3B,3D,3F-FF}", :nomap], + ["22", hexstr(""")], + ["26", hexstr("&")], + ["3C", hexstr("<")], + ["3E", hexstr(">")] + ]) - default: - rb_bug("unexpected char"); - } -} -<% - map_amp = {} - map_amp["{00-25,27-FF}"] = :nomap - map_amp["26"] = :func_so - transcode_generate_node(ActionMap.parse(map_amp), "escape_amp_as_chref") - - map_html_text = {} - map_html_text["{00-25,27-3B,3D,3F-FF}"] = :nomap - map_html_text["26"] = :func_so - map_html_text["3C"] = :func_so - map_html_text["3E"] = :func_so - transcode_generate_node(ActionMap.parse(map_html_text), "escape_html_text") - - map_html_attr = {} - map_html_attr["{00-FF}"] = :func_so - transcode_generate_node(ActionMap.parse(map_html_attr), "escape_html_attr") + map_xml_attr_quote = {} + map_xml_attr_quote["{00-FF}"] = :func_so + transcode_generate_node(ActionMap.parse(map_xml_attr_quote), "escape_xml_attr_quote") %> <%= transcode_generated_code %> -static const rb_transcoder -rb_escape_amp_as_chref = { - "", "amp-escaped", escape_amp_as_chref, - TRANSCODE_TABLE_INFO, - 1, /* input_unit_length */ - 1, /* max_input */ - 5, /* max_output */ - stateless_converter, /* stateful_type */ - 0, NULL, NULL, - NULL, NULL, NULL, &fun_so_escape_html_chref -}; - -static const rb_transcoder -rb_escape_html_text = { - "", "html-text-escaped", escape_html_text, - TRANSCODE_TABLE_INFO, - 1, /* input_unit_length */ - 1, /* max_input */ - 5, /* max_output */ - stateless_converter, /* stateful_type */ - 0, NULL, NULL, - NULL, NULL, NULL, &fun_so_escape_html_chref -}; - #define END 0 #define NORMAL 1 static int -escape_html_attr_init(void *statep) +escape_xml_attr_quote_init(void *statep) { unsigned char *sp = statep; *sp = END; @@ -95,7 +44,7 @@ } static int -fun_so_escape_html_attr(void *statep, const unsigned char *s, size_t l, unsigned char *o) +fun_so_escape_xml_attr_quote(void *statep, const unsigned char *s, size_t l, unsigned char *o) { unsigned char *sp = statep; int n = 0; @@ -103,23 +52,12 @@ *sp = NORMAL; o[n++] = '"'; } - switch (s[0]) { - case '&': - case '<': - case '>': - case '"': - n += fun_so_escape_html_chref(statep, s, l, o+n); - break; - - default: - o[n++] = s[0]; - break; - } + o[n++] = s[0]; return n; } static int -escape_html_attr_finish(void *statep, unsigned char *o) +escape_xml_attr_quote_finish(void *statep, unsigned char *o) { unsigned char *sp = statep; int n = 0; @@ -135,23 +73,22 @@ } static const rb_transcoder -rb_escape_html_attr = { - "", "html-attr-escaped", escape_html_attr, +rb_escape_xml_attr_quote = { + "", "xml-attr-quoted", escape_xml_attr_quote, TRANSCODE_TABLE_INFO, 1, /* input_unit_length */ 1, /* max_input */ 7, /* max_output */ stateful_encoder, /* stateful_type */ - 1, escape_html_attr_init, escape_html_attr_init, - NULL, NULL, NULL, fun_so_escape_html_attr, - escape_html_attr_finish + 1, escape_xml_attr_quote_init, escape_xml_attr_quote_init, + NULL, NULL, NULL, fun_so_escape_xml_attr_quote, + escape_xml_attr_quote_finish }; void Init_escape(void) { - rb_register_transcoder(&rb_escape_amp_as_chref); - rb_register_transcoder(&rb_escape_html_text); - rb_register_transcoder(&rb_escape_html_attr); +<%= transcode_register_code %> + rb_register_transcoder(&rb_escape_xml_attr_quote); } Index: mvm/enc/trans/iso2022.trans =================================================================== --- mvm/enc/trans/iso2022.trans (revision 19234) +++ mvm/enc/trans/iso2022.trans (revision 19235) @@ -1,35 +1,55 @@ #include "transcode_data.h" <% - map = {} - map["1b2842"] = :func_so # designate US-ASCII to G0. "ESC ( B" - map["1b284a"] = :func_so # designate JIS X 0201 latin to G0. "ESC ( J" - map["1b2440"] = :func_so # designate JIS X 0208 1978 to G0. "ESC $ @" - map["1b2442"] = :func_so # designate JIS X 0208 1983 to G0. "ESC $ B" - map["{00-0d,10-1a,1c-7f}"] = :func_si + map = { + "1b2842" => :func_so, # designate US-ASCII to G0. "ESC ( B" + "1b284a" => :func_so, # designate JIS X 0201 latin to G0. "ESC ( J" + "1b2440" => :func_so, # designate JIS X 0208 1978 to G0. "ESC $ @" + "1b2442" => :func_so, # designate JIS X 0208 1983 to G0. "ESC $ B" + "{00-0d,10-1a,1c-7f}" => :func_si, + } + transcode_generate_node(ActionMap.parse(map), "iso2022jp_decoder") - map_jisx0208_rest = {} - map_jisx0208_rest["{21-7e}"] = :func_so + map_jisx0208_rest = { + "{21-7e}" => :func_so + } + transcode_generate_node(ActionMap.parse(map_jisx0208_rest), "iso2022jp_decoder_jisx0208_rest") - transcode_generate_node(ActionMap.parse(map), "iso2022jp_to_eucjp") - transcode_generate_node(ActionMap.parse(map_jisx0208_rest), "iso2022jp_to_eucjp_jisx0208_rest") + map = { + "{00-0d,10-1a,1c-7f}" => :func_so, + "90{a1-fe}{a1-fe}" => :func_so, + "92{a1-fe}{a1-fe}" => :func_so, + } + transcode_generate_node(ActionMap.parse(map), "iso2022jp_encoder") - map_eucjp = { + map = { + "{00-0d,10-1a,1c-7f}" => :nomap, + "90{a1-fe}{a1-fe}" => :func_so, + "92{a1-fe}{a1-fe}" => :func_so, + } + transcode_generate_node(ActionMap.parse(map), "stateless_iso2022jp_to_eucjp") + + map = { "{0e,0f,1b}" => :undef, - "{00-0d,10-1a,1c-7f}" => :func_so, + "{00-0d,10-1a,1c-7f}" => :nomap, "{a1-fe}{a1-fe}" => :func_so, "8e{a1-fe}" => :undef, "8f{a1-fe}{a1-fe}" => :undef, } + transcode_generate_node(ActionMap.parse(map), "eucjp_to_stateless_iso2022jp") - transcode_generate_node(ActionMap.parse(map_eucjp), "eucjp_to_iso2022jp") %> <%= transcode_generated_code %> #define G0_ASCII 0 -#define G0_JISX0208 1 +/* ignore JIS X 0201 latin */ +#define G0_JISX0208_1978 1 +#define G0_JISX0208_1983 2 +#define EMACS_MULE_LEADING_CODE_JISX0208_1978 0220 +#define EMACS_MULE_LEADING_CODE_JISX0208_1983 0222 + static int iso2022jp_init(void *statep) { @@ -39,19 +59,19 @@ } static VALUE -fun_si_iso2022jp_to_eucjp(void *statep, const unsigned char *s, size_t l) +fun_si_iso2022jp_decoder(void *statep, const unsigned char *s, size_t l) { unsigned char *sp = statep; if (*sp == G0_ASCII) return (VALUE)NOMAP; else if (0x21 <= s[0] && s[0] <= 0x7e) - return (VALUE)iso2022jp_to_eucjp_jisx0208_rest; + return (VALUE)iso2022jp_decoder_jisx0208_rest; else return (VALUE)INVALID; } static int -fun_so_iso2022jp_to_eucjp(void *statep, const unsigned char *s, size_t l, unsigned char* o) +fun_so_iso2022jp_decoder(void *statep, const unsigned char *s, size_t l, unsigned char* o) { unsigned char *sp = statep; if (s[0] == 0x1b) { @@ -66,75 +86,94 @@ else { switch (s[l-1]) { case '@': + *sp = G0_JISX0208_1978; + break; + case 'B': - *sp = G0_JISX0208; + *sp = G0_JISX0208_1983; break; } } return 0; } else { - o[0] = s[0] | 0x80; - o[1] = s[1] | 0x80; - return 2; + if (*sp == G0_JISX0208_1978) + o[0] = EMACS_MULE_LEADING_CODE_JISX0208_1978; + else + o[0] = EMACS_MULE_LEADING_CODE_JISX0208_1983; + o[1] = s[0] | 0x80; + o[2] = s[1] | 0x80; + return 3; } } static const rb_transcoder -rb_ISO_2022_JP_to_EUC_JP = { - "ISO-2022-JP", "EUC-JP", iso2022jp_to_eucjp, +rb_iso2022jp_decoder = { + "ISO-2022-JP", "stateless-ISO-2022-JP", iso2022jp_decoder, TRANSCODE_TABLE_INFO, 1, /* input_unit_length */ 3, /* max_input */ 3, /* max_output */ stateful_decoder, /* stateful_type */ 1, iso2022jp_init, iso2022jp_init, /* state_size, state_init, state_fini */ - NULL, fun_si_iso2022jp_to_eucjp, NULL, fun_so_iso2022jp_to_eucjp + NULL, fun_si_iso2022jp_decoder, NULL, fun_so_iso2022jp_decoder }; static int -fun_so_eucjp_to_iso2022jp(void *statep, const unsigned char *s, size_t l, unsigned char *o) +fun_so_iso2022jp_encoder(void *statep, const unsigned char *s, size_t l, unsigned char *o) { unsigned char *sp = statep; unsigned char *output0 = o; + int newstate; - if (*sp != (l == 1 ? G0_ASCII : G0_JISX0208)) { - if (l == 1) { + if (l == 1) + newstate = G0_ASCII; + else if (s[0] == EMACS_MULE_LEADING_CODE_JISX0208_1978) + newstate = G0_JISX0208_1978; + else + newstate = G0_JISX0208_1983; + + if (*sp != newstate) { + if (newstate == G0_ASCII) { *o++ = 0x1b; *o++ = '('; *o++ = 'B'; - *sp = G0_ASCII; } + else if (newstate == G0_JISX0208_1978) { + *o++ = 0x1b; + *o++ = '$'; + *o++ = '@'; + } else { *o++ = 0x1b; *o++ = '$'; *o++ = 'B'; - *sp = G0_JISX0208; /* JIS X 0208 1983 */ } + *sp = newstate; } if (l == 1) { *o++ = s[0] & 0x7f; } else { - *o++ = s[0] & 0x7f; *o++ = s[1] & 0x7f; + *o++ = s[2] & 0x7f; } return o - output0; } static int -iso2022jp_reset_sequence_size(void *statep) +iso2022jp_encoder_reset_sequence_size(void *statep) { unsigned char *sp = statep; - if (*sp == G0_JISX0208) + if (*sp != G0_ASCII) return 3; return 0; } static int -finish_eucjp_to_iso2022jp(void *statep, unsigned char *o) +finish_iso2022jp_encoder(void *statep, unsigned char *o) { unsigned char *sp = statep; unsigned char *output0 = o; @@ -151,23 +190,66 @@ } static const rb_transcoder -rb_EUC_JP_to_ISO_2022_JP = { - "EUC-JP", "ISO-2022-JP", eucjp_to_iso2022jp, +rb_iso2022jp_encoder = { + "stateless-ISO-2022-JP", "ISO-2022-JP", iso2022jp_encoder, TRANSCODE_TABLE_INFO, 1, /* input_unit_length */ 3, /* max_input */ 5, /* max_output */ stateful_encoder, /* stateful_type */ 1, iso2022jp_init, iso2022jp_init, /* state_size, state_init, state_fini */ - NULL, NULL, NULL, fun_so_eucjp_to_iso2022jp, - finish_eucjp_to_iso2022jp, - iso2022jp_reset_sequence_size, finish_eucjp_to_iso2022jp + NULL, NULL, NULL, fun_so_iso2022jp_encoder, + finish_iso2022jp_encoder, + iso2022jp_encoder_reset_sequence_size, finish_iso2022jp_encoder }; +static int +fun_so_stateless_iso2022jp_to_eucjp(void *statep, const unsigned char *s, size_t l, unsigned char *o) +{ + o[0] = s[1]; + o[1] = s[2]; + return 2; +} + +static const rb_transcoder +rb_stateless_iso2022jp_to_eucjp = { + "stateless-ISO-2022-JP", "EUC-JP", stateless_iso2022jp_to_eucjp, + TRANSCODE_TABLE_INFO, + 1, /* input_unit_length */ + 3, /* max_input */ + 2, /* max_output */ + stateless_converter, /* stateful_type */ + 0, NULL, NULL, /* state_size, state_init, state_fini */ + NULL, NULL, NULL, fun_so_stateless_iso2022jp_to_eucjp, +}; + +static int +fun_so_eucjp_to_stateless_iso2022jp(void *statep, const unsigned char *s, size_t l, unsigned char *o) +{ + o[0] = EMACS_MULE_LEADING_CODE_JISX0208_1983; + o[1] = s[0]; + o[2] = s[1]; + return 3; +} + +static const rb_transcoder +rb_eucjp_to_stateless_iso2022jp = { + "EUC-JP", "stateless-ISO-2022-JP", eucjp_to_stateless_iso2022jp, + TRANSCODE_TABLE_INFO, + 1, /* input_unit_length */ + 3, /* max_input */ + 3, /* max_output */ + stateless_converter, /* stateful_type */ + 0, NULL, NULL, /* state_size, state_init, state_fini */ + NULL, NULL, NULL, fun_so_eucjp_to_stateless_iso2022jp, +}; + void Init_iso2022(void) { - rb_register_transcoder(&rb_ISO_2022_JP_to_EUC_JP); - rb_register_transcoder(&rb_EUC_JP_to_ISO_2022_JP); + rb_register_transcoder(&rb_iso2022jp_decoder); + rb_register_transcoder(&rb_iso2022jp_encoder); + rb_register_transcoder(&rb_stateless_iso2022jp_to_eucjp); + rb_register_transcoder(&rb_eucjp_to_stateless_iso2022jp); } Property changes on: mvm/enc/trans ___________________________________________________________________ Name: svn:ignore - single_byte.c korean.c utf_16_32.c japanese.c japanese_euc.c japanese_sjis.c iso2022.c newline.c + single_byte.c korean.c utf_16_32.c japanese.c japanese_euc.c japanese_sjis.c iso2022.c newline.c escape.c -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/