| 1 |
|
| #include <stdbool.h> |
| 2 |
|
| |
| 3 |
|
| #include "utf8.h" |
| 4 |
|
| |
| 5 |
184 |
| inline static bool _is_utf8_continuation(int c) { return (c & 0xc0) == 0x80; } |
| 6 |
|
| |
| 7 |
367606 |
| kdl_utf8_status _kdl_pop_codepoint(kdl_str* str, uint32_t* codepoint) |
| 8 |
|
| { |
| 9 |
367606 |
▶ | if (str->len < 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (str->len < 1) { |
12856 |
354750 |
367606 |
|
| 10 |
12856 |
| return KDL_UTF8_EOF; |
| 11 |
|
| } |
| 12 |
|
| |
| 13 |
354750 |
| char const* s = str->data; |
| 14 |
|
| // Check high bit |
| 15 |
354750 |
▶ | if ((s[0] & 0x80) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((s[0] & 0x80) == 0) { |
354596 |
154 |
354750 |
|
| 16 |
|
| // ASCII |
| 17 |
354596 |
| *codepoint = (uint32_t)*s; |
| 18 |
354596 |
| ++str->data; |
| 19 |
354596 |
| --str->len; |
| 20 |
354596 |
| return KDL_UTF8_OK; |
| 21 |
154 |
▶ | } else if ((s[0] & 0xe0) == 0xc0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if ((s[0] & 0xe0) == 0xc0) { |
62 |
92 |
154 |
|
| 22 |
|
| // 2-byte sequence |
| 23 |
62 |
▶ | if (str->len < 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (str->len < 2) { |
4 |
58 |
62 |
|
| 24 |
|
| // Incomplete UTF-8 sequence |
| 25 |
4 |
| return KDL_UTF8_INCOMPLETE; |
| 26 |
58 |
▶ | } else if (!_is_utf8_continuation(s[1])) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (!_is_utf8_continuation(s[1])) { |
0 |
58 |
58 |
|
| 27 |
|
| // Invalid UTF-8 sequence |
| 28 |
0 |
| return KDL_UTF8_DECODE_ERROR; |
| 29 |
|
| } |
| 30 |
58 |
| *codepoint = ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
| 31 |
58 |
| str->data += 2; |
| 32 |
58 |
| str->len -= 2; |
| 33 |
58 |
| return KDL_UTF8_OK; |
| 34 |
92 |
▶ | } else if ((s[0] & 0xf0) == 0xe0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if ((s[0] & 0xf0) == 0xe0) { |
70 |
22 |
92 |
|
| 35 |
|
| // 3-byte sequence |
| 36 |
70 |
▶ | if (str->len < 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (str->len < 3) { |
22 |
48 |
70 |
|
| 37 |
|
| // Incomplete UTF-8 sequence |
| 38 |
22 |
| return KDL_UTF8_INCOMPLETE; |
| 39 |
48 |
▶ | } else if (!_is_utf8_continuation(s[1]) || !_is_utf8_continuation(s[2])) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (!_is_utf8_continuation(s[1]) || !_is_utf8_continuation(s[2])) { |
48 |
0 |
48 |
| } else if (!_is_utf8_continuation(s[1]) || !_is_utf8_continuation(s[2])) { |
0 |
48 |
48 |
|
| 40 |
|
| // Invalid UTF-8 sequence |
| 41 |
0 |
| return KDL_UTF8_DECODE_ERROR; |
| 42 |
|
| } |
| 43 |
48 |
| *codepoint = ((s[0] & 0xf) << 12) | ((s[1] & 0x3f) << 6) | (s[2] & 0x3f); |
| 44 |
48 |
| str->data += 3; |
| 45 |
48 |
| str->len -= 3; |
| 46 |
48 |
| return KDL_UTF8_OK; |
| 47 |
22 |
▶ | } else if ((s[0] & 0xf8) == 0xf0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if ((s[0] & 0xf8) == 0xf0) { |
22 |
0 |
22 |
|
| 48 |
|
| // 4-byte sequence |
| 49 |
22 |
▶ | if (str->len < 4) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (str->len < 4) { |
12 |
10 |
22 |
|
| 50 |
|
| // Incomplete UTF-8 sequence |
| 51 |
12 |
| return KDL_UTF8_INCOMPLETE; |
| 52 |
10 |
▶ | } else if (!_is_utf8_continuation(s[1]) || !_is_utf8_continuation(s[2]) |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (!_is_utf8_continuation(s[1]) || !_is_utf8_continuation(s[2]) |
10 |
0 |
10 |
| } else if (!_is_utf8_continuation(s[1]) || !_is_utf8_continuation(s[2]) |
10 |
0 |
10 |
|
| 53 |
10 |
▶ | || !_is_utf8_continuation(s[3])) { |
Branch: 1 branch
| Branch | True | False | Total |
| || !_is_utf8_continuation(s[3])) { |
0 |
10 |
10 |
|
| 54 |
|
| // Invalid UTF-8 sequence |
| 55 |
0 |
| return KDL_UTF8_DECODE_ERROR; |
| 56 |
|
| } |
| 57 |
10 |
| *codepoint = ((s[0] & 0x7) << 18) | ((s[1] & 0x3f) << 12) | ((s[2] & 0x3f) << 6) | (s[3] & 0x3f); |
| 58 |
10 |
| str->data += 4; |
| 59 |
10 |
| str->len -= 4; |
| 60 |
10 |
| return KDL_UTF8_OK; |
| 61 |
|
| } else { |
| 62 |
0 |
| return KDL_UTF8_DECODE_ERROR; |
| 63 |
|
| } |
| 64 |
|
| } |
| 65 |
|
| |
| 66 |
1019 |
| int _kdl_push_codepoint(uint32_t codepoint, char* buf) |
| 67 |
|
| { |
| 68 |
1019 |
| char* p = buf; |
| 69 |
1019 |
▶ | if (codepoint <= 0x7f) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (codepoint <= 0x7f) { |
999 |
20 |
1019 |
|
| 70 |
|
| // ASCII |
| 71 |
999 |
| *(p++) = (char)codepoint; |
| 72 |
999 |
| return (int)(p - buf); |
| 73 |
20 |
▶ | } else if (codepoint <= 0x7ff) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (codepoint <= 0x7ff) { |
6 |
14 |
20 |
|
| 74 |
|
| // fits in two bytes |
| 75 |
6 |
| *(p++) = 0xc0 | (0x1f & (codepoint >> 6)); |
| 76 |
6 |
| *(p++) = 0x80 | (0x3f & codepoint); |
| 77 |
6 |
| return (int)(p - buf); |
| 78 |
14 |
▶ | } else if (codepoint <= 0xffff) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (codepoint <= 0xffff) { |
12 |
2 |
14 |
|
| 79 |
|
| // fits in three bytes |
| 80 |
12 |
| *(p++) = 0xe0 | (0x0f & (codepoint >> 12)); |
| 81 |
12 |
| *(p++) = 0x80 | (0x3f & (codepoint >> 6)); |
| 82 |
12 |
| *(p++) = 0x80 | (0x3f & codepoint); |
| 83 |
12 |
| return (int)(p - buf); |
| 84 |
2 |
▶ | } else if (codepoint <= 0x10ffff) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (codepoint <= 0x10ffff) { |
2 |
0 |
2 |
|
| 85 |
|
| // fits in four bytes |
| 86 |
2 |
| *(p++) = 0xf0 | (0x07 & (codepoint >> 18)); |
| 87 |
2 |
| *(p++) = 0x80 | (0x3f & (codepoint >> 12)); |
| 88 |
2 |
| *(p++) = 0x80 | (0x3f & (codepoint >> 6)); |
| 89 |
2 |
| *(p++) = 0x80 | (0x3f & codepoint); |
| 90 |
2 |
| return (int)(p - buf); |
| 91 |
|
| } else { |
| 92 |
|
| // value too big |
| 93 |
0 |
| return 0; |
| 94 |
|
| } |
| 95 |
|
| } |