blob: b626b398404bb8d16bd94cf127c36a957f5586a8 [file] [log] [blame]
Scott Graham66962112018-06-08 12:42:08 -07001// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
3/*
4******************************************************************************
5*
6* Copyright (C) 1999-2015, International Business Machines
7* Corporation and others. All Rights Reserved.
8*
9******************************************************************************
10*/
11
12#ifndef BASE_THIRD_PARTY_ICU_ICU_UTF_H_
13#define BASE_THIRD_PARTY_ICU_ICU_UTF_H_
14
15#include <stdint.h>
16
17namespace base_icu {
18
19// source/common/unicode/umachine.h
20
21/** The ICU boolean type @stable ICU 2.0 */
22typedef int8_t UBool;
23
24/**
25 * Define UChar32 as a type for single Unicode code points.
26 * UChar32 is a signed 32-bit integer (same as int32_t).
27 *
28 * The Unicode code point range is 0..0x10ffff.
29 * All other values (negative or >=0x110000) are illegal as Unicode code points.
30 * They may be used as sentinel values to indicate "done", "error"
31 * or similar non-code point conditions.
32 *
33 * Before ICU 2.4 (Jitterbug 2146), UChar32 was defined
34 * to be wchar_t if that is 32 bits wide (wchar_t may be signed or unsigned)
35 * or else to be uint32_t.
36 * That is, the definition of UChar32 was platform-dependent.
37 *
38 * @see U_SENTINEL
39 * @stable ICU 2.4
40 */
41typedef int32_t UChar32;
42
43/**
44 * This value is intended for sentinel values for APIs that
45 * (take or) return single code points (UChar32).
46 * It is outside of the Unicode code point range 0..0x10ffff.
47 *
48 * For example, a "done" or "error" value in a new API
49 * could be indicated with U_SENTINEL.
50 *
51 * ICU APIs designed before ICU 2.4 usually define service-specific "done"
52 * values, mostly 0xffff.
53 * Those may need to be distinguished from
54 * actual U+ffff text contents by calling functions like
55 * CharacterIterator::hasNext() or UnicodeString::length().
56 *
57 * @return -1
58 * @see UChar32
59 * @stable ICU 2.4
60 */
61#define CBU_SENTINEL (-1)
62
63// source/common/unicode/utf.h
64
65/**
66 * Is this code point a Unicode noncharacter?
67 * @param c 32-bit code point
68 * @return TRUE or FALSE
69 * @stable ICU 2.4
70 */
Scott Graham98cd3ca2018-06-14 22:26:55 -070071#define CBU_IS_UNICODE_NONCHAR(c) \
72 ((c) >= 0xfdd0 && ((c) <= 0xfdef || ((c)&0xfffe) == 0xfffe) && \
73 (c) <= 0x10ffff)
Scott Graham66962112018-06-08 12:42:08 -070074
75/**
76 * Is c a Unicode code point value (0..U+10ffff)
77 * that can be assigned a character?
78 *
79 * Code points that are not characters include:
80 * - single surrogate code points (U+d800..U+dfff, 2048 code points)
Scott Graham98cd3ca2018-06-14 22:26:55 -070081 * - the last two code points on each plane (U+__fffe and U+__ffff, 34 code
82 * points) - U+fdd0..U+fdef (new with Unicode 3.1, 32 code points) - the highest
83 * Unicode code point value is U+10ffff
Scott Graham66962112018-06-08 12:42:08 -070084 *
85 * This means that all code points below U+d800 are character code points,
86 * and that boundary is tested first for performance.
87 *
88 * @param c 32-bit code point
89 * @return TRUE or FALSE
90 * @stable ICU 2.4
91 */
92#define CBU_IS_UNICODE_CHAR(c) \
Scott Graham98cd3ca2018-06-14 22:26:55 -070093 ((uint32_t)(c) < 0xd800 || \
94 (0xdfff < (c) && (c) <= 0x10ffff && !CBU_IS_UNICODE_NONCHAR(c)))
Scott Graham66962112018-06-08 12:42:08 -070095
96/**
97 * Is this code point a surrogate (U+d800..U+dfff)?
98 * @param c 32-bit code point
99 * @return TRUE or FALSE
100 * @stable ICU 2.4
101 */
Scott Graham98cd3ca2018-06-14 22:26:55 -0700102#define CBU_IS_SURROGATE(c) (((c)&0xfffff800) == 0xd800)
Scott Graham66962112018-06-08 12:42:08 -0700103
104/**
105 * Assuming c is a surrogate code point (U_IS_SURROGATE(c)),
106 * is it a lead surrogate?
107 * @param c 32-bit code point
108 * @return TRUE or FALSE
109 * @stable ICU 2.4
110 */
Scott Graham98cd3ca2018-06-14 22:26:55 -0700111#define CBU_IS_SURROGATE_LEAD(c) (((c)&0x400) == 0)
Scott Graham66962112018-06-08 12:42:08 -0700112
113// source/common/unicode/utf8.h
114
115/**
Scott Graham98cd3ca2018-06-14 22:26:55 -0700116 * Internal bit vector for 3-byte UTF-8 validity check, for use in
117 * U8_IS_VALID_LEAD3_AND_T1. Each bit indicates whether one lead byte + first
118 * trail byte pair starts a valid sequence. Lead byte E0..EF bits 3..0 are used
119 * as byte index, first trail byte bits 7..5 are used as bit index into that
120 * byte.
Scott Graham66962112018-06-08 12:42:08 -0700121 * @see U8_IS_VALID_LEAD3_AND_T1
122 * @internal
123 */
Scott Graham98cd3ca2018-06-14 22:26:55 -0700124#define CBU8_LEAD3_T1_BITS \
125 "\x20\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x10\x30\x30"
Scott Graham66962112018-06-08 12:42:08 -0700126
127/**
128 * Internal 3-byte UTF-8 validity check.
Scott Graham98cd3ca2018-06-14 22:26:55 -0700129 * Non-zero if lead byte E0..EF and first trail byte 00..FF start a valid
130 * sequence.
Scott Graham66962112018-06-08 12:42:08 -0700131 * @internal
132 */
Scott Graham98cd3ca2018-06-14 22:26:55 -0700133#define CBU8_IS_VALID_LEAD3_AND_T1(lead, t1) \
134 (CBU8_LEAD3_T1_BITS[(lead)&0xf] & (1 << ((uint8_t)(t1) >> 5)))
Scott Graham66962112018-06-08 12:42:08 -0700135
136/**
Scott Graham98cd3ca2018-06-14 22:26:55 -0700137 * Internal bit vector for 4-byte UTF-8 validity check, for use in
138 * U8_IS_VALID_LEAD4_AND_T1. Each bit indicates whether one lead byte + first
139 * trail byte pair starts a valid sequence. First trail byte bits 7..4 are used
140 * as byte index, lead byte F0..F4 bits 2..0 are used as bit index into that
141 * byte.
Scott Graham66962112018-06-08 12:42:08 -0700142 * @see U8_IS_VALID_LEAD4_AND_T1
143 * @internal
144 */
Scott Graham98cd3ca2018-06-14 22:26:55 -0700145#define CBU8_LEAD4_T1_BITS \
146 "\x00\x00\x00\x00\x00\x00\x00\x00\x1E\x0F\x0F\x0F\x00\x00\x00\x00"
Scott Graham66962112018-06-08 12:42:08 -0700147
148/**
149 * Internal 4-byte UTF-8 validity check.
Scott Graham98cd3ca2018-06-14 22:26:55 -0700150 * Non-zero if lead byte F0..F4 and first trail byte 00..FF start a valid
151 * sequence.
Scott Graham66962112018-06-08 12:42:08 -0700152 * @internal
153 */
Scott Graham98cd3ca2018-06-14 22:26:55 -0700154#define CBU8_IS_VALID_LEAD4_AND_T1(lead, t1) \
155 (CBU8_LEAD4_T1_BITS[(uint8_t)(t1) >> 4] & (1 << ((lead)&7)))
Scott Graham66962112018-06-08 12:42:08 -0700156
157/**
158 * Function for handling "next code point" with error-checking.
159 *
160 * This is internal since it is not meant to be called directly by external clie
161nts;
162 * however it is U_STABLE (not U_INTERNAL) since it is called by public macros i
163n this
164 * file and thus must remain stable, and should not be hidden when other interna
165l
166 * functions are hidden (otherwise public macros would fail to compile).
167 * @internal
168 */
Scott Graham98cd3ca2018-06-14 22:26:55 -0700169UChar32 utf8_nextCharSafeBody(const uint8_t* s,
170 int32_t* pi,
171 int32_t length,
172 ::base_icu::UChar32 c,
173 ::base_icu::UBool strict);
Scott Graham66962112018-06-08 12:42:08 -0700174
175/**
176 * Does this code unit (byte) encode a code point by itself (US-ASCII 0..0x7f)?
177 * @param c 8-bit code unit (byte)
178 * @return TRUE or FALSE
179 * @stable ICU 2.4
180 */
Scott Graham98cd3ca2018-06-14 22:26:55 -0700181#define CBU8_IS_SINGLE(c) (((c)&0x80) == 0)
Scott Graham66962112018-06-08 12:42:08 -0700182
183/**
184 * Is this code unit (byte) a UTF-8 lead byte? (0xC2..0xF4)
185 * @param c 8-bit code unit (byte)
186 * @return TRUE or FALSE
187 * @stable ICU 2.4
188 */
Scott Graham98cd3ca2018-06-14 22:26:55 -0700189#define CBU8_IS_LEAD(c) ((uint8_t)((c)-0xc2) <= 0x32)
Scott Graham66962112018-06-08 12:42:08 -0700190
191/**
192 * Is this code unit (byte) a UTF-8 trail byte? (0x80..0xBF)
193 * @param c 8-bit code unit (byte)
194 * @return TRUE or FALSE
195 * @stable ICU 2.4
196 */
Scott Graham98cd3ca2018-06-14 22:26:55 -0700197#define CBU8_IS_TRAIL(c) ((int8_t)(c) < -0x40)
Scott Graham66962112018-06-08 12:42:08 -0700198
199/**
200 * How many code units (bytes) are used for the UTF-8 encoding
201 * of this Unicode code point?
202 * @param c 32-bit code point
203 * @return 1..4, or 0 if c is a surrogate or not a Unicode code point
204 * @stable ICU 2.4
205 */
Scott Graham98cd3ca2018-06-14 22:26:55 -0700206#define CBU8_LENGTH(c) \
207 ((uint32_t)(c) <= 0x7f \
208 ? 1 \
209 : ((uint32_t)(c) <= 0x7ff \
210 ? 2 \
211 : ((uint32_t)(c) <= 0xd7ff \
212 ? 3 \
213 : ((uint32_t)(c) <= 0xdfff || (uint32_t)(c) > 0x10ffff \
214 ? 0 \
215 : ((uint32_t)(c) <= 0xffff ? 3 : 4)))))
Scott Graham66962112018-06-08 12:42:08 -0700216
217/**
Scott Graham98cd3ca2018-06-14 22:26:55 -0700218 * The maximum number of UTF-8 code units (bytes) per Unicode code point
219 * (U+0000..U+10ffff).
Scott Graham66962112018-06-08 12:42:08 -0700220 * @return 4
221 * @stable ICU 2.4
222 */
223#define CBU8_MAX_LENGTH 4
224
225/**
226 * Get a code point from a string at a code point boundary offset,
227 * and advance the offset to the next code point boundary.
228 * (Post-incrementing forward iteration.)
229 * "Safe" macro, checks for illegal sequences and for string boundaries.
230 *
231 * The length can be negative for a NUL-terminated string.
232 *
233 * The offset may point to the lead byte of a multi-byte sequence,
234 * in which case the macro will read the whole sequence.
235 * If the offset points to a trail byte or an illegal UTF-8 sequence, then
236 * c is set to a negative value.
237 *
238 * @param s const uint8_t * string
239 * @param i int32_t string offset, must be i<length
240 * @param length int32_t string length
241 * @param c output UChar32 variable, set to <0 in case of an error
242 * @see U8_NEXT_UNSAFE
243 * @stable ICU 2.4
244 */
Scott Graham98cd3ca2018-06-14 22:26:55 -0700245#define CBU8_NEXT(s, i, length, c) \
246 { \
247 (c) = (uint8_t)(s)[(i)++]; \
248 if (!CBU8_IS_SINGLE(c)) { \
249 uint8_t __t1, __t2; \
250 if (/* handle U+0800..U+FFFF inline */ \
251 (0xe0 <= (c) && (c) < 0xf0) && \
252 (((i) + 1) < (length) || (length) < 0) && \
253 CBU8_IS_VALID_LEAD3_AND_T1((c), __t1 = (s)[i]) && \
254 (__t2 = (s)[(i) + 1] - 0x80) <= 0x3f) { \
255 (c) = (((c)&0xf) << 12) | ((__t1 & 0x3f) << 6) | __t2; \
256 (i) += 2; \
257 } else if (/* handle U+0080..U+07FF inline */ \
258 ((c) < 0xe0 && (c) >= 0xc2) && ((i) != (length)) && \
259 (__t1 = (s)[i] - 0x80) <= 0x3f) { \
260 (c) = (((c)&0x1f) << 6) | __t1; \
261 ++(i); \
262 } else { \
263 /* function call for "complicated" and error cases */ \
264 (c) = ::base_icu::utf8_nextCharSafeBody((const uint8_t*)s, &(i), \
265 (length), c, -1); \
266 } \
267 } \
268 }
Scott Graham66962112018-06-08 12:42:08 -0700269
270/**
271 * Append a code point to a string, overwriting 1 to 4 bytes.
272 * The offset points to the current end of the string contents
273 * and is advanced (post-increment).
Scott Graham98cd3ca2018-06-14 22:26:55 -0700274 * "Unsafe" macro, assumes a valid code point and sufficient space in the
275 * string. Otherwise, the result is undefined.
Scott Graham66962112018-06-08 12:42:08 -0700276 *
277 * @param s const uint8_t * string buffer
278 * @param i string offset
279 * @param c code point to append
280 * @see U8_APPEND
281 * @stable ICU 2.4
282 */
Scott Graham98cd3ca2018-06-14 22:26:55 -0700283#define CBU8_APPEND_UNSAFE(s, i, c) \
284 { \
285 if ((uint32_t)(c) <= 0x7f) { \
286 (s)[(i)++] = (uint8_t)(c); \
287 } else { \
288 if ((uint32_t)(c) <= 0x7ff) { \
289 (s)[(i)++] = (uint8_t)(((c) >> 6) | 0xc0); \
290 } else { \
291 if ((uint32_t)(c) <= 0xffff) { \
292 (s)[(i)++] = (uint8_t)(((c) >> 12) | 0xe0); \
293 } else { \
294 (s)[(i)++] = (uint8_t)(((c) >> 18) | 0xf0); \
295 (s)[(i)++] = (uint8_t)((((c) >> 12) & 0x3f) | 0x80); \
296 } \
297 (s)[(i)++] = (uint8_t)((((c) >> 6) & 0x3f) | 0x80); \
298 } \
299 (s)[(i)++] = (uint8_t)(((c)&0x3f) | 0x80); \
300 } \
301 }
Scott Graham66962112018-06-08 12:42:08 -0700302
303// source/common/unicode/utf16.h
304
305/**
306 * Does this code unit alone encode a code point (BMP, not a surrogate)?
307 * @param c 16-bit code unit
308 * @return TRUE or FALSE
309 * @stable ICU 2.4
310 */
311#define CBU16_IS_SINGLE(c) !CBU_IS_SURROGATE(c)
312
313/**
314 * Is this code unit a lead surrogate (U+d800..U+dbff)?
315 * @param c 16-bit code unit
316 * @return TRUE or FALSE
317 * @stable ICU 2.4
318 */
Scott Graham98cd3ca2018-06-14 22:26:55 -0700319#define CBU16_IS_LEAD(c) (((c)&0xfffffc00) == 0xd800)
Scott Graham66962112018-06-08 12:42:08 -0700320
321/**
322 * Is this code unit a trail surrogate (U+dc00..U+dfff)?
323 * @param c 16-bit code unit
324 * @return TRUE or FALSE
325 * @stable ICU 2.4
326 */
Scott Graham98cd3ca2018-06-14 22:26:55 -0700327#define CBU16_IS_TRAIL(c) (((c)&0xfffffc00) == 0xdc00)
Scott Graham66962112018-06-08 12:42:08 -0700328
329/**
330 * Is this code unit a surrogate (U+d800..U+dfff)?
331 * @param c 16-bit code unit
332 * @return TRUE or FALSE
333 * @stable ICU 2.4
334 */
335#define CBU16_IS_SURROGATE(c) CBU_IS_SURROGATE(c)
336
337/**
338 * Assuming c is a surrogate code point (U16_IS_SURROGATE(c)),
339 * is it a lead surrogate?
340 * @param c 16-bit code unit
341 * @return TRUE or FALSE
342 * @stable ICU 2.4
343 */
Scott Graham98cd3ca2018-06-14 22:26:55 -0700344#define CBU16_IS_SURROGATE_LEAD(c) (((c)&0x400) == 0)
Scott Graham66962112018-06-08 12:42:08 -0700345
346/**
347 * Helper constant for U16_GET_SUPPLEMENTARY.
348 * @internal
349 */
Scott Graham98cd3ca2018-06-14 22:26:55 -0700350#define CBU16_SURROGATE_OFFSET ((0xd800 << 10UL) + 0xdc00 - 0x10000)
Scott Graham66962112018-06-08 12:42:08 -0700351
352/**
353 * Get a supplementary code point value (U+10000..U+10ffff)
354 * from its lead and trail surrogates.
355 * The result is undefined if the input values are not
356 * lead and trail surrogates.
357 *
358 * @param lead lead surrogate (U+d800..U+dbff)
359 * @param trail trail surrogate (U+dc00..U+dfff)
360 * @return supplementary code point (U+10000..U+10ffff)
361 * @stable ICU 2.4
362 */
363#define CBU16_GET_SUPPLEMENTARY(lead, trail) \
Scott Graham98cd3ca2018-06-14 22:26:55 -0700364 (((::base_icu::UChar32)(lead) << 10UL) + \
365 (::base_icu::UChar32)(trail)-CBU16_SURROGATE_OFFSET)
Scott Graham66962112018-06-08 12:42:08 -0700366
367/**
368 * Get the lead surrogate (0xd800..0xdbff) for a
369 * supplementary code point (0x10000..0x10ffff).
370 * @param supplementary 32-bit code point (U+10000..U+10ffff)
371 * @return lead surrogate (U+d800..U+dbff) for supplementary
372 * @stable ICU 2.4
373 */
Scott Graham98cd3ca2018-06-14 22:26:55 -0700374#define CBU16_LEAD(supplementary) \
375 (::base_icu::UChar)(((supplementary) >> 10) + 0xd7c0)
Scott Graham66962112018-06-08 12:42:08 -0700376
377/**
378 * Get the trail surrogate (0xdc00..0xdfff) for a
379 * supplementary code point (0x10000..0x10ffff).
380 * @param supplementary 32-bit code point (U+10000..U+10ffff)
381 * @return trail surrogate (U+dc00..U+dfff) for supplementary
382 * @stable ICU 2.4
383 */
Scott Graham98cd3ca2018-06-14 22:26:55 -0700384#define CBU16_TRAIL(supplementary) \
385 (::base_icu::UChar)(((supplementary)&0x3ff) | 0xdc00)
Scott Graham66962112018-06-08 12:42:08 -0700386
387/**
Scott Graham98cd3ca2018-06-14 22:26:55 -0700388 * How many 16-bit code units are used to encode this Unicode code point? (1 or
389 * 2) The result is not defined if c is not a Unicode code point
390 * (U+0000..U+10ffff).
Scott Graham66962112018-06-08 12:42:08 -0700391 * @param c 32-bit code point
392 * @return 1 or 2
393 * @stable ICU 2.4
394 */
Scott Graham98cd3ca2018-06-14 22:26:55 -0700395#define CBU16_LENGTH(c) ((uint32_t)(c) <= 0xffff ? 1 : 2)
Scott Graham66962112018-06-08 12:42:08 -0700396
397/**
Scott Graham98cd3ca2018-06-14 22:26:55 -0700398 * The maximum number of 16-bit code units per Unicode code point
399 * (U+0000..U+10ffff).
Scott Graham66962112018-06-08 12:42:08 -0700400 * @return 2
401 * @stable ICU 2.4
402 */
403#define CBU16_MAX_LENGTH 2
404
405/**
406 * Get a code point from a string at a code point boundary offset,
407 * and advance the offset to the next code point boundary.
408 * (Post-incrementing forward iteration.)
409 * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
410 *
411 * The length can be negative for a NUL-terminated string.
412 *
413 * The offset may point to the lead surrogate unit
414 * for a supplementary code point, in which case the macro will read
415 * the following trail surrogate as well.
416 * If the offset points to a trail surrogate or
Scott Graham98cd3ca2018-06-14 22:26:55 -0700417 * to a single, unpaired lead surrogate, then c is set to that unpaired
418 * surrogate.
Scott Graham66962112018-06-08 12:42:08 -0700419 *
420 * @param s const UChar * string
421 * @param i string offset, must be i<length
422 * @param length string length
423 * @param c output UChar32 variable
424 * @see U16_NEXT_UNSAFE
425 * @stable ICU 2.4
426 */
Scott Graham98cd3ca2018-06-14 22:26:55 -0700427#define CBU16_NEXT(s, i, length, c) \
428 { \
429 (c) = (s)[(i)++]; \
430 if (CBU16_IS_LEAD(c)) { \
431 uint16_t __c2; \
432 if ((i) != (length) && CBU16_IS_TRAIL(__c2 = (s)[(i)])) { \
433 ++(i); \
434 (c) = CBU16_GET_SUPPLEMENTARY((c), __c2); \
435 } \
436 } \
437 }
Scott Graham66962112018-06-08 12:42:08 -0700438
439/**
440 * Append a code point to a string, overwriting 1 or 2 code units.
441 * The offset points to the current end of the string contents
442 * and is advanced (post-increment).
Scott Graham98cd3ca2018-06-14 22:26:55 -0700443 * "Unsafe" macro, assumes a valid code point and sufficient space in the
444 * string. Otherwise, the result is undefined.
Scott Graham66962112018-06-08 12:42:08 -0700445 *
446 * @param s const UChar * string buffer
447 * @param i string offset
448 * @param c code point to append
449 * @see U16_APPEND
450 * @stable ICU 2.4
451 */
Scott Graham98cd3ca2018-06-14 22:26:55 -0700452#define CBU16_APPEND_UNSAFE(s, i, c) \
453 { \
454 if ((uint32_t)(c) <= 0xffff) { \
455 (s)[(i)++] = (uint16_t)(c); \
456 } else { \
457 (s)[(i)++] = (uint16_t)(((c) >> 10) + 0xd7c0); \
458 (s)[(i)++] = (uint16_t)(((c)&0x3ff) | 0xdc00); \
459 } \
460 }
Scott Graham66962112018-06-08 12:42:08 -0700461
Scott Graham98cd3ca2018-06-14 22:26:55 -0700462} // namespace base_icu
Scott Graham66962112018-06-08 12:42:08 -0700463
464#endif // BASE_THIRD_PARTY_ICU_ICU_UTF_H_