mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
f6441bf61c
add state field. (TRANSCODING_STATE): defined. (rb_transcoder): add fields: state_size, state_init_func, state_fini_func. change rb_transcoding* argument to void*. * transcode.c (transcode_restartable0): use TRANSCODING_STATE for first arguments of transcoder functions. (rb_transcoding_open_by_transcoder): initialize state field. (rb_transcoding_close): finalize state field. * tool/transcode-tblgen.rb: provide state size/init/fini. * enc/trans/newline.trans (universal_newline_init): defined. (fun_so_universal_newline): take void* as a state pointer. (rb_universal_newline): provide state size/init/fini. (rb_crlf_newline): ditto. (rb_cr_newline): ditto. * enc/trans/iso2022.trans (iso2022jp_init): defined. (fun_si_iso2022jp_to_eucjp): take void* as a state pointer. (fun_so_iso2022jp_to_eucjp): ditto. (fun_so_eucjp_to_iso2022jp): ditto. (iso2022jp_reset_sequence_size): ditto. (finish_eucjp_to_iso2022jp): ditto. (rb_ISO_2022_JP_to_EUC_JP): provide state size/init/fini. (rb_EUC_JP_to_ISO_2022_JP): ditto. * enc/trans/utf_16_32.trans (fun_so_from_utf_16be): take void* as a state pointer. (fun_so_to_utf_16be): ditto. (fun_so_from_utf_16le): ditto. (fun_so_to_utf_16le): ditto. (fun_so_from_utf_32be): ditto. (fun_so_to_utf_32be): ditto. (fun_so_from_utf_32le): ditto. (fun_so_to_utf_32le): ditto. (rb_from_UTF_16BE): provide state size/init/fini. (rb_to_UTF_16BE): ditto. (rb_from_UTF_16LE): ditto. (rb_to_UTF_16LE): ditto. (rb_from_UTF_32BE): ditto. (rb_to_UTF_32BE): ditto. (rb_from_UTF_32LE): ditto. (rb_to_UTF_32LE): ditto. * enc/trans/japanese.trans (fun_so_eucjp2sjis): take void* as a state pointer. (fun_so_sjis2eucjp): ditto. (rb_eucjp2sjis): provide state size/init/fini. (rb_sjis2eucjp): provide state size/init/fini. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19096 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
173 lines
4.1 KiB
Text
173 lines
4.1 KiB
Text
#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_jisx0208_rest = {}
|
|
map_jisx0208_rest["{21-7e}"] = :func_so
|
|
|
|
transcode_generate_node(ActionMap.parse(map), "iso2022jp_to_eucjp")
|
|
transcode_generate_node(ActionMap.parse(map_jisx0208_rest), "iso2022jp_to_eucjp_jisx0208_rest")
|
|
|
|
map_eucjp = {
|
|
"{0e,0f,1b}" => :undef,
|
|
"{00-0d,10-1a,1c-7f}" => :func_so,
|
|
"{a1-fe}{a1-fe}" => :func_so,
|
|
"8e{a1-fe}" => :undef,
|
|
"8f{a1-fe}{a1-fe}" => :undef,
|
|
}
|
|
|
|
transcode_generate_node(ActionMap.parse(map_eucjp), "eucjp_to_iso2022jp")
|
|
%>
|
|
|
|
<%= transcode_generated_code %>
|
|
|
|
#define G0_ASCII 0
|
|
#define G0_JISX0208 1
|
|
|
|
static int
|
|
iso2022jp_init(void *statep)
|
|
{
|
|
unsigned char *sp = statep;
|
|
*sp = G0_ASCII;
|
|
return 0;
|
|
}
|
|
|
|
static VALUE
|
|
fun_si_iso2022jp_to_eucjp(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;
|
|
else
|
|
return (VALUE)INVALID;
|
|
}
|
|
|
|
static int
|
|
fun_so_iso2022jp_to_eucjp(void *statep, const unsigned char *s, size_t l, unsigned char* o)
|
|
{
|
|
unsigned char *sp = statep;
|
|
if (s[0] == 0x1b) {
|
|
if (s[1] == '(') {
|
|
switch (s[l-1]) {
|
|
case 'B':
|
|
case 'J':
|
|
*sp = G0_ASCII;
|
|
break;
|
|
}
|
|
}
|
|
else {
|
|
switch (s[l-1]) {
|
|
case '@':
|
|
case 'B':
|
|
*sp = G0_JISX0208;
|
|
break;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
else {
|
|
o[0] = s[0] | 0x80;
|
|
o[1] = s[1] | 0x80;
|
|
return 2;
|
|
}
|
|
}
|
|
|
|
static const rb_transcoder
|
|
rb_ISO_2022_JP_to_EUC_JP = {
|
|
"ISO-2022-JP", "EUC-JP", iso2022jp_to_eucjp,
|
|
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
|
|
};
|
|
|
|
static int
|
|
fun_so_eucjp_to_iso2022jp(void *statep, const unsigned char *s, size_t l, unsigned char *o)
|
|
{
|
|
unsigned char *sp = statep;
|
|
unsigned char *output0 = o;
|
|
|
|
if (*sp != (l == 1 ? G0_ASCII : G0_JISX0208)) {
|
|
if (l == 1) {
|
|
*o++ = 0x1b;
|
|
*o++ = '(';
|
|
*o++ = 'B';
|
|
*sp = G0_ASCII;
|
|
}
|
|
else {
|
|
*o++ = 0x1b;
|
|
*o++ = '$';
|
|
*o++ = 'B';
|
|
*sp = G0_JISX0208; /* JIS X 0208 1983 */
|
|
}
|
|
}
|
|
|
|
if (l == 1) {
|
|
*o++ = s[0] & 0x7f;
|
|
}
|
|
else {
|
|
*o++ = s[0] & 0x7f;
|
|
*o++ = s[1] & 0x7f;
|
|
}
|
|
|
|
return o - output0;
|
|
}
|
|
|
|
static int
|
|
iso2022jp_reset_sequence_size(void *statep)
|
|
{
|
|
unsigned char *sp = statep;
|
|
if (*sp == G0_JISX0208)
|
|
return 3;
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
finish_eucjp_to_iso2022jp(void *statep, unsigned char *o)
|
|
{
|
|
unsigned char *sp = statep;
|
|
unsigned char *output0 = o;
|
|
|
|
if (*sp == G0_ASCII)
|
|
return 0;
|
|
|
|
*o++ = 0x1b;
|
|
*o++ = '(';
|
|
*o++ = 'B';
|
|
*sp = G0_ASCII;
|
|
|
|
return o - output0;
|
|
}
|
|
|
|
static const rb_transcoder
|
|
rb_EUC_JP_to_ISO_2022_JP = {
|
|
"EUC-JP", "ISO-2022-JP", eucjp_to_iso2022jp,
|
|
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
|
|
};
|
|
|
|
void
|
|
Init_iso2022(void)
|
|
{
|
|
rb_register_transcoder(&rb_ISO_2022_JP_to_EUC_JP);
|
|
rb_register_transcoder(&rb_EUC_JP_to_ISO_2022_JP);
|
|
}
|
|
|