| 1 |
|
| #ifndef SCMUNI_H |
| 2 |
|
| #define SCMUNI_H |
| 3 |
|
| |
| 4 |
|
| #define VALID_UTF_MAX (0x10FFFF) |
| 5 |
|
| #define Is_VALID_UTF(uv) ((uv) <= VALID_UTF_MAX) |
| 6 |
|
| |
| 7 |
|
| #define UTF16_IS_SURROG(uv) (0xD800 <= (uv) && (uv) <= 0xDFFF) |
| 8 |
|
| #define UTF16_HI_SURROG(uv) (0xD800 <= (uv) && (uv) <= 0xDBFF) |
| 9 |
|
| #define UTF16_LO_SURROG(uv) (0xDC00 <= (uv) && (uv) <= 0xDFFF) |
| 10 |
|
| |
| 11 |
|
| #define UTF8A_SKIP(uv) \ |
| 12 |
|
| ( (uv) < 0x80 ? 1 : \ |
| 13 |
|
| (uv) < 0x800 ? 2 : \ |
| 14 |
|
| (uv) < 0x10000 ? 3 : \ |
| 15 |
|
| (uv) < 0x200000 ? 4 : \ |
| 16 |
|
| (uv) < 0x4000000 ? 5 : \ |
| 17 |
|
| (uv) < 0x80000000 ? 6 : 7 ) |
| 18 |
|
| |
| 19 |
|
| #define UTF8A_TRAIL(c) (((c) & 0xC0) == 0x80) |
| 20 |
|
| |
| 21 |
|
| static UV |
| 22 |
16531 |
| ord_in_utf16le(U8 *s, STRLEN curlen, STRLEN *retlen) |
| 23 |
|
| { |
| 24 |
|
| UV uv, luv; |
| 25 |
16531 |
| U8 *p = s; |
| 26 |
|
| |
| 27 |
16531 |
▶ | if (curlen < 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (curlen < 2) { |
5 |
16526 |
16531 |
|
| 28 |
5 |
▶ | if (retlen) |
Branch: 1 branch
| Branch | True | False | Total |
| if (retlen) |
5 |
0 |
5 |
|
| 29 |
5 |
| *retlen = 0; |
| 30 |
5 |
| return 0; |
| 31 |
|
| } |
| 32 |
|
| |
| 33 |
16526 |
| uv = (UV)((p[1] << 8) | p[0]); |
| 34 |
16526 |
| p += 2; |
| 35 |
|
| |
| 36 |
16526 |
▶ | if (UTF16_HI_SURROG(uv) && (4 <= curlen)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (UTF16_HI_SURROG(uv) && (4 <= curlen)) { |
2067 |
14459 |
16526 |
| if (UTF16_HI_SURROG(uv) && (4 <= curlen)) { |
1012 |
1055 |
2067 |
| if (UTF16_HI_SURROG(uv) && (4 <= curlen)) { |
1012 |
0 |
1012 |
|
| 37 |
1012 |
| luv = (UV)((p[1] << 8) | p[0]); |
| 38 |
|
| |
| 39 |
1012 |
▶ | if (UTF16_LO_SURROG(luv)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (UTF16_LO_SURROG(luv)) { |
1009 |
3 |
1012 |
| if (UTF16_LO_SURROG(luv)) { |
1009 |
0 |
1009 |
|
| 40 |
1009 |
| uv = 0x10000 + ((uv-0xD800) * 0x400) + (luv-0xDC00); |
| 41 |
1009 |
| p += 2; |
| 42 |
|
| } |
| 43 |
|
| } |
| 44 |
|
| |
| 45 |
16526 |
▶ | if (retlen) |
Branch: 1 branch
| Branch | True | False | Total |
| if (retlen) |
16526 |
0 |
16526 |
|
| 46 |
16526 |
| *retlen = p - s; |
| 47 |
16526 |
| return uv; |
| 48 |
|
| } |
| 49 |
|
| |
| 50 |
|
| |
| 51 |
|
| static UV |
| 52 |
16667 |
| ord_in_utf16be(U8 *s, STRLEN curlen, STRLEN *retlen) |
| 53 |
|
| { |
| 54 |
|
| UV uv, luv; |
| 55 |
16667 |
| U8 *p = s; |
| 56 |
|
| |
| 57 |
16667 |
▶ | if (curlen < 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (curlen < 2) { |
5 |
16662 |
16667 |
|
| 58 |
5 |
▶ | if (retlen) |
Branch: 1 branch
| Branch | True | False | Total |
| if (retlen) |
5 |
0 |
5 |
|
| 59 |
5 |
| *retlen = 0; |
| 60 |
5 |
| return 0; |
| 61 |
|
| } |
| 62 |
|
| |
| 63 |
16662 |
| uv = (UV)((p[0] << 8) | p[1]); |
| 64 |
16662 |
| p += 2; |
| 65 |
|
| |
| 66 |
16662 |
▶ | if (UTF16_HI_SURROG(uv) && (4 <= curlen)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (UTF16_HI_SURROG(uv) && (4 <= curlen)) { |
2094 |
14568 |
16662 |
| if (UTF16_HI_SURROG(uv) && (4 <= curlen)) { |
1012 |
1082 |
2094 |
| if (UTF16_HI_SURROG(uv) && (4 <= curlen)) { |
1012 |
0 |
1012 |
|
| 67 |
1012 |
| luv = (UV)((p[0] << 8) | p[1]); |
| 68 |
|
| |
| 69 |
1012 |
▶ | if (UTF16_LO_SURROG(luv)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (UTF16_LO_SURROG(luv)) { |
1009 |
3 |
1012 |
| if (UTF16_LO_SURROG(luv)) { |
1009 |
0 |
1009 |
|
| 70 |
1009 |
| uv = 0x10000 + ((uv-0xD800) * 0x400) + (luv-0xDC00); |
| 71 |
1009 |
| p += 2; |
| 72 |
|
| } |
| 73 |
|
| } |
| 74 |
|
| |
| 75 |
16662 |
▶ | if (retlen) |
Branch: 1 branch
| Branch | True | False | Total |
| if (retlen) |
16662 |
0 |
16662 |
|
| 76 |
16662 |
| *retlen = p - s; |
| 77 |
16662 |
| return uv; |
| 78 |
|
| } |
| 79 |
|
| |
| 80 |
|
| |
| 81 |
|
| static UV |
| 82 |
20644 |
| ord_in_utf32le(U8 *s, STRLEN curlen, STRLEN *retlen) |
| 83 |
|
| { |
| 84 |
20644 |
▶ | if (curlen < 4) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (curlen < 4) { |
17 |
20627 |
20644 |
|
| 85 |
17 |
▶ | if (retlen) |
Branch: 1 branch
| Branch | True | False | Total |
| if (retlen) |
17 |
0 |
17 |
|
| 86 |
17 |
| *retlen = 0; |
| 87 |
17 |
| return 0; |
| 88 |
|
| } |
| 89 |
|
| |
| 90 |
20627 |
▶ | if (retlen) |
Branch: 1 branch
| Branch | True | False | Total |
| if (retlen) |
20627 |
0 |
20627 |
|
| 91 |
20627 |
| *retlen = 4; |
| 92 |
20627 |
| return (UV)((s[3] << 24) | (s[2] << 16) | (s[1] << 8) | s[0]); |
| 93 |
|
| } |
| 94 |
|
| |
| 95 |
|
| |
| 96 |
|
| static UV |
| 97 |
20788 |
| ord_in_utf32be(U8 *s, STRLEN curlen, STRLEN *retlen) |
| 98 |
|
| { |
| 99 |
20788 |
▶ | if (curlen < 4) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (curlen < 4) { |
17 |
20771 |
20788 |
|
| 100 |
17 |
▶ | if (retlen) |
Branch: 1 branch
| Branch | True | False | Total |
| if (retlen) |
17 |
0 |
17 |
|
| 101 |
17 |
| *retlen = 0; |
| 102 |
17 |
| return 0; |
| 103 |
|
| } |
| 104 |
|
| |
| 105 |
20771 |
▶ | if (retlen) |
Branch: 1 branch
| Branch | True | False | Total |
| if (retlen) |
20771 |
0 |
20771 |
|
| 106 |
20771 |
| *retlen = 4; |
| 107 |
20771 |
| return (UV)((s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3]); |
| 108 |
|
| } |
| 109 |
|
| |
| 110 |
|
| |
| 111 |
|
| static UV |
| 112 |
20502 |
| ord_in_utf8(U8 *s, STRLEN curlen, STRLEN *retlen) |
| 113 |
|
| { |
| 114 |
20502 |
| UV uv = 0; |
| 115 |
|
| STRLEN len, i; |
| 116 |
|
| |
| 117 |
20502 |
▶ | if (curlen == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (curlen == 0) { |
0 |
20502 |
20502 |
|
| 118 |
0 |
▶ | if (retlen) |
Branch: 1 branch
| Branch | True | False | Total |
| if (retlen) |
0 |
0 |
0 |
|
| 119 |
0 |
| *retlen = 0; |
| 120 |
0 |
| return 0; |
| 121 |
|
| } |
| 122 |
|
| |
| 123 |
40933 |
▶ | len = *s < 0x80 ? 1 : |
Branch: 1 branch
| Branch | True | False | Total |
| len = *s < 0x80 ? 1 : |
20431 |
71 |
20502 |
|
| 124 |
20431 |
▶ | *s < 0xC0 ? 0 : |
Branch: 1 branch
| Branch | True | False | Total |
| *s < 0xC0 ? 0 : |
20423 |
8 |
20431 |
|
| 125 |
20423 |
▶ | *s < 0xE0 ? 2 : |
Branch: 1 branch
| Branch | True | False | Total |
| *s < 0xE0 ? 2 : |
18381 |
2042 |
20423 |
|
| 126 |
18381 |
▶ | *s < 0xF0 ? 3 : |
Branch: 1 branch
| Branch | True | False | Total |
| *s < 0xF0 ? 3 : |
5111 |
13270 |
18381 |
|
| 127 |
5111 |
▶ | *s < 0xF8 ? 4 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| *s < 0xF8 ? 4 : 0; |
5109 |
2 |
5111 |
|
| 128 |
|
| |
| 129 |
20502 |
▶ | if (curlen < len || len == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (curlen < len || len == 0) { |
20498 |
4 |
20502 |
| if (curlen < len || len == 0) { |
10 |
20488 |
20498 |
|
| 130 |
14 |
▶ | if (retlen) |
Branch: 1 branch
| Branch | True | False | Total |
| if (retlen) |
14 |
0 |
14 |
|
| 131 |
14 |
| *retlen = 0; |
| 132 |
14 |
| return 0; |
| 133 |
|
| } |
| 134 |
|
| |
| 135 |
20488 |
▶ | if (*s < 0x80) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s < 0x80) { |
71 |
20417 |
20488 |
|
| 136 |
71 |
| uv = (UV)*s; |
| 137 |
|
| } |
| 138 |
20417 |
▶ | else if (*s < 0xE0) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*s < 0xE0) { |
2040 |
18377 |
20417 |
|
| 139 |
2040 |
| uv = (UV)(((s[0] & 0x1f) << 6) | (s[1] & 0x3f)); |
| 140 |
|
| } |
| 141 |
18377 |
▶ | else if (*s < 0xF0) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*s < 0xF0) { |
13270 |
5107 |
18377 |
|
| 142 |
13270 |
| uv = (UV)(((s[0] & 0x0f) << 12) | |
| 143 |
13270 |
| ((s[1] & 0x3f) << 6) | (s[2] & 0x3f)); |
| 144 |
|
| } |
| 145 |
5107 |
▶ | else if (*s < 0xF8) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*s < 0xF8) { |
5107 |
0 |
5107 |
|
| 146 |
5107 |
| uv = (UV)(((s[0] & 0x07) << 18) | ((s[1] & 0x3f) << 12) | |
| 147 |
5107 |
| ((s[2] & 0x3f) << 6) | (s[3] & 0x3f)); |
| 148 |
|
| } |
| 149 |
|
| |
| 150 |
64385 |
▶ | for (i = 1; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < len; i++) { |
43901 |
20484 |
64385 |
|
| 151 |
43901 |
▶ | if (!UTF8A_TRAIL(s[i])) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!UTF8A_TRAIL(s[i])) { |
4 |
43897 |
43901 |
|
| 152 |
4 |
| len = 0; |
| 153 |
4 |
| break; |
| 154 |
|
| } |
| 155 |
|
| } |
| 156 |
|
| |
| 157 |
20488 |
▶ | if (len != (STRLEN) UTF8A_SKIP(uv)) |
Branch: 7 branches
| Branch | True | False | Total |
| if (len != (STRLEN) UTF8A_SKIP(uv)) |
20415 |
73 |
20488 |
| if (len != (STRLEN) UTF8A_SKIP(uv)) |
18377 |
2038 |
20415 |
| if (len != (STRLEN) UTF8A_SKIP(uv)) |
5107 |
13270 |
18377 |
| if (len != (STRLEN) UTF8A_SKIP(uv)) |
0 |
5107 |
5107 |
| if (len != (STRLEN) UTF8A_SKIP(uv)) |
0 |
0 |
0 |
| if (len != (STRLEN) UTF8A_SKIP(uv)) |
0 |
0 |
0 |
| if (len != (STRLEN) UTF8A_SKIP(uv)) |
6 |
20482 |
20488 |
|
| 158 |
6 |
| len = 0; |
| 159 |
|
| |
| 160 |
20488 |
▶ | if (retlen) |
Branch: 1 branch
| Branch | True | False | Total |
| if (retlen) |
20488 |
0 |
20488 |
|
| 161 |
20488 |
| *retlen = len; |
| 162 |
20488 |
| return uv; |
| 163 |
|
| } |
| 164 |
|
| |
| 165 |
|
| |
| 166 |
|
| static U8* |
| 167 |
5115 |
| app_in_utf16le(U8* s, UV uv) |
| 168 |
|
| { |
| 169 |
5115 |
▶ | if (uv <= 0xFFFF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (uv <= 0xFFFF) { |
5115 |
0 |
5115 |
|
| 170 |
5115 |
| *s++ = (U8)(uv & 0xff); |
| 171 |
5115 |
| *s++ = (U8)(uv >> 8); |
| 172 |
|
| } |
| 173 |
0 |
▶ | else if (Is_VALID_UTF(uv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (Is_VALID_UTF(uv)) { |
0 |
0 |
0 |
|
| 174 |
|
| int hi, lo; |
| 175 |
0 |
| uv -= 0x10000; |
| 176 |
0 |
| hi = (0xD800 | (uv >> 10)); |
| 177 |
0 |
| lo = (0xDC00 | (uv & 0x3FF)); |
| 178 |
0 |
| *s++ = (U8)(hi & 0xff); |
| 179 |
0 |
| *s++ = (U8)(hi >> 8); |
| 180 |
0 |
| *s++ = (U8)(lo & 0xff); |
| 181 |
0 |
| *s++ = (U8)(lo >> 8); |
| 182 |
|
| } |
| 183 |
5115 |
| return s; |
| 184 |
|
| } |
| 185 |
|
| |
| 186 |
|
| |
| 187 |
|
| static U8* |
| 188 |
5115 |
| app_in_utf16be(U8* s, UV uv) |
| 189 |
|
| { |
| 190 |
5115 |
▶ | if (uv <= 0xFFFF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (uv <= 0xFFFF) { |
5115 |
0 |
5115 |
|
| 191 |
5115 |
| *s++ = (U8)(uv >> 8); |
| 192 |
5115 |
| *s++ = (U8)(uv & 0xff); |
| 193 |
|
| } |
| 194 |
0 |
▶ | else if (Is_VALID_UTF(uv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (Is_VALID_UTF(uv)) { |
0 |
0 |
0 |
|
| 195 |
|
| int hi, lo; |
| 196 |
0 |
| uv -= 0x10000; |
| 197 |
0 |
| hi = (0xD800 | (uv >> 10)); |
| 198 |
0 |
| lo = (0xDC00 | (uv & 0x3FF)); |
| 199 |
0 |
| *s++ = (U8)(hi >> 8); |
| 200 |
0 |
| *s++ = (U8)(hi & 0xff); |
| 201 |
0 |
| *s++ = (U8)(lo >> 8); |
| 202 |
0 |
| *s++ = (U8)(lo & 0xff); |
| 203 |
|
| } |
| 204 |
5115 |
| return s; |
| 205 |
|
| } |
| 206 |
|
| |
| 207 |
|
| |
| 208 |
|
| static U8* |
| 209 |
5115 |
| app_in_utf32le(U8* s, UV uv) |
| 210 |
|
| { |
| 211 |
5115 |
▶ | if (Is_VALID_UTF(uv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (Is_VALID_UTF(uv)) { |
5115 |
0 |
5115 |
|
| 212 |
5115 |
| *s++ = (U8)((uv ) & 0xff); |
| 213 |
5115 |
| *s++ = (U8)((uv >> 8) & 0xff); |
| 214 |
5115 |
| *s++ = (U8)((uv >> 16) & 0xff); |
| 215 |
5115 |
| *s++ = (U8)((uv >> 24) & 0xff); |
| 216 |
|
| } |
| 217 |
5115 |
| return s; |
| 218 |
|
| } |
| 219 |
|
| |
| 220 |
|
| |
| 221 |
|
| static U8* |
| 222 |
5115 |
| app_in_utf32be(U8* s, UV uv) |
| 223 |
|
| { |
| 224 |
5115 |
▶ | if (Is_VALID_UTF(uv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (Is_VALID_UTF(uv)) { |
5115 |
0 |
5115 |
|
| 225 |
5115 |
| *s++ = (U8)((uv >> 24) & 0xff); |
| 226 |
5115 |
| *s++ = (U8)((uv >> 16) & 0xff); |
| 227 |
5115 |
| *s++ = (U8)((uv >> 8) & 0xff); |
| 228 |
5115 |
| *s++ = (U8)((uv ) & 0xff); |
| 229 |
|
| } |
| 230 |
5115 |
| return s; |
| 231 |
|
| } |
| 232 |
|
| |
| 233 |
|
| |
| 234 |
|
| static U8* |
| 235 |
5115 |
| app_in_utf8(U8* s, UV uv) |
| 236 |
|
| { |
| 237 |
5115 |
▶ | if (uv < 0x80) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (uv < 0x80) { |
42 |
5073 |
5115 |
|
| 238 |
42 |
| *s++ = (U8)(uv & 0xff); |
| 239 |
|
| } |
| 240 |
5073 |
▶ | else if (uv < 0x800) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (uv < 0x800) { |
1 |
5072 |
5073 |
|
| 241 |
1 |
| *s++ = (U8)(( uv >> 6) | 0xc0); |
| 242 |
1 |
| *s++ = (U8)(( uv & 0x3f) | 0x80); |
| 243 |
|
| } |
| 244 |
5072 |
▶ | else if (uv < 0x10000) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (uv < 0x10000) { |
5072 |
0 |
5072 |
|
| 245 |
5072 |
| *s++ = (U8)(( uv >> 12) | 0xe0); |
| 246 |
5072 |
| *s++ = (U8)(((uv >> 6) & 0x3f) | 0x80); |
| 247 |
5072 |
| *s++ = (U8)(( uv & 0x3f) | 0x80); |
| 248 |
|
| } |
| 249 |
0 |
▶ | else if (Is_VALID_UTF(uv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (Is_VALID_UTF(uv)) { |
0 |
0 |
0 |
|
| 250 |
0 |
| *s++ = (U8)(( uv >> 18) | 0xf0); |
| 251 |
0 |
| *s++ = (U8)(((uv >> 12) & 0x3f) | 0x80); |
| 252 |
0 |
| *s++ = (U8)(((uv >> 6) & 0x3f) | 0x80); |
| 253 |
0 |
| *s++ = (U8)(( uv & 0x3f) | 0x80); |
| 254 |
|
| } |
| 255 |
5115 |
| return s; |
| 256 |
|
| } |
| 257 |
|
| |
| 258 |
|
| #endif |