author | Ryan C. Gordon <icculus@icculus.org> |
Thu, 14 Aug 2014 21:33:24 -0400 | |
changeset 1351 | ea662d0cbcf0 |
parent 1321 | 6929d3c0a347 |
child 1373 | 527ef3c6a2d6 |
permissions | -rw-r--r-- |
787 | 1 |
#define __PHYSICSFS_INTERNAL__ |
2 |
#include "physfs_internal.h" |
|
3 |
||
4 |
||
5 |
/* |
|
6 |
* From rfc3629, the UTF-8 spec: |
|
7 |
* http://www.ietf.org/rfc/rfc3629.txt |
|
8 |
* |
|
9 |
* Char. number range | UTF-8 octet sequence |
|
10 |
* (hexadecimal) | (binary) |
|
11 |
* --------------------+--------------------------------------------- |
|
12 |
* 0000 0000-0000 007F | 0xxxxxxx |
|
13 |
* 0000 0080-0000 07FF | 110xxxxx 10xxxxxx |
|
14 |
* 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx |
|
15 |
* 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
|
16 |
*/ |
|
17 |
||
18 |
||
19 |
/* |
|
20 |
* This may not be the best value, but it's one that isn't represented |
|
21 |
* in Unicode (0x10FFFF is the largest codepoint value). We return this |
|
22 |
* value from utf8codepoint() if there's bogus bits in the |
|
23 |
* stream. utf8codepoint() will turn this value into something |
|
24 |
* reasonable (like a question mark), for text that wants to try to recover, |
|
25 |
* whereas utf8valid() will use the value to determine if a string has bad |
|
26 |
* bits. |
|
27 |
*/ |
|
28 |
#define UNICODE_BOGUS_CHAR_VALUE 0xFFFFFFFF |
|
29 |
||
30 |
/* |
|
31 |
* This is the codepoint we currently return when there was bogus bits in a |
|
32 |
* UTF-8 string. May not fly in Asian locales? |
|
33 |
*/ |
|
34 |
#define UNICODE_BOGUS_CHAR_CODEPOINT '?' |
|
35 |
||
36 |
static PHYSFS_uint32 utf8codepoint(const char **_str) |
|
37 |
{ |
|
38 |
const char *str = *_str; |
|
39 |
PHYSFS_uint32 retval = 0; |
|
40 |
PHYSFS_uint32 octet = (PHYSFS_uint32) ((PHYSFS_uint8) *str); |
|
41 |
PHYSFS_uint32 octet2, octet3, octet4; |
|
42 |
||
43 |
if (octet == 0) /* null terminator, end of string. */ |
|
44 |
return 0; |
|
45 |
||
46 |
else if (octet < 128) /* one octet char: 0 to 127 */ |
|
47 |
{ |
|
48 |
(*_str)++; /* skip to next possible start of codepoint. */ |
|
1016
957c97389257
Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents:
998
diff
changeset
|
49 |
return octet; |
787 | 50 |
} /* else if */ |
51 |
||
52 |
else if ((octet > 127) && (octet < 192)) /* bad (starts with 10xxxxxx). */ |
|
53 |
{ |
|
54 |
/* |
|
55 |
* Apparently each of these is supposed to be flagged as a bogus |
|
56 |
* char, instead of just resyncing to the next valid codepoint. |
|
57 |
*/ |
|
58 |
(*_str)++; /* skip to next possible start of codepoint. */ |
|
59 |
return UNICODE_BOGUS_CHAR_VALUE; |
|
60 |
} /* else if */ |
|
61 |
||
62 |
else if (octet < 224) /* two octets */ |
|
63 |
{ |
|
1163
e46c11bb2780
Make sure utf8codepoint() advances the pointer even on bogus input.
Ryan C. Gordon <icculus@icculus.org>
parents:
1129
diff
changeset
|
64 |
(*_str)++; /* advance at least one byte in case of an error */ |
787 | 65 |
octet -= (128+64); |
66 |
octet2 = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str)); |
|
67 |
if ((octet2 & (128+64)) != 128) /* Format isn't 10xxxxxx? */ |
|
68 |
return UNICODE_BOGUS_CHAR_VALUE; |
|
69 |
||
1163
e46c11bb2780
Make sure utf8codepoint() advances the pointer even on bogus input.
Ryan C. Gordon <icculus@icculus.org>
parents:
1129
diff
changeset
|
70 |
*_str += 1; /* skip to next possible start of codepoint. */ |
787 | 71 |
retval = ((octet << 6) | (octet2 - 128)); |
72 |
if ((retval >= 0x80) && (retval <= 0x7FF)) |
|
73 |
return retval; |
|
74 |
} /* else if */ |
|
75 |
||
76 |
else if (octet < 240) /* three octets */ |
|
77 |
{ |
|
1163
e46c11bb2780
Make sure utf8codepoint() advances the pointer even on bogus input.
Ryan C. Gordon <icculus@icculus.org>
parents:
1129
diff
changeset
|
78 |
(*_str)++; /* advance at least one byte in case of an error */ |
787 | 79 |
octet -= (128+64+32); |
80 |
octet2 = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str)); |
|
81 |
if ((octet2 & (128+64)) != 128) /* Format isn't 10xxxxxx? */ |
|
82 |
return UNICODE_BOGUS_CHAR_VALUE; |
|
83 |
||
84 |
octet3 = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str)); |
|
85 |
if ((octet3 & (128+64)) != 128) /* Format isn't 10xxxxxx? */ |
|
86 |
return UNICODE_BOGUS_CHAR_VALUE; |
|
87 |
||
1163
e46c11bb2780
Make sure utf8codepoint() advances the pointer even on bogus input.
Ryan C. Gordon <icculus@icculus.org>
parents:
1129
diff
changeset
|
88 |
*_str += 2; /* skip to next possible start of codepoint. */ |
787 | 89 |
retval = ( ((octet << 12)) | ((octet2-128) << 6) | ((octet3-128)) ); |
90 |
||
91 |
/* There are seven "UTF-16 surrogates" that are illegal in UTF-8. */ |
|
92 |
switch (retval) |
|
93 |
{ |
|
94 |
case 0xD800: |
|
95 |
case 0xDB7F: |
|
96 |
case 0xDB80: |
|
97 |
case 0xDBFF: |
|
98 |
case 0xDC00: |
|
99 |
case 0xDF80: |
|
100 |
case 0xDFFF: |
|
101 |
return UNICODE_BOGUS_CHAR_VALUE; |
|
102 |
} /* switch */ |
|
103 |
||
104 |
/* 0xFFFE and 0xFFFF are illegal, too, so we check them at the edge. */ |
|
105 |
if ((retval >= 0x800) && (retval <= 0xFFFD)) |
|
106 |
return retval; |
|
107 |
} /* else if */ |
|
108 |
||
109 |
else if (octet < 248) /* four octets */ |
|
110 |
{ |
|
1163
e46c11bb2780
Make sure utf8codepoint() advances the pointer even on bogus input.
Ryan C. Gordon <icculus@icculus.org>
parents:
1129
diff
changeset
|
111 |
(*_str)++; /* advance at least one byte in case of an error */ |
787 | 112 |
octet -= (128+64+32+16); |
113 |
octet2 = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str)); |
|
114 |
if ((octet2 & (128+64)) != 128) /* Format isn't 10xxxxxx? */ |
|
115 |
return UNICODE_BOGUS_CHAR_VALUE; |
|
116 |
||
117 |
octet3 = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str)); |
|
118 |
if ((octet3 & (128+64)) != 128) /* Format isn't 10xxxxxx? */ |
|
119 |
return UNICODE_BOGUS_CHAR_VALUE; |
|
120 |
||
121 |
octet4 = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str)); |
|
122 |
if ((octet4 & (128+64)) != 128) /* Format isn't 10xxxxxx? */ |
|
123 |
return UNICODE_BOGUS_CHAR_VALUE; |
|
124 |
||
1163
e46c11bb2780
Make sure utf8codepoint() advances the pointer even on bogus input.
Ryan C. Gordon <icculus@icculus.org>
parents:
1129
diff
changeset
|
125 |
*_str += 3; /* skip to next possible start of codepoint. */ |
787 | 126 |
retval = ( ((octet << 18)) | ((octet2 - 128) << 12) | |
127 |
((octet3 - 128) << 6) | ((octet4 - 128)) ); |
|
128 |
if ((retval >= 0x10000) && (retval <= 0x10FFFF)) |
|
129 |
return retval; |
|
130 |
} /* else if */ |
|
131 |
||
132 |
/* |
|
133 |
* Five and six octet sequences became illegal in rfc3629. |
|
134 |
* We throw the codepoint away, but parse them to make sure we move |
|
135 |
* ahead the right number of bytes and don't overflow the buffer. |
|
136 |
*/ |
|
137 |
||
138 |
else if (octet < 252) /* five octets */ |
|
139 |
{ |
|
1163
e46c11bb2780
Make sure utf8codepoint() advances the pointer even on bogus input.
Ryan C. Gordon <icculus@icculus.org>
parents:
1129
diff
changeset
|
140 |
(*_str)++; /* advance at least one byte in case of an error */ |
787 | 141 |
octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str)); |
142 |
if ((octet & (128+64)) != 128) /* Format isn't 10xxxxxx? */ |
|
143 |
return UNICODE_BOGUS_CHAR_VALUE; |
|
144 |
||
145 |
octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str)); |
|
146 |
if ((octet & (128+64)) != 128) /* Format isn't 10xxxxxx? */ |
|
147 |
return UNICODE_BOGUS_CHAR_VALUE; |
|
148 |
||
149 |
octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str)); |
|
150 |
if ((octet & (128+64)) != 128) /* Format isn't 10xxxxxx? */ |
|
151 |
return UNICODE_BOGUS_CHAR_VALUE; |
|
152 |
||
153 |
octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str)); |
|
154 |
if ((octet & (128+64)) != 128) /* Format isn't 10xxxxxx? */ |
|
155 |
return UNICODE_BOGUS_CHAR_VALUE; |
|
156 |
||
1163
e46c11bb2780
Make sure utf8codepoint() advances the pointer even on bogus input.
Ryan C. Gordon <icculus@icculus.org>
parents:
1129
diff
changeset
|
157 |
*_str += 4; /* skip to next possible start of codepoint. */ |
787 | 158 |
return UNICODE_BOGUS_CHAR_VALUE; |
159 |
} /* else if */ |
|
160 |
||
161 |
else /* six octets */ |
|
162 |
{ |
|
1163
e46c11bb2780
Make sure utf8codepoint() advances the pointer even on bogus input.
Ryan C. Gordon <icculus@icculus.org>
parents:
1129
diff
changeset
|
163 |
(*_str)++; /* advance at least one byte in case of an error */ |
787 | 164 |
octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str)); |
165 |
if ((octet & (128+64)) != 128) /* Format isn't 10xxxxxx? */ |
|
166 |
return UNICODE_BOGUS_CHAR_VALUE; |
|
167 |
||
168 |
octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str)); |
|
169 |
if ((octet & (128+64)) != 128) /* Format isn't 10xxxxxx? */ |
|
170 |
return UNICODE_BOGUS_CHAR_VALUE; |
|
171 |
||
172 |
octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str)); |
|
173 |
if ((octet & (128+64)) != 128) /* Format isn't 10xxxxxx? */ |
|
174 |
return UNICODE_BOGUS_CHAR_VALUE; |
|
175 |
||
176 |
octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str)); |
|
177 |
if ((octet & (128+64)) != 128) /* Format isn't 10xxxxxx? */ |
|
178 |
return UNICODE_BOGUS_CHAR_VALUE; |
|
179 |
||
180 |
octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str)); |
|
181 |
if ((octet & (128+64)) != 128) /* Format isn't 10xxxxxx? */ |
|
182 |
return UNICODE_BOGUS_CHAR_VALUE; |
|
183 |
||
184 |
*_str += 6; /* skip to next possible start of codepoint. */ |
|
185 |
return UNICODE_BOGUS_CHAR_VALUE; |
|
186 |
} /* else if */ |
|
187 |
||
188 |
return UNICODE_BOGUS_CHAR_VALUE; |
|
189 |
} /* utf8codepoint */ |
|
190 |
||
828
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
191 |
|
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
192 |
void PHYSFS_utf8ToUcs4(const char *src, PHYSFS_uint32 *dst, PHYSFS_uint64 len) |
787 | 193 |
{ |
194 |
len -= sizeof (PHYSFS_uint32); /* save room for null char. */ |
|
195 |
while (len >= sizeof (PHYSFS_uint32)) |
|
196 |
{ |
|
197 |
PHYSFS_uint32 cp = utf8codepoint(&src); |
|
198 |
if (cp == 0) |
|
199 |
break; |
|
200 |
else if (cp == UNICODE_BOGUS_CHAR_VALUE) |
|
201 |
cp = UNICODE_BOGUS_CHAR_CODEPOINT; |
|
202 |
*(dst++) = cp; |
|
203 |
len -= sizeof (PHYSFS_uint32); |
|
204 |
} /* while */ |
|
205 |
||
206 |
*dst = 0; |
|
828
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
207 |
} /* PHYSFS_utf8ToUcs4 */ |
787 | 208 |
|
828
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
209 |
|
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
210 |
void PHYSFS_utf8ToUcs2(const char *src, PHYSFS_uint16 *dst, PHYSFS_uint64 len) |
787 | 211 |
{ |
212 |
len -= sizeof (PHYSFS_uint16); /* save room for null char. */ |
|
213 |
while (len >= sizeof (PHYSFS_uint16)) |
|
214 |
{ |
|
215 |
PHYSFS_uint32 cp = utf8codepoint(&src); |
|
216 |
if (cp == 0) |
|
217 |
break; |
|
218 |
else if (cp == UNICODE_BOGUS_CHAR_VALUE) |
|
219 |
cp = UNICODE_BOGUS_CHAR_CODEPOINT; |
|
220 |
||
1094
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
221 |
if (cp > 0xFFFF) /* UTF-16 surrogates (bogus chars in UCS-2) */ |
787 | 222 |
cp = UNICODE_BOGUS_CHAR_CODEPOINT; |
223 |
||
224 |
*(dst++) = cp; |
|
225 |
len -= sizeof (PHYSFS_uint16); |
|
226 |
} /* while */ |
|
227 |
||
228 |
*dst = 0; |
|
828
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
229 |
} /* PHYSFS_utf8ToUcs2 */ |
787 | 230 |
|
1094
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
231 |
|
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
232 |
void PHYSFS_utf8ToUtf16(const char *src, PHYSFS_uint16 *dst, PHYSFS_uint64 len) |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
233 |
{ |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
234 |
len -= sizeof (PHYSFS_uint16); /* save room for null char. */ |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
235 |
while (len >= sizeof (PHYSFS_uint16)) |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
236 |
{ |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
237 |
PHYSFS_uint32 cp = utf8codepoint(&src); |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
238 |
if (cp == 0) |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
239 |
break; |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
240 |
else if (cp == UNICODE_BOGUS_CHAR_VALUE) |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
241 |
cp = UNICODE_BOGUS_CHAR_CODEPOINT; |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
242 |
|
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
243 |
if (cp > 0xFFFF) /* encode as surrogate pair */ |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
244 |
{ |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
245 |
if (len < (sizeof (PHYSFS_uint16) * 2)) |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
246 |
break; /* not enough room for the pair, stop now. */ |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
247 |
|
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
248 |
cp -= 0x10000; /* Make this a 20-bit value */ |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
249 |
|
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
250 |
*(dst++) = 0xD800 + ((cp >> 10) & 0x3FF); |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
251 |
len -= sizeof (PHYSFS_uint16); |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
252 |
|
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
253 |
cp = 0xDC00 + (cp & 0x3FF); |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
254 |
} /* if */ |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
255 |
|
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
256 |
*(dst++) = cp; |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
257 |
len -= sizeof (PHYSFS_uint16); |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
258 |
} /* while */ |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
259 |
|
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
260 |
*dst = 0; |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
261 |
} /* PHYSFS_utf8ToUtf16 */ |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
262 |
|
787 | 263 |
static void utf8fromcodepoint(PHYSFS_uint32 cp, char **_dst, PHYSFS_uint64 *_len) |
264 |
{ |
|
265 |
char *dst = *_dst; |
|
266 |
PHYSFS_uint64 len = *_len; |
|
267 |
||
268 |
if (len == 0) |
|
269 |
return; |
|
270 |
||
271 |
if (cp > 0x10FFFF) |
|
272 |
cp = UNICODE_BOGUS_CHAR_CODEPOINT; |
|
273 |
else if ((cp == 0xFFFE) || (cp == 0xFFFF)) /* illegal values. */ |
|
274 |
cp = UNICODE_BOGUS_CHAR_CODEPOINT; |
|
275 |
else |
|
276 |
{ |
|
277 |
/* There are seven "UTF-16 surrogates" that are illegal in UTF-8. */ |
|
278 |
switch (cp) |
|
279 |
{ |
|
280 |
case 0xD800: |
|
281 |
case 0xDB7F: |
|
282 |
case 0xDB80: |
|
283 |
case 0xDBFF: |
|
284 |
case 0xDC00: |
|
285 |
case 0xDF80: |
|
286 |
case 0xDFFF: |
|
287 |
cp = UNICODE_BOGUS_CHAR_CODEPOINT; |
|
288 |
} /* switch */ |
|
289 |
} /* else */ |
|
290 |
||
291 |
/* Do the encoding... */ |
|
292 |
if (cp < 0x80) |
|
293 |
{ |
|
294 |
*(dst++) = (char) cp; |
|
295 |
len--; |
|
296 |
} /* if */ |
|
297 |
||
298 |
else if (cp < 0x800) |
|
299 |
{ |
|
300 |
if (len < 2) |
|
301 |
len = 0; |
|
302 |
else |
|
303 |
{ |
|
304 |
*(dst++) = (char) ((cp >> 6) | 128 | 64); |
|
305 |
*(dst++) = (char) (cp & 0x3F) | 128; |
|
306 |
len -= 2; |
|
307 |
} /* else */ |
|
308 |
} /* else if */ |
|
309 |
||
310 |
else if (cp < 0x10000) |
|
311 |
{ |
|
312 |
if (len < 3) |
|
313 |
len = 0; |
|
314 |
else |
|
315 |
{ |
|
316 |
*(dst++) = (char) ((cp >> 12) | 128 | 64 | 32); |
|
317 |
*(dst++) = (char) ((cp >> 6) & 0x3F) | 128; |
|
318 |
*(dst++) = (char) (cp & 0x3F) | 128; |
|
319 |
len -= 3; |
|
320 |
} /* else */ |
|
321 |
} /* else if */ |
|
322 |
||
323 |
else |
|
324 |
{ |
|
325 |
if (len < 4) |
|
326 |
len = 0; |
|
327 |
else |
|
328 |
{ |
|
329 |
*(dst++) = (char) ((cp >> 18) | 128 | 64 | 32 | 16); |
|
330 |
*(dst++) = (char) ((cp >> 12) & 0x3F) | 128; |
|
331 |
*(dst++) = (char) ((cp >> 6) & 0x3F) | 128; |
|
332 |
*(dst++) = (char) (cp & 0x3F) | 128; |
|
333 |
len -= 4; |
|
334 |
} /* else if */ |
|
335 |
} /* else */ |
|
336 |
||
337 |
*_dst = dst; |
|
338 |
*_len = len; |
|
339 |
} /* utf8fromcodepoint */ |
|
340 |
||
341 |
#define UTF8FROMTYPE(typ, src, dst, len) \ |
|
1092
12c87d886a75
Zero-sized destination buffers when converting to UTF-8 shouldn't overflow.
Ryan C. Gordon <icculus@icculus.org>
parents:
1016
diff
changeset
|
342 |
if (len == 0) return; \ |
787 | 343 |
len--; \ |
344 |
while (len) \ |
|
345 |
{ \ |
|
998
974b90b56c43
Fixed latin1 to UTF-8 conversion (sign conversion was broken).
Ryan C. Gordon <icculus@icculus.org>
parents:
986
diff
changeset
|
346 |
const PHYSFS_uint32 cp = (PHYSFS_uint32) ((typ) (*(src++))); \ |
787 | 347 |
if (cp == 0) break; \ |
348 |
utf8fromcodepoint(cp, &dst, &len); \ |
|
349 |
} \ |
|
350 |
*dst = '\0'; \ |
|
351 |
||
828
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
352 |
void PHYSFS_utf8FromUcs4(const PHYSFS_uint32 *src, char *dst, PHYSFS_uint64 len) |
787 | 353 |
{ |
354 |
UTF8FROMTYPE(PHYSFS_uint32, src, dst, len); |
|
828
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
355 |
} /* PHYSFS_utf8FromUcs4 */ |
787 | 356 |
|
828
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
357 |
void PHYSFS_utf8FromUcs2(const PHYSFS_uint16 *src, char *dst, PHYSFS_uint64 len) |
787 | 358 |
{ |
359 |
UTF8FROMTYPE(PHYSFS_uint64, src, dst, len); |
|
1093
738b2ee912b9
Fixed incorrect comments.
Ryan C. Gordon <icculus@icculus.org>
parents:
1092
diff
changeset
|
360 |
} /* PHYSFS_utf8FromUcs2 */ |
787 | 361 |
|
362 |
/* latin1 maps to unicode codepoints directly, we just utf-8 encode it. */ |
|
828
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
363 |
void PHYSFS_utf8FromLatin1(const char *src, char *dst, PHYSFS_uint64 len) |
787 | 364 |
{ |
365 |
UTF8FROMTYPE(PHYSFS_uint8, src, dst, len); |
|
828
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
366 |
} /* PHYSFS_utf8FromLatin1 */ |
787 | 367 |
|
368 |
#undef UTF8FROMTYPE |
|
369 |
||
828
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
370 |
|
1094
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
371 |
void PHYSFS_utf8FromUtf16(const PHYSFS_uint16 *src, char *dst, PHYSFS_uint64 len) |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
372 |
{ |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
373 |
if (len == 0) |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
374 |
return; |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
375 |
|
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
376 |
len--; |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
377 |
while (len) |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
378 |
{ |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
379 |
PHYSFS_uint32 cp = (PHYSFS_uint32) *(src++); |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
380 |
if (cp == 0) |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
381 |
break; |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
382 |
|
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
383 |
/* Orphaned second half of surrogate pair? */ |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
384 |
if ((cp >= 0xDC00) && (cp <= 0xDFFF)) |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
385 |
cp = UNICODE_BOGUS_CHAR_CODEPOINT; |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
386 |
else if ((cp >= 0xD800) && (cp <= 0xDBFF)) /* start surrogate pair! */ |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
387 |
{ |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
388 |
const PHYSFS_uint32 pair = (PHYSFS_uint32) *src; |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
389 |
if ((pair < 0xDC00) || (pair > 0xDFFF)) |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
390 |
cp = UNICODE_BOGUS_CHAR_CODEPOINT; |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
391 |
else |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
392 |
{ |
1114
db75213cb4b5
Fixed some single-line comments.
Ryan C. Gordon <icculus@icculus.org>
parents:
1094
diff
changeset
|
393 |
src++; /* eat the other surrogate. */ |
1094
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
394 |
cp = (((cp - 0xD800) << 10) | (pair - 0xDC00)); |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
395 |
} /* else */ |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
396 |
} /* else if */ |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
397 |
|
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
398 |
utf8fromcodepoint(cp, &dst, &len); |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
399 |
} /* while */ |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
400 |
|
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
401 |
*dst = '\0'; |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
402 |
} /* PHYSFS_utf8FromUtf16 */ |
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
403 |
|
aa12a958cc47
Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents:
1093
diff
changeset
|
404 |
|
828
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
405 |
typedef struct CaseFoldMapping |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
406 |
{ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
407 |
PHYSFS_uint32 from; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
408 |
PHYSFS_uint32 to0; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
409 |
PHYSFS_uint32 to1; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
410 |
PHYSFS_uint32 to2; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
411 |
} CaseFoldMapping; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
412 |
|
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
413 |
typedef struct CaseFoldHashBucket |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
414 |
{ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
415 |
const PHYSFS_uint8 count; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
416 |
const CaseFoldMapping *list; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
417 |
} CaseFoldHashBucket; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
418 |
|
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
419 |
#include "physfs_casefolding.h" |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
420 |
|
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
421 |
static void locate_case_fold_mapping(const PHYSFS_uint32 from, |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
422 |
PHYSFS_uint32 *to) |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
423 |
{ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
424 |
PHYSFS_uint32 i; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
425 |
const PHYSFS_uint8 hashed = ((from ^ (from >> 8)) & 0xFF); |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
426 |
const CaseFoldHashBucket *bucket = &case_fold_hash[hashed]; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
427 |
const CaseFoldMapping *mapping = bucket->list; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
428 |
|
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
429 |
for (i = 0; i < bucket->count; i++, mapping++) |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
430 |
{ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
431 |
if (mapping->from == from) |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
432 |
{ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
433 |
to[0] = mapping->to0; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
434 |
to[1] = mapping->to1; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
435 |
to[2] = mapping->to2; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
436 |
return; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
437 |
} /* if */ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
438 |
} /* for */ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
439 |
|
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
440 |
/* Not found...there's no remapping for this codepoint. */ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
441 |
to[0] = from; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
442 |
to[1] = 0; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
443 |
to[2] = 0; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
444 |
} /* locate_case_fold_mapping */ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
445 |
|
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
446 |
|
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
447 |
static int utf8codepointcmp(const PHYSFS_uint32 cp1, const PHYSFS_uint32 cp2) |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
448 |
{ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
449 |
PHYSFS_uint32 folded1[3], folded2[3]; |
1351
ea662d0cbcf0
Short circuit case-insensitive compare of two equal Unicode codepoints.
Ryan C. Gordon <icculus@icculus.org>
parents:
1321
diff
changeset
|
450 |
|
ea662d0cbcf0
Short circuit case-insensitive compare of two equal Unicode codepoints.
Ryan C. Gordon <icculus@icculus.org>
parents:
1321
diff
changeset
|
451 |
if (cp1 == cp2) |
ea662d0cbcf0
Short circuit case-insensitive compare of two equal Unicode codepoints.
Ryan C. Gordon <icculus@icculus.org>
parents:
1321
diff
changeset
|
452 |
return 0; /* obviously matches. */ |
ea662d0cbcf0
Short circuit case-insensitive compare of two equal Unicode codepoints.
Ryan C. Gordon <icculus@icculus.org>
parents:
1321
diff
changeset
|
453 |
|
828
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
454 |
locate_case_fold_mapping(cp1, folded1); |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
455 |
locate_case_fold_mapping(cp2, folded2); |
1321
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
456 |
|
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
457 |
if (folded1[0] < folded2[0]) |
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
458 |
return -1; |
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
459 |
else if (folded1[0] > folded2[0]) |
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
460 |
return 1; |
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
461 |
else if (folded1[1] < folded2[1]) |
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
462 |
return -1; |
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
463 |
else if (folded1[1] > folded2[1]) |
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
464 |
return 1; |
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
465 |
else if (folded1[2] < folded2[2]) |
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
466 |
return -1; |
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
467 |
else if (folded1[2] > folded2[2]) |
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
468 |
return 1; |
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
469 |
|
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
470 |
return 0; /* complete match. */ |
828
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
471 |
} /* utf8codepointcmp */ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
472 |
|
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
473 |
|
1255
f15ec4962d08
Renamed __PHYSFS_utf8strcasecmp() to __PHYSFS_utf8stricmp().
Ryan C. Gordon <icculus@icculus.org>
parents:
1163
diff
changeset
|
474 |
int __PHYSFS_utf8stricmp(const char *str1, const char *str2) |
828
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
475 |
{ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
476 |
while (1) |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
477 |
{ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
478 |
const PHYSFS_uint32 cp1 = utf8codepoint(&str1); |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
479 |
const PHYSFS_uint32 cp2 = utf8codepoint(&str2); |
1321
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
480 |
const int rc = utf8codepointcmp(cp1, cp2); |
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
481 |
if (rc != 0) |
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
482 |
return rc; |
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
483 |
else if (cp1 == 0) |
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
484 |
break; /* complete match. */ |
828
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
485 |
} /* while */ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
486 |
|
986
e69d37f80ce2
Fixed some Sun Studio warnings about unreachable code.
Ryan C. Gordon <icculus@icculus.org>
parents:
972
diff
changeset
|
487 |
return 0; |
1255
f15ec4962d08
Renamed __PHYSFS_utf8strcasecmp() to __PHYSFS_utf8stricmp().
Ryan C. Gordon <icculus@icculus.org>
parents:
1163
diff
changeset
|
488 |
} /* __PHYSFS_utf8stricmp */ |
828
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
489 |
|
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
490 |
|
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
491 |
int __PHYSFS_utf8strnicmp(const char *str1, const char *str2, PHYSFS_uint32 n) |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
492 |
{ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
493 |
while (n > 0) |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
494 |
{ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
495 |
const PHYSFS_uint32 cp1 = utf8codepoint(&str1); |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
496 |
const PHYSFS_uint32 cp2 = utf8codepoint(&str2); |
1321
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
497 |
const int rc = utf8codepointcmp(cp1, cp2); |
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
498 |
if (rc != 0) |
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
499 |
return rc; |
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
500 |
else if (cp1 == 0) |
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
501 |
return 0; |
828
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
502 |
n--; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
503 |
} /* while */ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
504 |
|
1321
6929d3c0a347
Make __PHYSFS_utf8stricmp() work like you'd expect.
Ryan C. Gordon <icculus@icculus.org>
parents:
1255
diff
changeset
|
505 |
return 0; /* matched to n chars. */ |
828
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
506 |
} /* __PHYSFS_utf8strnicmp */ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
507 |
|
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
508 |
|
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
509 |
int __PHYSFS_stricmpASCII(const char *str1, const char *str2) |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
510 |
{ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
511 |
while (1) |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
512 |
{ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
513 |
const char ch1 = *(str1++); |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
514 |
const char ch2 = *(str2++); |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
515 |
const char cp1 = ((ch1 >= 'A') && (ch1 <= 'Z')) ? (ch1+32) : ch1; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
516 |
const char cp2 = ((ch2 >= 'A') && (ch2 <= 'Z')) ? (ch2+32) : ch2; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
517 |
if (cp1 < cp2) |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
518 |
return -1; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
519 |
else if (cp1 > cp2) |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
520 |
return 1; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
521 |
else if (cp1 == 0) /* they're both null chars? */ |
986
e69d37f80ce2
Fixed some Sun Studio warnings about unreachable code.
Ryan C. Gordon <icculus@icculus.org>
parents:
972
diff
changeset
|
522 |
break; |
828
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
523 |
} /* while */ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
524 |
|
986
e69d37f80ce2
Fixed some Sun Studio warnings about unreachable code.
Ryan C. Gordon <icculus@icculus.org>
parents:
972
diff
changeset
|
525 |
return 0; |
828
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
526 |
} /* __PHYSFS_stricmpASCII */ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
527 |
|
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
528 |
|
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
529 |
int __PHYSFS_strnicmpASCII(const char *str1, const char *str2, PHYSFS_uint32 n) |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
530 |
{ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
531 |
while (n-- > 0) |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
532 |
{ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
533 |
const char ch1 = *(str1++); |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
534 |
const char ch2 = *(str2++); |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
535 |
const char cp1 = ((ch1 >= 'A') && (ch1 <= 'Z')) ? (ch1+32) : ch1; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
536 |
const char cp2 = ((ch2 >= 'A') && (ch2 <= 'Z')) ? (ch2+32) : ch2; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
537 |
if (cp1 < cp2) |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
538 |
return -1; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
539 |
else if (cp1 > cp2) |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
540 |
return 1; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
541 |
else if (cp1 == 0) /* they're both null chars? */ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
542 |
return 0; |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
543 |
} /* while */ |
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
544 |
|
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
545 |
return 0; |
1093
738b2ee912b9
Fixed incorrect comments.
Ryan C. Gordon <icculus@icculus.org>
parents:
1092
diff
changeset
|
546 |
} /* __PHYSFS_strnicmpASCII */ |
828
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
547 |
|
ee871d51510d
Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents:
808
diff
changeset
|
548 |
|
787 | 549 |
/* end of physfs_unicode.c ... */ |
550 |