| 1 |
|
| /* stencil_str.h - compare two strings as perl's `cmp` does, by code point, |
| 2 |
|
| * across the latin-1 / UTF-8 boundary, without allocating. |
| 3 |
|
| * |
| 4 |
|
| * A latin-1 "caf\xE9" equals its UTF-8 twin "caf\xC3\xA9". Perl compares |
| 5 |
|
| * them with bytes_cmp_utf8(), which arrived in 5.13.7 and which ppport.h |
| 6 |
|
| * does not back-port, so the rule is written here, Perl-free so t/07 can |
| 7 |
|
| * test it on its own. Same form on both sides: memcmp, then length (for |
| 8 |
|
| * well-formed UTF-8, byte order is code point order). Mixed: walk both; an |
| 9 |
|
| * ASCII byte is itself, C2/C3 plus a continuation byte is the code point |
| 10 |
|
| * 0x80..0xFF it encodes, and anything else on the UTF-8 side (above 0xFF, |
| 11 |
|
| * or malformed) is greater than every latin-1 character. Never reads past |
| 12 |
|
| * either length. No locale. |
| 13 |
|
| */ |
| 14 |
|
| #ifndef STENCIL_STR_H |
| 15 |
|
| #define STENCIL_STR_H |
| 16 |
|
| |
| 17 |
|
| #include <stddef.h> |
| 18 |
|
| #include <string.h> |
| 19 |
|
| |
| 20 |
|
| /* latin-1 bytes `l` vs UTF-8 `u`: <0, 0, >0 as l is less, equal, greater */ |
| 21 |
3848 |
| static int stencil_str_cmp_l1_u8(const unsigned char *l, size_t llen, |
| 22 |
|
| const unsigned char *u, size_t ulen) |
| 23 |
|
| { |
| 24 |
3848 |
| size_t i = 0, j = 0; |
| 25 |
5626 |
▶ | while (i < llen && j < ulen) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (i < llen && j < ulen) { |
3001 |
2625 |
5626 |
| while (i < llen && j < ulen) { |
2830 |
171 |
3001 |
|
| 26 |
2830 |
| unsigned int cu = u[j]; |
| 27 |
2830 |
▶ | if (cu < 0x80) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cu < 0x80) { |
2378 |
452 |
2830 |
|
| 28 |
2378 |
| j++; |
| 29 |
452 |
▶ | } else if ((cu == 0xC2 || cu == 0xC3) && j + 1 < ulen |
Branch: 3 branches
| Branch | True | False | Total |
| } else if ((cu == 0xC2 || cu == 0xC3) && j + 1 < ulen |
452 |
0 |
452 |
| } else if ((cu == 0xC2 || cu == 0xC3) && j + 1 < ulen |
341 |
111 |
452 |
| } else if ((cu == 0xC2 || cu == 0xC3) && j + 1 < ulen |
341 |
0 |
341 |
|
| 30 |
341 |
▶ | && (u[j + 1] & 0xC0) == 0x80) { |
Branch: 1 branch
| Branch | True | False | Total |
| && (u[j + 1] & 0xC0) == 0x80) { |
341 |
0 |
341 |
|
| 31 |
341 |
| cu = ((cu & 0x1Fu) << 6) | (u[j + 1] & 0x3Fu); |
| 32 |
341 |
| j += 2; |
| 33 |
|
| } else { |
| 34 |
111 |
| return -1; /* above latin-1, or malformed: l is less */ |
| 35 |
|
| } |
| 36 |
2719 |
▶ | if (l[i] != cu) |
Branch: 1 branch
| Branch | True | False | Total |
| if (l[i] != cu) |
941 |
1778 |
2719 |
|
| 37 |
941 |
▶ | return l[i] < cu ? -1 : 1; |
Branch: 1 branch
| Branch | True | False | Total |
| return l[i] < cu ? -1 : 1; |
494 |
447 |
941 |
|
| 38 |
1778 |
| i++; |
| 39 |
|
| } |
| 40 |
2796 |
▶ | if (i == llen && j == ulen) |
Branch: 2 branches
| Branch | True | False | Total |
| if (i == llen && j == ulen) |
2625 |
171 |
2796 |
| if (i == llen && j == ulen) |
1127 |
1498 |
2625 |
|
| 41 |
1127 |
| return 0; |
| 42 |
1669 |
▶ | return i < llen ? 1 : -1; |
Branch: 1 branch
| Branch | True | False | Total |
| return i < llen ? 1 : -1; |
171 |
1498 |
1669 |
|
| 43 |
|
| } |
| 44 |
|
| |
| 45 |
|
| /* Compare a[0..alen) and b[0..blen); a_utf8/b_utf8 say which form each is |
| 46 |
|
| * in. Returns <0, 0 or >0 (only the sign is meaningful). */ |
| 47 |
17065 |
| static int stencil_str_cmp(const char *a, size_t alen, int a_utf8, |
| 48 |
|
| const char *b, size_t blen, int b_utf8) |
| 49 |
|
| { |
| 50 |
17065 |
▶ | if (!a_utf8 == !b_utf8) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!a_utf8 == !b_utf8) { |
13217 |
3848 |
17065 |
|
| 51 |
13217 |
| size_t n = alen < blen ? alen : blen; |
| 52 |
13217 |
▶ | int r = n ? memcmp(a, b, n) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| int r = n ? memcmp(a, b, n) : 0; |
13206 |
11 |
13217 |
|
| 53 |
13217 |
▶ | if (r) |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) |
9900 |
3317 |
13217 |
|
| 54 |
9900 |
▶ | return r < 0 ? -1 : 1; |
Branch: 1 branch
| Branch | True | False | Total |
| return r < 0 ? -1 : 1; |
5066 |
4834 |
9900 |
|
| 55 |
3317 |
▶ | return alen < blen ? -1 : alen > blen ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return alen < blen ? -1 : alen > blen ? 1 : 0; |
2971 |
346 |
3317 |
|
| 56 |
|
| } |
| 57 |
3848 |
▶ | if (a_utf8) |
Branch: 1 branch
| Branch | True | False | Total |
| if (a_utf8) |
0 |
3848 |
3848 |
|
| 58 |
0 |
| return -stencil_str_cmp_l1_u8((const unsigned char *)b, blen, |
| 59 |
|
| (const unsigned char *)a, alen); |
| 60 |
3848 |
| return stencil_str_cmp_l1_u8((const unsigned char *)a, alen, |
| 61 |
|
| (const unsigned char *)b, blen); |
| 62 |
|
| } |
| 63 |
|
| |
| 64 |
|
| #endif /* STENCIL_STR_H */ |