| 1 |
|
| #ifndef SXMUNI_H |
| 2 |
|
| #define SXMUNI_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 |
43046 |
| ord_in_utf16le(U8 *s, STRLEN curlen, STRLEN *retlen) |
| 23 |
|
| { |
| 24 |
|
| UV uv, luv; |
| 25 |
43046 |
| U8 *p = s; |
| 26 |
|
| |
| 27 |
43046 |
▶ | if (curlen < 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (curlen < 2) { |
18 |
43028 |
43046 |
|
| 28 |
18 |
▶ | if (retlen) |
Branch: 1 branch
| Branch | True | False | Total |
| if (retlen) |
18 |
0 |
18 |
|
| 29 |
18 |
| *retlen = 0; |
| 30 |
18 |
| return 0; |
| 31 |
|
| } |
| 32 |
|
| |
| 33 |
43028 |
| uv = (UV)((p[1] << 8) | p[0]); |
| 34 |
43028 |
| p += 2; |
| 35 |
|
| |
| 36 |
43028 |
▶ | if (UTF16_HI_SURROG(uv) && (4 <= curlen)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (UTF16_HI_SURROG(uv) && (4 <= curlen)) { |
4070 |
38958 |
43028 |
| if (UTF16_HI_SURROG(uv) && (4 <= curlen)) { |
2040 |
2030 |
4070 |
| if (UTF16_HI_SURROG(uv) && (4 <= curlen)) { |
2040 |
0 |
2040 |
|
| 37 |
2040 |
| luv = (UV)((p[1] << 8) | p[0]); |
| 38 |
|
| |
| 39 |
2040 |
▶ | if (UTF16_LO_SURROG(luv)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (UTF16_LO_SURROG(luv)) { |
2034 |
6 |
2040 |
| if (UTF16_LO_SURROG(luv)) { |
2034 |
0 |
2034 |
|
| 40 |
2034 |
| uv = 0x10000 + ((uv-0xD800) * 0x400) + (luv-0xDC00); |
| 41 |
2034 |
| p += 2; |
| 42 |
|
| } |
| 43 |
|
| } |
| 44 |
|
| |
| 45 |
43028 |
▶ | if (retlen) |
Branch: 1 branch
| Branch | True | False | Total |
| if (retlen) |
43028 |
0 |
43028 |
|
| 46 |
43028 |
| *retlen = p - s; |
| 47 |
43028 |
| return uv; |
| 48 |
|
| } |
| 49 |
|
| |
| 50 |
|
| |
| 51 |
|
| static UV |
| 52 |
43305 |
| ord_in_utf16be(U8 *s, STRLEN curlen, STRLEN *retlen) |
| 53 |
|
| { |
| 54 |
|
| UV uv, luv; |
| 55 |
43305 |
| U8 *p = s; |
| 56 |
|
| |
| 57 |
43305 |
▶ | if (curlen < 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (curlen < 2) { |
18 |
43287 |
43305 |
|
| 58 |
18 |
▶ | if (retlen) |
Branch: 1 branch
| Branch | True | False | Total |
| if (retlen) |
18 |
0 |
18 |
|
| 59 |
18 |
| *retlen = 0; |
| 60 |
18 |
| return 0; |
| 61 |
|
| } |
| 62 |
|
| |
| 63 |
43287 |
| uv = (UV)((p[0] << 8) | p[1]); |
| 64 |
43287 |
| p += 2; |
| 65 |
|
| |
| 66 |
43287 |
▶ | if (UTF16_HI_SURROG(uv) && (4 <= curlen)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (UTF16_HI_SURROG(uv) && (4 <= curlen)) { |
4092 |
39195 |
43287 |
| if (UTF16_HI_SURROG(uv) && (4 <= curlen)) { |
2048 |
2044 |
4092 |
| if (UTF16_HI_SURROG(uv) && (4 <= curlen)) { |
2048 |
0 |
2048 |
|
| 67 |
2048 |
| luv = (UV)((p[0] << 8) | p[1]); |
| 68 |
|
| |
| 69 |
2048 |
▶ | if (UTF16_LO_SURROG(luv)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (UTF16_LO_SURROG(luv)) { |
2042 |
6 |
2048 |
| if (UTF16_LO_SURROG(luv)) { |
2042 |
0 |
2042 |
|
| 70 |
2042 |
| uv = 0x10000 + ((uv-0xD800) * 0x400) + (luv-0xDC00); |
| 71 |
2042 |
| p += 2; |
| 72 |
|
| } |
| 73 |
|
| } |
| 74 |
|
| |
| 75 |
43287 |
▶ | if (retlen) |
Branch: 1 branch
| Branch | True | False | Total |
| if (retlen) |
43287 |
0 |
43287 |
|
| 76 |
43287 |
| *retlen = p - s; |
| 77 |
43287 |
| return uv; |
| 78 |
|
| } |
| 79 |
|
| |
| 80 |
|
| |
| 81 |
|
| static UV |
| 82 |
50764 |
| ord_in_utf32le(U8 *s, STRLEN curlen, STRLEN *retlen) |
| 83 |
|
| { |
| 84 |
50764 |
▶ | if (curlen < 4) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (curlen < 4) { |
46 |
50718 |
50764 |
|
| 85 |
46 |
▶ | if (retlen) |
Branch: 1 branch
| Branch | True | False | Total |
| if (retlen) |
46 |
0 |
46 |
|
| 86 |
46 |
| *retlen = 0; |
| 87 |
46 |
| return 0; |
| 88 |
|
| } |
| 89 |
|
| |
| 90 |
50718 |
▶ | if (retlen) |
Branch: 1 branch
| Branch | True | False | Total |
| if (retlen) |
50718 |
0 |
50718 |
|
| 91 |
50718 |
| *retlen = 4; |
| 92 |
50718 |
| return (UV)((s[3] << 24) | (s[2] << 16) | (s[1] << 8) | s[0]); |
| 93 |
|
| } |
| 94 |
|
| |
| 95 |
|
| |
| 96 |
|
| static UV |
| 97 |
51043 |
| ord_in_utf32be(U8 *s, STRLEN curlen, STRLEN *retlen) |
| 98 |
|
| { |
| 99 |
51043 |
▶ | if (curlen < 4) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (curlen < 4) { |
50 |
50993 |
51043 |
|
| 100 |
50 |
▶ | if (retlen) |
Branch: 1 branch
| Branch | True | False | Total |
| if (retlen) |
50 |
0 |
50 |
|
| 101 |
50 |
| *retlen = 0; |
| 102 |
50 |
| return 0; |
| 103 |
|
| } |
| 104 |
|
| |
| 105 |
50993 |
▶ | if (retlen) |
Branch: 1 branch
| Branch | True | False | Total |
| if (retlen) |
50993 |
0 |
50993 |
|
| 106 |
50993 |
| *retlen = 4; |
| 107 |
50993 |
| return (UV)((s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3]); |
| 108 |
|
| } |
| 109 |
|
| |
| 110 |
|
| |
| 111 |
|
| static UV |
| 112 |
50513 |
| ord_in_utf8(U8 *s, STRLEN curlen, STRLEN *retlen) |
| 113 |
|
| { |
| 114 |
50513 |
| UV uv = 0; |
| 115 |
|
| STRLEN len, i; |
| 116 |
|
| |
| 117 |
50513 |
▶ | if (curlen == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (curlen == 0) { |
0 |
50513 |
50513 |
|
| 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 |
100826 |
▶ | len = *s < 0x80 ? 1 : |
Branch: 1 branch
| Branch | True | False | Total |
| len = *s < 0x80 ? 1 : |
50313 |
200 |
50513 |
|
| 124 |
50313 |
▶ | *s < 0xC0 ? 0 : |
Branch: 1 branch
| Branch | True | False | Total |
| *s < 0xC0 ? 0 : |
50297 |
16 |
50313 |
|
| 125 |
50297 |
▶ | *s < 0xE0 ? 2 : |
Branch: 1 branch
| Branch | True | False | Total |
| *s < 0xE0 ? 2 : |
36189 |
14108 |
50297 |
|
| 126 |
36189 |
▶ | *s < 0xF0 ? 3 : |
Branch: 1 branch
| Branch | True | False | Total |
| *s < 0xF0 ? 3 : |
9718 |
26471 |
36189 |
|
| 127 |
9718 |
▶ | *s < 0xF8 ? 4 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| *s < 0xF8 ? 4 : 0; |
9714 |
4 |
9718 |
|
| 128 |
|
| |
| 129 |
50513 |
▶ | if (curlen < len || len == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (curlen < len || len == 0) { |
50497 |
16 |
50513 |
| if (curlen < len || len == 0) { |
20 |
50477 |
50497 |
|
| 130 |
36 |
▶ | if (retlen) |
Branch: 1 branch
| Branch | True | False | Total |
| if (retlen) |
36 |
0 |
36 |
|
| 131 |
36 |
| *retlen = 0; |
| 132 |
36 |
| return 0; |
| 133 |
|
| } |
| 134 |
|
| |
| 135 |
50477 |
▶ | if (*s < 0x80) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s < 0x80) { |
200 |
50277 |
50477 |
|
| 136 |
200 |
| uv = (UV)*s; |
| 137 |
|
| } |
| 138 |
50277 |
▶ | else if (*s < 0xE0) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*s < 0xE0) { |
14096 |
36181 |
50277 |
|
| 139 |
14096 |
| uv = (UV)(((s[0] & 0x1f) << 6) | (s[1] & 0x3f)); |
| 140 |
|
| } |
| 141 |
36181 |
▶ | else if (*s < 0xF0) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*s < 0xF0) { |
26471 |
9710 |
36181 |
|
| 142 |
26471 |
| uv = (UV)(((s[0] & 0x0f) << 12) | |
| 143 |
26471 |
| ((s[1] & 0x3f) << 6) | (s[2] & 0x3f)); |
| 144 |
|
| } |
| 145 |
9710 |
▶ | else if (*s < 0xF8) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*s < 0xF8) { |
9710 |
0 |
9710 |
|
| 146 |
9710 |
| uv = (UV)(((s[0] & 0x07) << 18) | ((s[1] & 0x3f) << 12) | |
| 147 |
9710 |
| ((s[2] & 0x3f) << 6) | (s[3] & 0x3f)); |
| 148 |
|
| } |
| 149 |
|
| |
| 150 |
146629 |
▶ | for (i = 1; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < len; i++) { |
96168 |
50461 |
146629 |
|
| 151 |
96168 |
▶ | if (!UTF8A_TRAIL(s[i])) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!UTF8A_TRAIL(s[i])) { |
16 |
96152 |
96168 |
|
| 152 |
16 |
| len = 0; |
| 153 |
16 |
| break; |
| 154 |
|
| } |
| 155 |
|
| } |
| 156 |
|
| |
| 157 |
50477 |
▶ | if (len != (STRLEN) UTF8A_SKIP(uv)) |
Branch: 7 branches
| Branch | True | False | Total |
| if (len != (STRLEN) UTF8A_SKIP(uv)) |
50273 |
204 |
50477 |
| if (len != (STRLEN) UTF8A_SKIP(uv)) |
36181 |
14092 |
50273 |
| if (len != (STRLEN) UTF8A_SKIP(uv)) |
9710 |
26471 |
36181 |
| if (len != (STRLEN) UTF8A_SKIP(uv)) |
0 |
9710 |
9710 |
| if (len != (STRLEN) UTF8A_SKIP(uv)) |
0 |
0 |
0 |
| if (len != (STRLEN) UTF8A_SKIP(uv)) |
0 |
0 |
0 |
| if (len != (STRLEN) UTF8A_SKIP(uv)) |
20 |
50457 |
50477 |
|
| 158 |
20 |
| len = 0; |
| 159 |
|
| |
| 160 |
50477 |
▶ | if (retlen) |
Branch: 1 branch
| Branch | True | False | Total |
| if (retlen) |
50477 |
0 |
50477 |
|
| 161 |
50477 |
| *retlen = len; |
| 162 |
50477 |
| return uv; |
| 163 |
|
| } |
| 164 |
|
| |
| 165 |
|
| |
| 166 |
|
| static U8* |
| 167 |
18233 |
| app_in_utf16le(U8* s, UV uv) |
| 168 |
|
| { |
| 169 |
18233 |
▶ | if (uv <= 0xFFFF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (uv <= 0xFFFF) { |
18225 |
8 |
18233 |
|
| 170 |
18225 |
| *s++ = (U8)(uv & 0xff); |
| 171 |
18225 |
| *s++ = (U8)(uv >> 8); |
| 172 |
|
| } |
| 173 |
8 |
▶ | else if (Is_VALID_UTF(uv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (Is_VALID_UTF(uv)) { |
8 |
0 |
8 |
|
| 174 |
|
| int hi, lo; |
| 175 |
8 |
| uv -= 0x10000; |
| 176 |
8 |
| hi = (0xD800 | (uv >> 10)); |
| 177 |
8 |
| lo = (0xDC00 | (uv & 0x3FF)); |
| 178 |
8 |
| *s++ = (U8)(hi & 0xff); |
| 179 |
8 |
| *s++ = (U8)(hi >> 8); |
| 180 |
8 |
| *s++ = (U8)(lo & 0xff); |
| 181 |
8 |
| *s++ = (U8)(lo >> 8); |
| 182 |
|
| } |
| 183 |
18233 |
| return s; |
| 184 |
|
| } |
| 185 |
|
| |
| 186 |
|
| |
| 187 |
|
| static U8* |
| 188 |
18233 |
| app_in_utf16be(U8* s, UV uv) |
| 189 |
|
| { |
| 190 |
18233 |
▶ | if (uv <= 0xFFFF) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (uv <= 0xFFFF) { |
18225 |
8 |
18233 |
|
| 191 |
18225 |
| *s++ = (U8)(uv >> 8); |
| 192 |
18225 |
| *s++ = (U8)(uv & 0xff); |
| 193 |
|
| } |
| 194 |
8 |
▶ | else if (Is_VALID_UTF(uv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (Is_VALID_UTF(uv)) { |
8 |
0 |
8 |
|
| 195 |
|
| int hi, lo; |
| 196 |
8 |
| uv -= 0x10000; |
| 197 |
8 |
| hi = (0xD800 | (uv >> 10)); |
| 198 |
8 |
| lo = (0xDC00 | (uv & 0x3FF)); |
| 199 |
8 |
| *s++ = (U8)(hi >> 8); |
| 200 |
8 |
| *s++ = (U8)(hi & 0xff); |
| 201 |
8 |
| *s++ = (U8)(lo >> 8); |
| 202 |
8 |
| *s++ = (U8)(lo & 0xff); |
| 203 |
|
| } |
| 204 |
18233 |
| return s; |
| 205 |
|
| } |
| 206 |
|
| |
| 207 |
|
| |
| 208 |
|
| static U8* |
| 209 |
18233 |
| app_in_utf32le(U8* s, UV uv) |
| 210 |
|
| { |
| 211 |
18233 |
▶ | if (Is_VALID_UTF(uv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (Is_VALID_UTF(uv)) { |
18233 |
0 |
18233 |
|
| 212 |
18233 |
| *s++ = (U8)((uv ) & 0xff); |
| 213 |
18233 |
| *s++ = (U8)((uv >> 8) & 0xff); |
| 214 |
18233 |
| *s++ = (U8)((uv >> 16) & 0xff); |
| 215 |
18233 |
| *s++ = (U8)((uv >> 24) & 0xff); |
| 216 |
|
| } |
| 217 |
18233 |
| return s; |
| 218 |
|
| } |
| 219 |
|
| |
| 220 |
|
| |
| 221 |
|
| static U8* |
| 222 |
18233 |
| app_in_utf32be(U8* s, UV uv) |
| 223 |
|
| { |
| 224 |
18233 |
▶ | if (Is_VALID_UTF(uv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (Is_VALID_UTF(uv)) { |
18233 |
0 |
18233 |
|
| 225 |
18233 |
| *s++ = (U8)((uv >> 24) & 0xff); |
| 226 |
18233 |
| *s++ = (U8)((uv >> 16) & 0xff); |
| 227 |
18233 |
| *s++ = (U8)((uv >> 8) & 0xff); |
| 228 |
18233 |
| *s++ = (U8)((uv ) & 0xff); |
| 229 |
|
| } |
| 230 |
18233 |
| return s; |
| 231 |
|
| } |
| 232 |
|
| |
| 233 |
|
| |
| 234 |
|
| static U8* |
| 235 |
18233 |
| app_in_utf8(U8* s, UV uv) |
| 236 |
|
| { |
| 237 |
18233 |
▶ | if (uv < 0x80) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (uv < 0x80) { |
130 |
18103 |
18233 |
|
| 238 |
130 |
| *s++ = (U8)(uv & 0xff); |
| 239 |
|
| } |
| 240 |
18103 |
▶ | else if (uv < 0x800) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (uv < 0x800) { |
8024 |
10079 |
18103 |
|
| 241 |
8024 |
| *s++ = (U8)(( uv >> 6) | 0xc0); |
| 242 |
8024 |
| *s++ = (U8)(( uv & 0x3f) | 0x80); |
| 243 |
|
| } |
| 244 |
10079 |
▶ | else if (uv < 0x10000) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (uv < 0x10000) { |
10071 |
8 |
10079 |
|
| 245 |
10071 |
| *s++ = (U8)(( uv >> 12) | 0xe0); |
| 246 |
10071 |
| *s++ = (U8)(((uv >> 6) & 0x3f) | 0x80); |
| 247 |
10071 |
| *s++ = (U8)(( uv & 0x3f) | 0x80); |
| 248 |
|
| } |
| 249 |
8 |
▶ | else if (Is_VALID_UTF(uv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (Is_VALID_UTF(uv)) { |
8 |
0 |
8 |
|
| 250 |
8 |
| *s++ = (U8)(( uv >> 18) | 0xf0); |
| 251 |
8 |
| *s++ = (U8)(((uv >> 12) & 0x3f) | 0x80); |
| 252 |
8 |
| *s++ = (U8)(((uv >> 6) & 0x3f) | 0x80); |
| 253 |
8 |
| *s++ = (U8)(( uv & 0x3f) | 0x80); |
| 254 |
|
| } |
| 255 |
18233 |
| return s; |
| 256 |
|
| } |
| 257 |
|
| |
| 258 |
|
| #endif |