Debian Codegraph @

rpython/rlib/fastutf8/src/utf8-scalar.c

raw symbols SCIP
1 #include "utf8.h"
2
3 int _check_continuation(const uint8_t ** encoded, const uint8_t * endptr, int count) {
4 ssize_t size = endptr - *encoded;
5
6 if (size < count) {
7 // not enough bytes to be a valid 2 byte utf8 code point
8 return -1;
9 }
10 for (int i = 0; i < count; i++) {
11 uint8_t byte = *(*encoded)++;
12 if ((byte & 0xc0) != 0x80) {
13 // continuation byte does NOT match 0x10xxxxxx
14 return -1;
15 }
16 }
17 return 0;
18 }
19
20 ssize_t fu8_count_utf8_codepoints_seq(const char * utf8, size_t len) {
21 size_t num_codepoints = 0;
22 uint8_t byte = 0;
23 const uint8_t * encoded = (const uint8_t*)utf8;
24 const uint8_t * endptr = encoded + len;
25
26 while (encoded < endptr) {
27 byte = *encoded++;
28 if (byte < 0x80) {
29 num_codepoints += 1;
30 continue;
31 } else {
32 //asm("int $3");
33 if ((byte & 0xe0) == 0xc0) {
34 // one continuation byte
35 if (byte < 0xc2) {
36 return -1;
37 }
38 if (_check_continuation(&encoded, endptr, 1) != 0) {
39 return -1;
40 }
41 } else if ((byte & 0xf0) == 0xe0) {
42 // two continuation byte
43 if (_check_continuation(&encoded, endptr, 2) != 0) {
44 return -1;
45 }
46 uint8_t byte1 = encoded[-2];
47 //surrogates shouldn't be valid UTF-8!
48 if ((byte == 0xe0 && byte1 < 0xa0) ||
49 (byte == 0xed && byte1 > 0x9f && !ALLOW_SURROGATES)) {
50 return -1;
51 }
52 } else if ((byte & 0xf8) == 0xf0) {
53 // three continuation byte
54 if (_check_continuation(&encoded, endptr, 3) != 0) {
55 return -1;
56 }
57 uint8_t byte1 = encoded[-3];
58 if ((byte == 0xf0 && byte1 < 0x90) ||
59 (byte == 0xf4 && byte1 > 0x8f) ||
60 (byte >= 0xf5)) {
61 return -1;
62 }
63 } else {
64 // TODO
65 return -1;
66 }
67 num_codepoints += 1;
68 }
69 }
70 return num_codepoints;
71 }