• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 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 
17 namespace base_icu {
18 
19 // source/common/unicode/umachine.h
20 
21 /** The ICU boolean type @stable ICU 2.0 */
22 typedef 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  */
41 typedef 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 /**
64  * \def UPRV_BLOCK_MACRO_BEGIN
65  * Defined as the "do" keyword by default.
66  * @internal
67  */
68 #ifndef CBUPRV_BLOCK_MACRO_BEGIN
69 #define CBUPRV_BLOCK_MACRO_BEGIN do
70 #endif
71 
72 /**
73  * \def UPRV_BLOCK_MACRO_END
74  * Defined as "while (FALSE)" by default.
75  * @internal
76  */
77 #ifndef CBUPRV_BLOCK_MACRO_END
78 #define CBUPRV_BLOCK_MACRO_END while (0)
79 #endif
80 
81 
82 // source/common/unicode/utf.h
83 
84 /**
85  * Is this code point a Unicode noncharacter?
86  * @param c 32-bit code point
87  * @return TRUE or FALSE
88  * @stable ICU 2.4
89  */
90 #define CBU_IS_UNICODE_NONCHAR(c) \
91     ((c)>=0xfdd0 && \
92      ((c)<=0xfdef || ((c)&0xfffe)==0xfffe) && (c)<=0x10ffff)
93 
94 /**
95  * Is c a Unicode code point value (0..U+10ffff)
96  * that can be assigned a character?
97  *
98  * Code points that are not characters include:
99  * - single surrogate code points (U+d800..U+dfff, 2048 code points)
100  * - the last two code points on each plane (U+__fffe and U+__ffff, 34 code points)
101  * - U+fdd0..U+fdef (new with Unicode 3.1, 32 code points)
102  * - the highest Unicode code point value is U+10ffff
103  *
104  * This means that all code points below U+d800 are character code points,
105  * and that boundary is tested first for performance.
106  *
107  * @param c 32-bit code point
108  * @return TRUE or FALSE
109  * @stable ICU 2.4
110  */
111 #define CBU_IS_UNICODE_CHAR(c) \
112     ((uint32_t)(c)<0xd800 || \
113         (0xdfff<(c) && (c)<=0x10ffff && !CBU_IS_UNICODE_NONCHAR(c)))
114 
115 /**
116  * Is this code point a surrogate (U+d800..U+dfff)?
117  * @param c 32-bit code point
118  * @return TRUE or FALSE
119  * @stable ICU 2.4
120  */
121 #define CBU_IS_SURROGATE(c) (((c)&0xfffff800)==0xd800)
122 
123 /**
124  * Assuming c is a surrogate code point (U_IS_SURROGATE(c)),
125  * is it a lead surrogate?
126  * @param c 32-bit code point
127  * @return TRUE or FALSE
128  * @stable ICU 2.4
129  */
130 #define CBU_IS_SURROGATE_LEAD(c) (((c)&0x400)==0)
131 
132 // source/common/unicode/utf8.h
133 
134 /**
135  * Internal bit vector for 3-byte UTF-8 validity check, for use in U8_IS_VALID_LEAD3_AND_T1.
136  * Each bit indicates whether one lead byte + first trail byte pair starts a valid sequence.
137  * Lead byte E0..EF bits 3..0 are used as byte index,
138  * first trail byte bits 7..5 are used as bit index into that byte.
139  * @see U8_IS_VALID_LEAD3_AND_T1
140  * @internal
141  */
142 #define CBU8_LEAD3_T1_BITS "\x20\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x10\x30\x30"
143 
144 /**
145  * Internal 3-byte UTF-8 validity check.
146  * Non-zero if lead byte E0..EF and first trail byte 00..FF start a valid sequence.
147  * @internal
148  */
149 #define CBU8_IS_VALID_LEAD3_AND_T1(lead, t1) (CBU8_LEAD3_T1_BITS[(lead)&0xf]&(1<<((uint8_t)(t1)>>5)))
150 
151 /**
152  * Internal bit vector for 4-byte UTF-8 validity check, for use in U8_IS_VALID_LEAD4_AND_T1.
153  * Each bit indicates whether one lead byte + first trail byte pair starts a valid sequence.
154  * First trail byte bits 7..4 are used as byte index,
155  * lead byte F0..F4 bits 2..0 are used as bit index into that byte.
156  * @see U8_IS_VALID_LEAD4_AND_T1
157  * @internal
158  */
159 #define CBU8_LEAD4_T1_BITS "\x00\x00\x00\x00\x00\x00\x00\x00\x1E\x0F\x0F\x0F\x00\x00\x00\x00"
160 
161 /**
162  * Internal 4-byte UTF-8 validity check.
163  * Non-zero if lead byte F0..F4 and first trail byte 00..FF start a valid sequence.
164  * @internal
165  */
166 #define CBU8_IS_VALID_LEAD4_AND_T1(lead, t1) (CBU8_LEAD4_T1_BITS[(uint8_t)(t1)>>4]&(1<<((lead)&7)))
167 
168 /**
169  * Does this code unit (byte) encode a code point by itself (US-ASCII 0..0x7f)?
170  * @param c 8-bit code unit (byte)
171  * @return TRUE or FALSE
172  * @stable ICU 2.4
173  */
174 #define CBU8_IS_SINGLE(c) (((c)&0x80)==0)
175 
176 /**
177  * Is this code unit (byte) a UTF-8 lead byte? (0xC2..0xF4)
178  * @param c 8-bit code unit (byte)
179  * @return TRUE or FALSE
180  * @stable ICU 2.4
181  */
182 #define CBU8_IS_LEAD(c) ((uint8_t)((c)-0xc2)<=0x32)
183 
184 /**
185  * Is this code unit (byte) a UTF-8 trail byte? (0x80..0xBF)
186  * @param c 8-bit code unit (byte)
187  * @return TRUE or FALSE
188  * @stable ICU 2.4
189  */
190 #define CBU8_IS_TRAIL(c) ((int8_t)(c)<-0x40)
191 
192 /**
193  * How many code units (bytes) are used for the UTF-8 encoding
194  * of this Unicode code point?
195  * @param c 32-bit code point
196  * @return 1..4, or 0 if c is a surrogate or not a Unicode code point
197  * @stable ICU 2.4
198  */
199 #define CBU8_LENGTH(c) \
200     ((uint32_t)(c)<=0x7f ? 1 : \
201         ((uint32_t)(c)<=0x7ff ? 2 : \
202             ((uint32_t)(c)<=0xd7ff ? 3 : \
203                 ((uint32_t)(c)<=0xdfff || (uint32_t)(c)>0x10ffff ? 0 : \
204                     ((uint32_t)(c)<=0xffff ? 3 : 4)\
205                 ) \
206             ) \
207         ) \
208     )
209 
210 /**
211  * The maximum number of UTF-8 code units (bytes) per Unicode code point (U+0000..U+10ffff).
212  * @return 4
213  * @stable ICU 2.4
214  */
215 #define CBU8_MAX_LENGTH 4
216 
217 /**
218  * Get a code point from a string at a code point boundary offset,
219  * and advance the offset to the next code point boundary.
220  * (Post-incrementing forward iteration.)
221  * "Safe" macro, checks for illegal sequences and for string boundaries.
222  *
223  * The length can be negative for a NUL-terminated string.
224  *
225  * The offset may point to the lead byte of a multi-byte sequence,
226  * in which case the macro will read the whole sequence.
227  * If the offset points to a trail byte or an illegal UTF-8 sequence, then
228  * c is set to a negative value.
229  *
230  * @param s const uint8_t * string
231  * @param i int32_t string offset, must be i<length
232  * @param length int32_t string length
233  * @param c output UChar32 variable, set to <0 in case of an error
234  * @see U8_NEXT_UNSAFE
235  * @stable ICU 2.4
236  */
237 #define CBU8_NEXT(s, i, length, c) CBU8_INTERNAL_NEXT_OR_SUB(s, i, length, c, CBU_SENTINEL)
238 
239 /** @internal */
240 #define CBU8_INTERNAL_NEXT_OR_SUB(s, i, length, c, sub) CBUPRV_BLOCK_MACRO_BEGIN { \
241     (c)=(uint8_t)(s)[(i)++]; \
242     if(!CBU8_IS_SINGLE(c)) { \
243         uint8_t __t = 0; \
244         if((i)!=(length) && \
245             /* fetch/validate/assemble all but last trail byte */ \
246             ((c)>=0xe0 ? \
247                 ((c)<0xf0 ?  /* U+0800..U+FFFF except surrogates */ \
248                     CBU8_LEAD3_T1_BITS[(c)&=0xf]&(1<<((__t=(s)[i])>>5)) && \
249                     (__t&=0x3f, 1) \
250                 :  /* U+10000..U+10FFFF */ \
251                     ((c)-=0xf0)<=4 && \
252                     CBU8_LEAD4_T1_BITS[(__t=(s)[i])>>4]&(1<<(c)) && \
253                     ((c)=((c)<<6)|(__t&0x3f), ++(i)!=(length)) && \
254                     (__t=(s)[i]-0x80)<=0x3f) && \
255                 /* valid second-to-last trail byte */ \
256                 ((c)=((c)<<6)|__t, ++(i)!=(length)) \
257             :  /* U+0080..U+07FF */ \
258                 (c)>=0xc2 && ((c)&=0x1f, 1)) && \
259             /* last trail byte */ \
260             (__t=(s)[i]-0x80)<=0x3f && \
261             ((c)=((c)<<6)|__t, ++(i), 1)) { \
262         } else { \
263             (c)=(sub);  /* ill-formed*/ \
264         } \
265     } \
266 } CBUPRV_BLOCK_MACRO_END
267 
268 /**
269  * Append a code point to a string, overwriting 1 to 4 bytes.
270  * The offset points to the current end of the string contents
271  * and is advanced (post-increment).
272  * "Unsafe" macro, assumes a valid code point and sufficient space in the string.
273  * Otherwise, the result is undefined.
274  *
275  * @param s const uint8_t * string buffer
276  * @param i string offset
277  * @param c code point to append
278  * @see U8_APPEND
279  * @stable ICU 2.4
280  */
281 #define CBU8_APPEND_UNSAFE(s, i, c) CBUPRV_BLOCK_MACRO_BEGIN { \
282     uint32_t __uc=(c); \
283     if(__uc<=0x7f) { \
284         (s)[(i)++]=(uint8_t)__uc; \
285     } else { \
286         if(__uc<=0x7ff) { \
287             (s)[(i)++]=(uint8_t)((__uc>>6)|0xc0); \
288         } else { \
289             if(__uc<=0xffff) { \
290                 (s)[(i)++]=(uint8_t)((__uc>>12)|0xe0); \
291             } else { \
292                 (s)[(i)++]=(uint8_t)((__uc>>18)|0xf0); \
293                 (s)[(i)++]=(uint8_t)(((__uc>>12)&0x3f)|0x80); \
294             } \
295             (s)[(i)++]=(uint8_t)(((__uc>>6)&0x3f)|0x80); \
296         } \
297         (s)[(i)++]=(uint8_t)((__uc&0x3f)|0x80); \
298     } \
299 } CBUPRV_BLOCK_MACRO_END
300 
301 // source/common/unicode/utf16.h
302 
303 /**
304  * Does this code unit alone encode a code point (BMP, not a surrogate)?
305  * @param c 16-bit code unit
306  * @return TRUE or FALSE
307  * @stable ICU 2.4
308  */
309 #define CBU16_IS_SINGLE(c) !CBU_IS_SURROGATE(c)
310 
311 /**
312  * Is this code unit a lead surrogate (U+d800..U+dbff)?
313  * @param c 16-bit code unit
314  * @return TRUE or FALSE
315  * @stable ICU 2.4
316  */
317 #define CBU16_IS_LEAD(c) (((c)&0xfffffc00)==0xd800)
318 
319 /**
320  * Is this code unit a trail surrogate (U+dc00..U+dfff)?
321  * @param c 16-bit code unit
322  * @return TRUE or FALSE
323  * @stable ICU 2.4
324  */
325 #define CBU16_IS_TRAIL(c) (((c)&0xfffffc00)==0xdc00)
326 
327 /**
328  * Is this code unit a surrogate (U+d800..U+dfff)?
329  * @param c 16-bit code unit
330  * @return TRUE or FALSE
331  * @stable ICU 2.4
332  */
333 #define CBU16_IS_SURROGATE(c) CBU_IS_SURROGATE(c)
334 
335 /**
336  * Assuming c is a surrogate code point (U16_IS_SURROGATE(c)),
337  * is it a lead surrogate?
338  * @param c 16-bit code unit
339  * @return TRUE or FALSE
340  * @stable ICU 2.4
341  */
342 #define CBU16_IS_SURROGATE_LEAD(c) (((c)&0x400)==0)
343 
344 /**
345  * Helper constant for U16_GET_SUPPLEMENTARY.
346  * @internal
347  */
348 #define CBU16_SURROGATE_OFFSET ((0xd800<<10UL)+0xdc00-0x10000)
349 
350 /**
351  * Get a supplementary code point value (U+10000..U+10ffff)
352  * from its lead and trail surrogates.
353  * The result is undefined if the input values are not
354  * lead and trail surrogates.
355  *
356  * @param lead lead surrogate (U+d800..U+dbff)
357  * @param trail trail surrogate (U+dc00..U+dfff)
358  * @return supplementary code point (U+10000..U+10ffff)
359  * @stable ICU 2.4
360  */
361 #define CBU16_GET_SUPPLEMENTARY(lead, trail) \
362     (((::base_icu::UChar32)(lead)<<10UL)+(::base_icu::UChar32)(trail)-CBU16_SURROGATE_OFFSET)
363 
364 /**
365  * Get the lead surrogate (0xd800..0xdbff) for a
366  * supplementary code point (0x10000..0x10ffff).
367  * @param supplementary 32-bit code point (U+10000..U+10ffff)
368  * @return lead surrogate (U+d800..U+dbff) for supplementary
369  * @stable ICU 2.4
370  */
371 #define CBU16_LEAD(supplementary) (::base_icu::UChar)(((supplementary)>>10)+0xd7c0)
372 
373 /**
374  * Get the trail surrogate (0xdc00..0xdfff) for a
375  * supplementary code point (0x10000..0x10ffff).
376  * @param supplementary 32-bit code point (U+10000..U+10ffff)
377  * @return trail surrogate (U+dc00..U+dfff) for supplementary
378  * @stable ICU 2.4
379  */
380 #define CBU16_TRAIL(supplementary) (::base_icu::UChar)(((supplementary)&0x3ff)|0xdc00)
381 
382 /**
383  * How many 16-bit code units are used to encode this Unicode code point? (1 or 2)
384  * The result is not defined if c is not a Unicode code point (U+0000..U+10ffff).
385  * @param c 32-bit code point
386  * @return 1 or 2
387  * @stable ICU 2.4
388  */
389 #define CBU16_LENGTH(c) ((uint32_t)(c)<=0xffff ? 1 : 2)
390 
391 /**
392  * The maximum number of 16-bit code units per Unicode code point (U+0000..U+10ffff).
393  * @return 2
394  * @stable ICU 2.4
395  */
396 #define CBU16_MAX_LENGTH 2
397 
398 /**
399  * Get a code point from a string at a random-access offset,
400  * without changing the offset.
401  * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
402  *
403  * The offset may point to either the lead or trail surrogate unit
404  * for a supplementary code point, in which case the macro will read
405  * the adjacent matching surrogate as well.
406  *
407  * The length can be negative for a NUL-terminated string.
408  *
409  * If the offset points to a single, unpaired surrogate, then
410  * c is set to that unpaired surrogate.
411  * Iteration through a string is more efficient with U16_NEXT_UNSAFE or U16_NEXT.
412  *
413  * @param s const UChar * string
414  * @param start starting string offset (usually 0)
415  * @param i string offset, must be start<=i<length
416  * @param length string length
417  * @param c output UChar32 variable
418  * @see U16_GET_UNSAFE
419  * @stable ICU 2.4
420  */
421 #define CBU16_GET(s, start, i, length, c) CBUPRV_BLOCK_MACRO_BEGIN { \
422     (c)=(s)[i]; \
423     if(CBU16_IS_SURROGATE(c)) { \
424         uint16_t __c2; \
425         if(CBU16_IS_SURROGATE_LEAD(c)) { \
426             if((i)+1!=(length) && CBU16_IS_TRAIL(__c2=(s)[(i)+1])) { \
427                 (c)=CBU16_GET_SUPPLEMENTARY((c), __c2); \
428             } \
429         } else { \
430             if((i)>(start) && CBU16_IS_LEAD(__c2=(s)[(i)-1])) { \
431                 (c)=CBU16_GET_SUPPLEMENTARY(__c2, (c)); \
432             } \
433         } \
434     } \
435 } CBUPRV_BLOCK_MACRO_END
436 
437 /**
438  * Get a code point from a string at a code point boundary offset,
439  * and advance the offset to the next code point boundary.
440  * (Post-incrementing forward iteration.)
441  * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
442  *
443  * The length can be negative for a NUL-terminated string.
444  *
445  * The offset may point to the lead surrogate unit
446  * for a supplementary code point, in which case the macro will read
447  * the following trail surrogate as well.
448  * If the offset points to a trail surrogate or
449  * to a single, unpaired lead surrogate, then c is set to that unpaired surrogate.
450  *
451  * @param s const UChar * string
452  * @param i string offset, must be i<length
453  * @param length string length
454  * @param c output UChar32 variable
455  * @see U16_NEXT_UNSAFE
456  * @stable ICU 2.4
457  */
458 #define CBU16_NEXT(s, i, length, c) CBUPRV_BLOCK_MACRO_BEGIN { \
459     (c)=(s)[(i)++]; \
460     if(CBU16_IS_LEAD(c)) { \
461         uint16_t __c2; \
462         if((i)!=(length) && CBU16_IS_TRAIL(__c2=(s)[(i)])) { \
463             ++(i); \
464             (c)=CBU16_GET_SUPPLEMENTARY((c), __c2); \
465         } \
466     } \
467 } CBUPRV_BLOCK_MACRO_END
468 
469 /**
470  * Append a code point to a string, overwriting 1 or 2 code units.
471  * The offset points to the current end of the string contents
472  * and is advanced (post-increment).
473  * "Unsafe" macro, assumes a valid code point and sufficient space in the string.
474  * Otherwise, the result is undefined.
475  *
476  * @param s const UChar * string buffer
477  * @param i string offset
478  * @param c code point to append
479  * @see U16_APPEND
480  * @stable ICU 2.4
481  */
482 #define CBU16_APPEND_UNSAFE(s, i, c) CBUPRV_BLOCK_MACRO_BEGIN { \
483     if((uint32_t)(c)<=0xffff) { \
484         (s)[(i)++]=(uint16_t)(c); \
485     } else { \
486         (s)[(i)++]=(uint16_t)(((c)>>10)+0xd7c0); \
487         (s)[(i)++]=(uint16_t)(((c)&0x3ff)|0xdc00); \
488     } \
489 } CBUPRV_BLOCK_MACRO_END
490 
491 /**
492  * Adjust a random-access offset to a code point boundary
493  * at the start of a code point.
494  * If the offset points to the trail surrogate of a surrogate pair,
495  * then the offset is decremented.
496  * Otherwise, it is not modified.
497  * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
498  *
499  * @param s const UChar * string
500  * @param start starting string offset (usually 0)
501  * @param i string offset, must be start<=i
502  * @see U16_SET_CP_START_UNSAFE
503  * @stable ICU 2.4
504  */
505 #define CBU16_SET_CP_START(s, start, i) CBUPRV_BLOCK_MACRO_BEGIN { \
506     if(CBU16_IS_TRAIL((s)[i]) && (i)>(start) && CBU16_IS_LEAD((s)[(i)-1])) { \
507         --(i); \
508     } \
509 } CBUPRV_BLOCK_MACRO_END
510 
511 /**
512  * Move the string offset from one code point boundary to the previous one
513  * and get the code point between them.
514  * (Pre-decrementing backward iteration.)
515  * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
516  *
517  * The input offset may be the same as the string length.
518  * If the offset is behind a trail surrogate unit
519  * for a supplementary code point, then the macro will read
520  * the preceding lead surrogate as well.
521  * If the offset is behind a lead surrogate or behind a single, unpaired
522  * trail surrogate, then c is set to that unpaired surrogate.
523  *
524  * @param s const UChar * string
525  * @param start starting string offset (usually 0)
526  * @param i string offset, must be start<i
527  * @param c output UChar32 variable
528  * @see U16_PREV_UNSAFE
529  * @stable ICU 2.4
530  */
531 #define CBU16_PREV(s, start, i, c) CBUPRV_BLOCK_MACRO_BEGIN { \
532     (c)=(s)[--(i)]; \
533     if(CBU16_IS_TRAIL(c)) { \
534         uint16_t __c2; \
535         if((i)>(start) && CBU16_IS_LEAD(__c2=(s)[(i)-1])) { \
536             --(i); \
537             (c)=CBU16_GET_SUPPLEMENTARY(__c2, (c)); \
538         } \
539     } \
540 } CBUPRV_BLOCK_MACRO_END
541 
542 /**
543  * Adjust a random-access offset to a code point boundary after a code point.
544  * If the offset is behind the lead surrogate of a surrogate pair,
545  * then the offset is incremented.
546  * Otherwise, it is not modified.
547  * The input offset may be the same as the string length.
548  * "Safe" macro, handles unpaired surrogates and checks for string boundaries.
549  *
550  * The length can be negative for a NUL-terminated string.
551  *
552  * @param s const UChar * string
553  * @param start int32_t starting string offset (usually 0)
554  * @param i int32_t string offset, start<=i<=length
555  * @param length int32_t string length
556  * @see U16_SET_CP_LIMIT_UNSAFE
557  * @stable ICU 2.4
558  */
559 #define CBU16_SET_CP_LIMIT(s, start, i, length) CBUPRV_BLOCK_MACRO_BEGIN { \
560     if((start)<(i) && ((i)<(length) || (length)<0) && CBU16_IS_LEAD((s)[(i)-1]) && CBU16_IS_TRAIL((s)[i])) { \
561         ++(i); \
562     } \
563 } CBUPRV_BLOCK_MACRO_END
564 
565 }  // namesapce base_icu
566 
567 #endif  // BASE_THIRD_PARTY_ICU_ICU_UTF_H_
568