]> granicus.if.org Git - onig/blob - src/utf32_be.c
add encoding argument into str_dup()
[onig] / src / utf32_be.c
1 /**********************************************************************
2   utf32_be.c -  Oniguruma (regular expression library)
3 **********************************************************************/
4 /*-
5  * Copyright (c) 2002-2016  K.Kosako  <sndgk393 AT ybb DOT ne DOT jp>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29
30 #include "regenc.h"
31
32 static int
33 utf32be_mbc_enc_len(const UChar* p ARG_UNUSED)
34 {
35   return 4;
36 }
37
38 static int
39 is_valid_mbc_string(const UChar* s, const UChar* end)
40 {
41   return onigenc_length_check_is_valid_mbc_string(ONIG_ENCODING_UTF32_BE, s, end);
42 }
43
44 static int
45 utf32be_is_mbc_newline(const UChar* p, const UChar* end)
46 {
47   if (p + 3 < end) {
48     if (*(p+3) == 0x0a && *(p+2) == 0 && *(p+1) == 0 && *p == 0)
49       return 1;
50 #ifdef USE_UNICODE_ALL_LINE_TERMINATORS
51     if ((
52 #ifndef USE_CRNL_AS_LINE_TERMINATOR
53          *(p+3) == 0x0d ||
54 #endif
55          *(p+3) == 0x85)
56         && *(p+2) == 0 && *(p+1) == 0 && *p == 0x00)
57       return 1;
58
59     if (*(p+2) == 0x20 && (*(p+3) == 0x29 || *(p+3) == 0x28)
60         && *(p+1) == 0 && *p == 0)
61       return 1;
62 #endif
63   }
64   return 0;
65 }
66
67 static OnigCodePoint
68 utf32be_mbc_to_code(const UChar* p, const UChar* end ARG_UNUSED)
69 {
70   return (OnigCodePoint )(((p[0] * 256 + p[1]) * 256 + p[2]) * 256 + p[3]);
71 }
72
73 static int
74 utf32be_code_to_mbclen(OnigCodePoint code ARG_UNUSED)
75 {
76   return 4;
77 }
78
79 static int
80 utf32be_code_to_mbc(OnigCodePoint code, UChar *buf)
81 {
82   UChar* p = buf;
83
84   *p++ = (UChar )((code & 0xff000000) >>24);
85   *p++ = (UChar )((code & 0xff0000)   >>16);
86   *p++ = (UChar )((code & 0xff00)     >> 8);
87   *p++ = (UChar ) (code & 0xff);
88   return 4;
89 }
90
91 static int
92 utf32be_mbc_case_fold(OnigCaseFoldType flag,
93                       const UChar** pp, const UChar* end, UChar* fold)
94 {
95   const UChar* p = *pp;
96
97   if (ONIGENC_IS_ASCII_CODE(*(p+3)) && *(p+2) == 0 && *(p+1) == 0 && *p == 0) {
98     *fold++ = 0;
99     *fold++ = 0;
100
101 #ifdef USE_UNICODE_CASE_FOLD_TURKISH_AZERI
102     if ((flag & ONIGENC_CASE_FOLD_TURKISH_AZERI) != 0) {
103       if (*(p+3) == 0x49) {
104         *fold++ = 0x01;
105         *fold   = 0x31;
106         (*pp) += 4;
107         return 4;
108       }
109     }
110 #endif
111
112     *fold++ = 0;
113     *fold   = ONIGENC_ASCII_CODE_TO_LOWER_CASE(*(p+3));
114     *pp += 4;
115     return 4;
116   }
117   else
118     return onigenc_unicode_mbc_case_fold(ONIG_ENCODING_UTF32_BE, flag, pp, end,
119                                          fold);
120 }
121
122 #if 0
123 static int
124 utf32be_is_mbc_ambiguous(OnigCaseFoldType flag, const UChar** pp, const UChar* end)
125 {
126   const UChar* p = *pp;
127
128   (*pp) += 4;
129
130   if (*(p+2) == 0 && *(p+1) == 0 && *p == 0) {
131     int c, v;
132
133     p += 3;
134     if (*p == 0xdf && (flag & INTERNAL_ONIGENC_CASE_FOLD_MULTI_CHAR) != 0) {
135       return TRUE;
136     }
137
138     c = *p;
139     v = ONIGENC_IS_UNICODE_ISO_8859_1_BIT_CTYPE(c,
140                        (BIT_CTYPE_UPPER | BIT_CTYPE_LOWER));
141     if ((v | BIT_CTYPE_LOWER) != 0) {
142       /* 0xaa, 0xb5, 0xba are lower case letter, but can't convert. */
143       if (c >= 0xaa && c <= 0xba)
144         return FALSE;
145       else
146         return TRUE;
147     }
148     return (v != 0 ? TRUE : FALSE);
149   }
150
151   return FALSE;
152 }
153 #endif
154
155 static UChar*
156 utf32be_left_adjust_char_head(const UChar* start, const UChar* s)
157 {
158   int rem;
159
160   if (s <= start) return (UChar* )s;
161
162   rem = (s - start) % 4;
163   return (UChar* )(s - rem);
164 }
165
166 static int
167 utf32be_get_case_fold_codes_by_str(OnigCaseFoldType flag,
168     const OnigUChar* p, const OnigUChar* end, OnigCaseFoldCodeItem items[])
169 {
170   return onigenc_unicode_get_case_fold_codes_by_str(ONIG_ENCODING_UTF32_BE,
171                                                     flag, p, end, items);
172 }
173
174 OnigEncodingType OnigEncodingUTF32_BE = {
175   utf32be_mbc_enc_len,
176   "UTF-32BE",   /* name */
177   4,            /* max byte length */
178   4,            /* min byte length */
179   utf32be_is_mbc_newline,
180   utf32be_mbc_to_code,
181   utf32be_code_to_mbclen,
182   utf32be_code_to_mbc,
183   utf32be_mbc_case_fold,
184   onigenc_unicode_apply_all_case_fold,
185   utf32be_get_case_fold_codes_by_str,
186   onigenc_unicode_property_name_to_ctype,
187   onigenc_unicode_is_code_ctype,
188   onigenc_utf16_32_get_ctype_code_range,
189   utf32be_left_adjust_char_head,
190   onigenc_always_false_is_allowed_reverse_match,
191   NULL, /* init */
192   NULL, /* is_initialized */
193   is_valid_mbc_string
194 };