1 /**********************************************************************
2 cp949.c - Oniguruma (regular expression library)
3 **********************************************************************/
5 * Copyright (c) 2002-2007 K.Kosako <sndgk393 AT ybb DOT ne DOT jp>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const int EncLen_CP949
[] = {
33 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
34 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
35 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
36 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
37 1, 1, 1, 1, 1, 1, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
61 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
62 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
63 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
64 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
65 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
66 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
67 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0
70 #define CP949_ISMB_FIRST(byte) (EncLen_CP949[byte] > 1)
71 #define CP949_ISMB_TRAIL(byte) CP949_CAN_BE_TRAIL_TABLE[(byte)]
73 typedef enum { FAILURE
= -2, ACCEPT
= -1, S0
= 0, S1
} state_t
;
76 static const signed char trans
[][0x100] = {
77 { /* S0 0 1 2 3 4 5 6 7 8 9 a b c d e f */
78 /* 0 */ A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
,
79 /* 1 */ A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
,
80 /* 2 */ A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
,
81 /* 3 */ A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
,
82 /* 4 */ A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
,
83 /* 5 */ A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
,
84 /* 6 */ A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
,
85 /* 7 */ A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
,
86 /* 8 */ A
, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
87 /* 9 */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
88 /* a */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
89 /* b */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
90 /* c */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
91 /* d */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
92 /* e */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
93 /* f */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, F
95 { /* S1 0 1 2 3 4 5 6 7 8 9 a b c d e f */
96 /* 0 */ F
, F
, F
, F
, F
, F
, F
, F
, F
, F
, F
, F
, F
, F
, F
, F
,
97 /* 1 */ F
, F
, F
, F
, F
, F
, F
, F
, F
, F
, F
, F
, F
, F
, F
, F
,
98 /* 2 */ F
, F
, F
, F
, F
, F
, F
, F
, F
, F
, F
, F
, F
, F
, F
, F
,
99 /* 3 */ F
, F
, F
, F
, F
, F
, F
, F
, F
, F
, F
, F
, F
, F
, F
, F
,
100 /* 4 */ F
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
,
101 /* 5 */ A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, F
, F
, F
, F
, F
,
102 /* 6 */ F
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
,
103 /* 7 */ A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, F
, F
, F
, F
, F
,
104 /* 8 */ F
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
,
105 /* 9 */ A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
,
106 /* a */ A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
,
107 /* b */ A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
,
108 /* c */ A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
,
109 /* d */ A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
,
110 /* e */ A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
,
111 /* f */ A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, A
, F
118 cp949_mbc_enc_len(const UChar
* p
, const UChar
* e
, OnigEncoding enc ARG_UNUSED
)
120 int firstbyte
= *p
++;
121 state_t s
= trans
[0][firstbyte
];
123 return s == ACCEPT ? ONIGENC_CONSTRUCT_MBCLEN_CHARFOUND(n) : \
124 ONIGENC_CONSTRUCT_MBCLEN_INVALID()
125 if (s
< 0) RETURN(1);
126 if (p
== e
) return ONIGENC_CONSTRUCT_MBCLEN_NEEDMORE(EncLen_CP949
[firstbyte
]-1);
133 cp949_mbc_to_code(const UChar
* p
, const UChar
* end
, OnigEncoding enc
)
135 return onigenc_mbn_mbc_to_code(enc
, p
, end
);
139 cp949_code_to_mbc(OnigCodePoint code
, UChar
*buf
, OnigEncoding enc
)
141 return onigenc_mb2_code_to_mbc(enc
, code
, buf
);
145 cp949_mbc_case_fold(OnigCaseFoldType flag
, const UChar
** pp
, const UChar
* end
,
146 UChar
* lower
, OnigEncoding enc
)
148 return onigenc_mbn_mbc_case_fold(enc
, flag
,
154 cp949_is_mbc_ambiguous(OnigCaseFoldType flag
,
155 const UChar
** pp
, const UChar
* end
, OnigEncoding enc
)
157 return onigenc_mbn_is_mbc_ambiguous(enc
, flag
, pp
, end
);
162 cp949_is_code_ctype(OnigCodePoint code
, unsigned int ctype
, OnigEncoding enc
)
164 return onigenc_mb2_is_code_ctype(enc
, code
, ctype
);
168 cp949_left_adjust_char_head(const UChar
* start
, const UChar
* s
, const UChar
* end
, OnigEncoding enc
)
173 if (s
<= start
) return (UChar
* )s
;
176 if (CP949_ISMB_TRAIL(*p
)) {
178 if (! CP949_ISMB_FIRST(*--p
)) {
184 len
= enclen(enc
, p
, end
);
185 if (p
+ len
> s
) return (UChar
* )p
;
187 return (UChar
* )(p
+ ((s
- p
) & ~1));
191 cp949_is_allowed_reverse_match(const UChar
* s
, const UChar
* end ARG_UNUSED
, OnigEncoding enc ARG_UNUSED
)
194 return (CP949_ISMB_TRAIL(c
) ? FALSE
: TRUE
);
197 OnigEncodingDefine(cp949
, CP949
) = {
200 2, /* max enc length */
201 1, /* min enc length */
202 onigenc_is_mbc_newline_0x0a
,
204 onigenc_mb2_code_to_mbclen
,
207 onigenc_ascii_apply_all_case_fold
,
208 onigenc_ascii_get_case_fold_codes_by_str
,
209 onigenc_minimum_property_name_to_ctype
,
211 onigenc_not_support_get_ctype_code_range
,
212 cp949_left_adjust_char_head
,
213 cp949_is_allowed_reverse_match
,
214 onigenc_ascii_only_case_map
,
220 * Link: http://www.microsoft.com/globaldev/reference/dbcs/949.mspx
221 * Link: https://en.wikipedia.org/wiki/EUC-KR#EUC-KR