| 40 | }; |
| 41 | |
| 42 | char const* cm_utf8_decode_character(char const* first, char const* last, |
| 43 | unsigned int* pc) |
| 44 | { |
| 45 | /* We need at least one byte. */ |
| 46 | if (first == last) { |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | /* Count leading ones in the first byte. */ |
| 51 | unsigned char c = (unsigned char)*first++; |
| 52 | unsigned char const ones = cm_utf8_ones[c]; |
| 53 | switch (ones) { |
| 54 | case 0: |
| 55 | *pc = c; |
| 56 | return first; /* One-byte character. */ |
| 57 | case 1: |
| 58 | case 7: |
| 59 | case 8: |
| 60 | return 0; /* Invalid leading byte. */ |
| 61 | default: |
| 62 | break; |
| 63 | } |
| 64 | |
| 65 | /* Extract bits from this multi-byte character. */ |
| 66 | { |
| 67 | unsigned int uc = c & cm_utf8_mask[ones]; |
| 68 | int left; |
| 69 | for (left = ones - 1; left && first != last; --left) { |
| 70 | c = (unsigned char)*first++; |
| 71 | if (cm_utf8_ones[c] != 1) { |
| 72 | return 0; |
| 73 | } |
| 74 | uc = (uc << 6) | (c & cm_utf8_mask[1]); |
| 75 | } |
| 76 | |
| 77 | if (left > 0 || uc < cm_utf8_min[ones]) { |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | /* UTF-16 surrogate halves. */ |
| 82 | if (0xD800 <= uc && uc <= 0xDFFF) { |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | /* Invalid codepoints. */ |
| 87 | if (0x10FFFF < uc) { |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | *pc = uc; |
| 92 | return first; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | int cm_utf8_is_valid(char const* s) |
| 97 | { |
no outgoing calls
searching dependent graphs…