include/slug_utf8.h

filter: Statementstmt 52.6% 20 / 38 Branchbran 58.8% 20 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 55.5% 40 / 72 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Slug.pm ↑ Summary Slug.xs »
1 #ifndef SLUG_UTF8_H
2 #define SLUG_UTF8_H
3  
4 /*
5 * slug_utf8.h — UTF-8 codepoint decoder
6 *
7 * Pure C, no Perl dependencies.
8 * Decodes one UTF-8 codepoint at a time, advancing the pointer.
9 * Invalid sequences are skipped (advance by 1 byte, return 0xFFFD).
10 */
11  
12 #include <stdint.h>
13  
14 #define SLUG_REPLACEMENT_CHAR 0xFFFDu
15  
16 /* Decode one UTF-8 codepoint from *p, advance *p past it.
17 * Returns the codepoint, or SLUG_REPLACEMENT_CHAR on invalid input.
18 * Guarantees forward progress: *p always advances by at least 1 byte. */
19 99 static inline uint32_t slug_utf8_decode(const unsigned char **p,
20 const unsigned char *end) {
21 99 const unsigned char *s = *p;
22 uint32_t cp;
23 int trail;
24  
25 99 ▶ if (s >= end) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (s >= end) return 0; 0 99 99
26  
27 /* 1-byte: 0xxxxxxx */
28 99 ▶ if (s[0] < 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (s[0] < 0x80) { 0 99 99
29 0 *p = s + 1;
30 0 return s[0];
31 }
32 /* 2-byte: 110xxxxx 10xxxxxx */
33 99 ▶ else if ((s[0] & 0xE0) == 0xC0) {
Branch: 1 branch
BranchTrueFalseTotal
else if ((s[0] & 0xE0) == 0xC0) { 87 12 99
34 87 trail = 1;
35 87 cp = s[0] & 0x1F;
36 }
37 /* 3-byte: 1110xxxx 10xxxxxx 10xxxxxx */
38 12 ▶ else if ((s[0] & 0xF0) == 0xE0) {
Branch: 1 branch
BranchTrueFalseTotal
else if ((s[0] & 0xF0) == 0xE0) { 12 0 12
39 12 trail = 2;
40 12 cp = s[0] & 0x0F;
41 }
42 /* 4-byte: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
43 0 ▶ else if ((s[0] & 0xF8) == 0xF0) {
Branch: 1 branch
BranchTrueFalseTotal
else if ((s[0] & 0xF8) == 0xF0) { 0 0 0
44 0 trail = 3;
45 0 cp = s[0] & 0x07;
46 }
47 /* Invalid lead byte */
48 else {
49 0 *p = s + 1;
50 0 return SLUG_REPLACEMENT_CHAR;
51 }
52  
53 /* Check we have enough bytes */
54 99 ▶ if (s + 1 + trail > end) {
Branch: 1 branch
BranchTrueFalseTotal
if (s + 1 + trail > end) { 0 99 99
55 0 *p = s + 1;
56 0 return SLUG_REPLACEMENT_CHAR;
57 }
58  
59 /* Decode continuation bytes */
60 {
61 int i;
62 210 ▶ for (i = 1; i <= trail; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i <= trail; i++) { 111 99 210
63 111 ▶ if ((s[i] & 0xC0) != 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if ((s[i] & 0xC0) != 0x80) { 0 111 111
64 0 *p = s + 1;
65 0 return SLUG_REPLACEMENT_CHAR;
66 }
67 111 cp = (cp << 6) | (s[i] & 0x3F);
68 }
69 }
70  
71 /* Overlong check */
72 99 ▶ if ((trail == 1 && cp < 0x80) ||
Branch: 3 branches
BranchTrueFalseTotal
if ((trail == 1 && cp < 0x80) || 87 12 99
if ((trail == 1 && cp < 0x80) || 87 0 87
if ((trail == 1 && cp < 0x80) || 12 87 99
73 99 ▶ (trail == 2 && cp < 0x800) ||
Branch: 2 branches
BranchTrueFalseTotal
(trail == 2 && cp < 0x800) || 12 0 12
(trail == 2 && cp < 0x800) || 0 99 99
74 0 ▶ (trail == 3 && cp < 0x10000)) {
Branch: 1 branch
BranchTrueFalseTotal
(trail == 3 && cp < 0x10000)) { 0 0 0
75 0 *p = s + 1;
76 0 return SLUG_REPLACEMENT_CHAR;
77 }
78  
79 /* Surrogate check */
80 99 ▶ if (cp >= 0xD800 && cp <= 0xDFFF) {
Branch: 2 branches
BranchTrueFalseTotal
if (cp >= 0xD800 && cp <= 0xDFFF) { 5 94 99
if (cp >= 0xD800 && cp <= 0xDFFF) { 0 5 5
81 0 *p = s + 1;
82 0 return SLUG_REPLACEMENT_CHAR;
83 }
84  
85 /* Beyond valid Unicode */
86 99 ▶ if (cp > 0x10FFFF) {
Branch: 1 branch
BranchTrueFalseTotal
if (cp > 0x10FFFF) { 0 99 99
87 0 *p = s + 1;
88 0 return SLUG_REPLACEMENT_CHAR;
89 }
90  
91 99 *p = s + 1 + trail;
92 99 return cp;
93 }
94  
95 #endif /* SLUG_UTF8_H */
« blib/lib/Slug.pm ↑ Summary Slug.xs »