]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/utf_impl.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
6 * Copyright (C) 1999-2012, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 ******************************************************************************
10 * file name: utf_impl.c
12 * tab size: 8 (not used)
15 * created on: 1999sep13
16 * created by: Markus W. Scherer
18 * This file provides implementation functions for macros in the utfXX.h
19 * that would otherwise be too long as macros.
22 /* set import/export definitions */
27 #include "unicode/utypes.h"
28 #include "unicode/utf.h"
29 #include "unicode/utf8.h"
30 #include "unicode/utf_old.h"
34 * Table of the number of utf8 trail bytes, indexed by the lead byte.
35 * Used by the deprecated macro UTF8_COUNT_TRAIL_BYTES, defined in utf_old.h
37 * The current macro, U8_COUNT_TRAIL_BYTES, does _not_ use this table.
39 * Note that this table cannot be removed, even if UTF8_COUNT_TRAIL_BYTES were
40 * changed to no longer use it. References to the table from expansions of UTF8_COUNT_TRAIL_BYTES
41 * may exist in old client code that must continue to run with newer icu library versions.
43 * This table could be replaced on many machines by
44 * a few lines of assembler code using an
45 * "index of first 0-bit from msb" instruction and
46 * one or two more integer instructions.
48 * For example, on an i386, do something like
50 * - NOT AL (8-bit, leave b15..b8==0..0, reverse only b7..b0)
52 * - BSR BX, AX (16-bit)
53 * - MOV AX, 6 (result)
54 * - JZ finish (ZF==1 if leadByte==0xff)
55 * - SUB AX, BX (result)
57 * (BSR: Bit Scan Reverse, scans for a 1-bit, starting from the MSB)
59 * In Unicode, all UTF-8 byte sequences with more than 4 bytes are illegal;
60 * lead bytes above 0xf4 are illegal.
61 * We keep them in this table for skipping long ISO 10646-UTF-8 sequences.
63 extern "C" U_EXPORT
const uint8_t
64 utf8_countTrailBytes
[256]={
65 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
66 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
67 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
68 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
72 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
73 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
76 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
78 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
80 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
81 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
83 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
85 3, 3, 3, /* illegal in Unicode */
86 4, 4, 4, 4, /* illegal in Unicode */
87 5, 5, /* illegal in Unicode */
88 0, 0 /* illegal bytes 0xfe and 0xff */
92 utf8_minLegal
[4]={ 0, 0x80, 0x800, 0x10000 };
96 UTF8_ERROR_VALUE_1
, UTF8_ERROR_VALUE_2
, UTF_ERROR_VALUE
, 0x10ffff,
101 errorValue(int32_t count
, int8_t strict
) {
103 return utf8_errorValue
[count
];
104 } else if(strict
==-3) {
112 * Handle the non-inline part of the U8_NEXT() and U8_NEXT_FFFD() macros
113 * and their obsolete sibling UTF8_NEXT_CHAR_SAFE().
115 * U8_NEXT() supports NUL-terminated strings indicated via length<0.
117 * The "strict" parameter controls the error behavior:
118 * <0 "Safe" behavior of U8_NEXT():
119 * -1: All illegal byte sequences yield U_SENTINEL=-1.
120 * -2: Same as -1, except for lenient treatment of surrogate code points as legal.
121 * Some implementations use this for roundtripping of
122 * Unicode 16-bit strings that are not well-formed UTF-16, that is, they
123 * contain unpaired surrogates.
124 * -3: All illegal byte sequences yield U+FFFD.
125 * 0 Obsolete "safe" behavior of UTF8_NEXT_CHAR_SAFE(..., FALSE):
126 * All illegal byte sequences yield a positive code point such that this
127 * result code point would be encoded with the same number of bytes as
128 * the illegal sequence.
129 * >0 Obsolete "strict" behavior of UTF8_NEXT_CHAR_SAFE(..., TRUE):
130 * Same as the obsolete "safe" behavior, but non-characters are also treated
131 * like illegal sequences.
133 * Note that a UBool is the same as an int8_t.
135 U_CAPI UChar32 U_EXPORT2
136 utf8_nextCharSafeBody(const uint8_t *s
, int32_t *pi
, int32_t length
, UChar32 c
, UBool strict
) {
138 uint8_t count
=U8_COUNT_TRAIL_BYTES(c
);
139 U_ASSERT(count
<= 5); /* U8_COUNT_TRAIL_BYTES returns value 0...5 */
140 if(i
+count
<=length
|| length
<0) {
143 U8_MASK_LEAD_BYTE(c
, count
);
144 /* support NUL-terminated strings: do not read beyond the first non-trail byte */
146 /* each branch falls through to the next one */
148 /* count==0 for illegally leading trail bytes and the illegal bytes 0xfe and 0xff */
151 /* count>=4 is always illegal: no more than 3 trail bytes in Unicode's UTF-8 */
156 /* c>=0x110 would result in code point>0x10ffff, outside Unicode */
157 if(c
>=0x110 || trail
>0x3f) { break; }
163 * test for a surrogate d800..dfff unless we are lenient:
164 * before the last (c<<6), a surrogate is c=360..37f
166 if(((c
&0xffe0)==0x360 && strict
!=-2) || trail
>0x3f) { break; }
171 if(trail
>0x3f) { break; }
172 /* correct sequence - all trail bytes have (b7..b6)==(10) */
173 if(c
>=utf8_minLegal
[count
] &&
174 /* strict: forbid non-characters like U+fffe */
175 (strict
<=0 || !U_IS_UNICODE_NONCHAR(c
))) {
179 /* no default branch to optimize switch() - all values are covered */
182 /* too few bytes left */
188 while(count
>0 && U8_IS_TRAIL(s
[i
])) {
192 c
=errorValue(i
-*pi
, strict
);
197 U_CAPI
int32_t U_EXPORT2
198 utf8_appendCharSafeBody(uint8_t *s
, int32_t i
, int32_t length
, UChar32 c
, UBool
*pIsError
) {
199 if((uint32_t)(c
)<=0x7ff) {
201 (s
)[(i
)++]=(uint8_t)(((c
)>>6)|0xc0);
202 (s
)[(i
)++]=(uint8_t)(((c
)&0x3f)|0x80);
205 } else if((uint32_t)(c
)<=0xffff) {
206 /* Starting with Unicode 3.2, surrogate code points must not be encoded in UTF-8. */
207 if((i
)+2<(length
) && !U_IS_SURROGATE(c
)) {
208 (s
)[(i
)++]=(uint8_t)(((c
)>>12)|0xe0);
209 (s
)[(i
)++]=(uint8_t)((((c
)>>6)&0x3f)|0x80);
210 (s
)[(i
)++]=(uint8_t)(((c
)&0x3f)|0x80);
213 } else if((uint32_t)(c
)<=0x10ffff) {
215 (s
)[(i
)++]=(uint8_t)(((c
)>>18)|0xf0);
216 (s
)[(i
)++]=(uint8_t)((((c
)>>12)&0x3f)|0x80);
217 (s
)[(i
)++]=(uint8_t)((((c
)>>6)&0x3f)|0x80);
218 (s
)[(i
)++]=(uint8_t)(((c
)&0x3f)|0x80);
222 /* c>0x10ffff or not enough space, write an error value */
234 c
=utf8_errorValue
[length
-1];
235 UTF8_APPEND_CHAR_UNSAFE(s
, offset
, c
);
242 U_CAPI UChar32 U_EXPORT2
243 utf8_prevCharSafeBody(const uint8_t *s
, int32_t start
, int32_t *pi
, UChar32 c
, UBool strict
) {
245 uint8_t b
, count
=1, shift
=6;
247 if(!U8_IS_TRAIL(c
)) { return errorValue(0, strict
); }
249 /* extract value bits from the last trail byte */
254 /* no lead byte at all */
255 return errorValue(0, strict
);
258 /* read another previous byte */
260 if((uint8_t)(b
-0x80)<0x7e) { /* 0x80<=b<0xfe */
262 /* lead byte, this will always end the loop */
263 uint8_t shouldCount
=U8_COUNT_TRAIL_BYTES(b
);
265 if(count
==shouldCount
) {
266 /* set the new position */
268 U8_MASK_LEAD_BYTE(b
, count
);
269 c
|=(UChar32
)b
<<shift
;
270 if(count
>=4 || c
>0x10ffff || c
<utf8_minLegal
[count
] || (U_IS_SURROGATE(c
) && strict
!=-2) || (strict
>0 && U_IS_UNICODE_NONCHAR(c
))) {
271 /* illegal sequence or (strict and non-character) */
275 c
=errorValue(count
, strict
);
277 /* exit with correct c */
280 /* the lead byte does not match the number of trail bytes */
281 /* only set the position to the lead byte if it would
282 include the trail byte that we started with */
283 if(count
<shouldCount
) {
285 c
=errorValue(count
, strict
);
287 c
=errorValue(0, strict
);
293 c
|=(UChar32
)(b
&0x3f)<<shift
;
297 /* more than 5 trail bytes is illegal */
298 c
=errorValue(0, strict
);
302 /* single-byte character precedes trailing bytes */
303 c
=errorValue(0, strict
);
310 U_CAPI
int32_t U_EXPORT2
311 utf8_back1SafeBody(const uint8_t *s
, int32_t start
, int32_t i
) {
312 /* i had been decremented once before the function call */
316 /* read at most the 6 bytes s[Z] to s[i], inclusively */
323 /* return I if the sequence starting there is long enough to include i */
326 if((uint8_t)(b
-0x80)>=0x7e) { /* not 0x80<=b<0xfe */
329 if(U8_COUNT_TRAIL_BYTES(b
)>=(i
-I
)) {
337 /* return i itself to be consistent with the FWD_1 macro */