Debian Codegraph @

utf8.c

raw symbols SCIP
1 /* Foma: a finite-state toolkit and library. */cxx . . $ `<file>/utf8.c`/
2 /* Copyright © 2008-2021 Mans Hulden */
3
4 /* This file is part of foma. */
5
6 /* Licensed under the Apache License, Version 2.0 (the "License"); */
7 /* you may not use this file except in compliance with the License. */
8 /* You may obtain a copy of the License at */
9
10 /* http://www.apache.org/licenses/LICENSE-2.0 */
11
12 /* Unless required by applicable law or agreed to in writing, software */
13 /* distributed under the License is distributed on an "AS IS" BASIS, */
14 /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
15 /* See the License for the specific language governing permissions and */
16 /* limitations under the License. */
17
18 #include <stdlib.h>
19 #include <string.h>
20 #include "foma.h"cxx . . $ `<file>/foma.h`/
21
22 unsigned char *int2utf8strcxx . . $ int2utf8str(f18dc31a6ecbac56).(int codepoint);
23
24 static int hexstrtointcxx . . $ hexstrtoint(1ef8e6ec083aabb9).(char *str);
25
26 /* Removes trailing character c, as well as spaces and tabs */
27 char *remove_trailingcxx . . $ remove_trailing(dabe84363a524a9e).(char *s, char c) {
28 int i, len;
29 len = strlencxx . . $ strlen(751346f8406fd082).(s)-1;
30 for (i = len; i>=0 ; i--) {
31 if (*(s+i) != c && *(s+i) != ' ' && *(s+i) != '\t') {
32 break;
33 }
34 *(s+i) = '\0';
35 }
36 return(s);
37 }
38
39 /* Remove trailing space and \t */
40 char *trimcxx . . $ trim(7a9722d75b371431).(char *string) {
41 int i;
42 if (string == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
43 return(string);
44 for (i = strlencxx . . $ strlen(751346f8406fd082).(string) - 1; i >=0; i--) {
45 if (*(string+i) != ' ' && *(string+i) != '\t')
46 break;
47 *(string+i) = '\0';
48 }
49 return(string);
50 }
51
52 /* Reverses string in-place */
53 char *xstrrevcxx . . $ xstrrev(7a9722d75b371431).(char *str) {
54 char *p1, *p2;
55 if (! str || ! *str)
56 return str;
57 for (p1 = str, p2 = str + strlencxx . . $ strlen(751346f8406fd082).(str) - 1; p2 > p1; ++p1, --p2) {
58 *p1 ^= *p2;
59 *p2 ^= *p1;
60 *p1 ^= *p2;
61 }
62 return str;
63 }
64
65 char *escape_stringcxx . . $ escape_string(dabe84363a524a9e).(char *string, char chr) {
66 size_tcxx . . $ size_t# i,j;
67 char *newstring;
68 for (i=0,j=0; i < strlencxx . . $ strlen(751346f8406fd082).(string); i++) {
69 if (string[i] == chr) {
70 j++;
71 }
72 }
73 if (j>0) {
74 newstring = calloccxx . . $ calloc(63a7bf2941bfcd7a).((strlencxx . . $ strlen(751346f8406fd082).(string)+j),sizeof(char));
75 for (i=0,j=0; i<strlencxx . . $ strlen(751346f8406fd082).(string); i++, j++) {
76 if (string[i] == chr) {
77 newstring[j++] = '\\';
78 newstring[j] = chr;
79 } else {
80 newstring[j] = string[i];
81 }
82 }
83 return(newstring);
84 } else {
85 return(string);
86 }
87 }
88
89 /* Substitute first \n for \0 */
90 void strip_newlinecxx . . $ strip_newline(fda57c8427c60377).(char *s) {
91 int i, len;
92 len = strlencxx . . $ strlen(751346f8406fd082).(s);
93 /* remove the null terminator */
94 for (i = 0; i < len; i++ ) {
95 if (s[i] == '\n' ) {
96 s[i] = '\0';
97 return;
98 }
99 }
100 }
101 /* Removes initial and final quote, and decodes the string if it contains special chars */
102 void dequote_stringcxx . . $ dequote_string(fda57c8427c60377).(char *s) {
103 int len, i, j;
104 len = strlencxx . . $ strlen(751346f8406fd082).(s);
105 if (*s == 0x22 && *(s+len-1) == 0x22) {
106 for (i = 1, j = 0; i<len-1; i++, j++) {
107 *(s+j) = *(s+i);
108
109 }
110 *(s+j) = '\0';
111 decode_quotedcxx . . $ decode_quoted(fda57c8427c60377).(s);
112 }
113 }
114
115 /* Decode quoted strings. This includes: */
116 /* Changing \uXXXX sequences to their unicode equivalents */
117
118 void decode_quotedcxx . . $ decode_quoted(fda57c8427c60377).(char *s) {
119 int len, i, j, skip;
120 unsigned char *unistr;
121
122 len = strlencxx . . $ strlen(751346f8406fd082).(s);
123 for (i=0, j=0; i < len; ) {
124 if (*(s+i) == 0x5c && len-i > 5 && *(s+i+1) == 0x75 && ishexstrcxx . . $ ishexstr(1ef8e6ec083aabb9).(s+i+2)) {
125 for (unistr=utf8code16tostrcxx . . $ utf8code16tostr(3a30dc52308f2d2).(s+i+2); *unistr; j++, unistr++) {
126 *(s+j) = *unistr;
127 }
128 i += 6;
129 } else {
130 for(skip = utf8skipcxx . . $ utf8skip(1ef8e6ec083aabb9).(s+i)+1; skip > 0; skip--) {
131 *(s+j) = *(s+i);
132 i++; j++;
133 }
134 }
135 }
136 *(s+j) = *(s+i);
137 }
138
139
140 /* Replace equal length substrings in s */
141 char *streqrepcxx . . $ streqrep(a1df38fec57fccd9).(char *s, char *oldstring, char *newstring) {
142 char *ptr;
143 int len;
144 len = strlencxx . . $ strlen(751346f8406fd082).(oldstring);
145
146 while ((ptr = strstrcxx . . $ strstr(a713fe7b9a57cf96).(s, oldstring)) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
147 memcpycxx . . $ memcpy(cc817cb175f282d).(ptr, newstring, len);
148 }
149 return(s);
150 }
151
152 int ishexstrcxx . . $ ishexstr(1ef8e6ec083aabb9). (char *str) {
153 int i;
154 for (i=0; i<4; i++) {
155 if ((*(str+i) > 0x2f && *(str+i) < 0x3a) || (*(str+i) > 0x40 && *(str+i) < 0x47) || (*(str+i) > 0x60 && *(str+i) < 0x67))
156 continue;
157 return 0;
158 }
159 return 1;
160 }
161 int utf8strlencxx . . $ utf8strlen(1ef8e6ec083aabb9).(char *str) {
162 int i,j, len;
163 len = strlencxx . . $ strlen(751346f8406fd082).(str);
164 for (i=0, j=0; *(str+i) != '\0' && i < len;j++ ) {
165 i = i + utf8skipcxx . . $ utf8skip(1ef8e6ec083aabb9).(str+i) + 1;
166 }
167 return j;
168 }
169
170 /* Checks if the next character in the string is a combining character */
171 /* according to Unicode 7.0 */
172 /* i.e. codepoints 0300-036F Combining Diacritical Marks */
173 /* 1AB0-1ABE Combining Diacritical Marks Extended */
174 /* 1DC0-1DFF Combining Diacritical Marks Supplement */
175 /* 20D0-20F0 Combining Diacritical Marks for Symbols */
176 /* FE20-FE2D Combining Half Marks */
177 /* Returns number of bytes of char. representation, or 0 if not combining */
178
179 int utf8iscombiningcxx . . $ utf8iscombining(b2e27cdb9bef2230).(unsigned char *s) {
180 if (*s == '\0' || *(s+1) == '\0')
181 return 0;
182 if (!(*s == 0xcc || *s == 0xcd || *s == 0xe1 || *s == 0xe2 || *s == 0xef))
183 return 0;
184 /* 0300-036F */
185 if (*s == 0xcc && *(s+1) >= 0x80 && *(s+1) <= 0xbf)
186 return 2;
187 if (*s == 0xcd && *(s+1) >= 0x80 && *(s+1) <= 0xaf)
188 return 2;
189 if (*(s+2) == '\0')
190 return 0;
191 /* 1AB0-1ABE */
192 if (*s == 0xe1 && *(s+1) == 0xaa && *(s+2) >= 0xb0 && *(s+2) <= 0xbe)
193 return 3;
194 /* 1DC0-1DFF */
195 if (*s == 0xe1 && *(s+1) == 0xb7 && *(s+2) >= 0x80 && *(s+2) <= 0xbf)
196 return 3;
197 /* 20D0-20F0 */
198 if (*s == 0xe2 && *(s+1) == 0x83 && *(s+2) >= 0x90 && *(s+2) <= 0xb0)
199 return 3;
200 /* FE20-FE2D */
201 if (*s == 0xef && *(s+1) == 0xb8 && *(s+2) >= 0xa0 && *(s+2) <= 0xad)
202 return 3;
203 return 0;
204 }
205
206 int utf8skipcxx . . $ utf8skip(1ef8e6ec083aabb9).(char *str) {
207 unsigned char s;
208
209 s = (unsigned char)(unsigned int) (*str);
210 if (s < 0x80)
211 return 0;
212 if ((s & 0xe0) == 0xc0) {
213 return 1;
214 }
215 if ((s & 0xf0) == 0xe0) {
216 return 2;
217 }
218 if ((s & 0xf8) == 0xf0) {
219 return 3;
220 }
221 return -1;
222 }
223
224 unsigned char *utf8code16tostrcxx . . $ utf8code16tostr(3a30dc52308f2d2).(char *str) {
225 int codepoint;
226 codepoint = (hexstrtointcxx . . $ hexstrtoint(1ef8e6ec083aabb9).(str) << 8) + hexstrtointcxx . . $ hexstrtoint(1ef8e6ec083aabb9).(str+2);
227 return(int2utf8strcxx . . $ int2utf8str(f18dc31a6ecbac56).(codepoint));
228 }
229
230 unsigned char *int2utf8strcxx . . $ int2utf8str(f18dc31a6ecbac56).(int codepoint) {
231 unsigned char *value;
232 value = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(sizeof(unsigned char)*5);
233
234 if (codepoint < 0x80) {
235 *(value) = (unsigned char)(codepoint);
236 *(value+1) = 0;
237 return(value);
238 } else if (codepoint < 0x800) {
239 *(value) = (0xc0 | (unsigned char)(codepoint >> 6));
240 *(value+1) = (0x80 | (unsigned char)(codepoint & 0x3f));
241 *(value+2) = 0;
242 return(value);
243 } else if (codepoint < 0x10000) {
244 *(value) = (0xe0 | (unsigned char)(codepoint >> 12));
245 *(value+1) = (0x80 | (unsigned char)((codepoint >> 6) & 0x3f));
246 *(value+2) = (0x80 | (unsigned char)(codepoint & 0x3f));
247 *(value+3) = 0;
248 return(value);
249 } else {
250 return (0);
251 }
252 }
253
254 int hexstrtointcxx . . $ hexstrtoint(1ef8e6ec083aabb9).(char *str) {
255 int hex;
256
257 if (*str > 0x60) {
258 hex = (*str - 0x57) << 4;
259 } else if (*str > 0x40) {
260 hex = (*str - 0x37) << 4;
261 } else {
262 hex = (*str - 0x30) << 4;
263 }
264 if (*(str+1) > 0x60) {
265 hex += (*(str+1) - 0x57);
266 } else if (*(str+1) > 0x40) {
267 hex += (*(str+1) - 0x37);
268 } else {
269 hex += (*(str+1) - 0x30);
270 }
271 return hex;
272 }