dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 100.0% 18 / 0 / 18
Functions: 100.0% 9 / 0 / 9
Branches: 100.0% 2 / 0 / 2

src/util/unicode.h
Line Branch Exec Source
1 #ifndef UTIL_UNICODE_H
2 #define UTIL_UNICODE_H
3
4 #include <stdbool.h>
5 #include <stdint.h>
6 #include "macros.h"
7 #include "numtostr.h"
8 #include "str-util.h"
9
10 // Work around some musl-targeted toolchains failing to include this
11 // header automatically and thus failing to define __STDC_ISO_10646__
12 #if HAS_INCLUDE(<stdc-predef.h>)
13 # include <stdc-predef.h> // NOLINT(portability-restrict-system-includes)
14 #endif
15
16 #if defined(WINT_MAX) && (WINT_MAX >= 0x10FFFFUL) && defined(__STDC_ISO_10646__)
17 # define SANE_WCTYPE 1
18 #endif
19
20 // The maximum Unicode codepoint allowed by RFC 3629
21 #define UNICODE_MAX_VALID_CODEPOINT (0x10FFFFUL)
22 #define CODEPOINT_STR_BUFSIZE (sizeof("U+10FFFF"))
23
24 typedef uint32_t CodePoint;
25
26 1886 static inline bool u_is_unicode(CodePoint u)
27 {
28 1886 return u <= UNICODE_MAX_VALID_CODEPOINT;
29 }
30
31 7 static inline size_t u_codepoint_to_str(CodePoint u, char buf[static CODEPOINT_STR_BUFSIZE])
32 {
33
2/2
✓ Branch 2 → 3 taken 2 times.
✓ Branch 2 → 5 taken 5 times.
7 if (!u_is_unicode(u)) {
34 2 return copyliteral(buf, "Invalid\0") - 1;
35 }
36
37 5 size_t i = copyliteral(buf, "U+");
38 5 i += buf_umax_to_hex_str(u, buf + i, 4);
39 5 return i;
40 }
41
42 // https://www.unicode.org/versions/latest/core-spec/chapter-3/#G2630
43 45 static inline bool u_is_surrogate(CodePoint u)
44 {
45 45 return (u >= 0xD800 && u <= 0xDFFF);
46 }
47
48 830 static inline bool u_is_cntrl(CodePoint u)
49 {
50 830 return (u < 0x20) || (u >= 0x7F && u <= 0x9F);
51 }
52
53 1099 static inline bool u_is_ascii_upper(CodePoint u)
54 {
55 1099 return u - 'A' < 26;
56 }
57
58 #ifdef SANE_WCTYPE
59 #include <wctype.h> // NOLINT(portability-restrict-system-includes)
60 // NOLINTBEGIN(*-unsafe-functions)
61 14 static inline bool u_is_lower(CodePoint u) {return iswlower((wint_t)u);}
62 18 static inline bool u_is_upper(CodePoint u) {return iswupper((wint_t)u);}
63 7 static inline CodePoint u_to_lower(CodePoint u) {return towlower((wint_t)u);}
64 9 static inline CodePoint u_to_upper(CodePoint u) {return towupper((wint_t)u);}
65 // NOLINTEND(*-unsafe-functions)
66 #else
67 static inline bool u_is_lower(CodePoint u) {return (u - 'a') < 26;}
68 static inline bool u_is_upper(CodePoint u) {return (u - 'A') < 26;}
69 static inline CodePoint u_to_lower(CodePoint u) {return u_is_upper(u) ? u + 32 : u;}
70 static inline CodePoint u_to_upper(CodePoint u) {return u_is_lower(u) ? u - 32 : u;}
71 #endif
72
73 bool u_is_breakable_whitespace(CodePoint u) CONST_FN;
74 bool u_is_word_char(CodePoint u) CONST_FN;
75 bool u_is_unprintable(CodePoint u);
76 bool u_is_special_whitespace(CodePoint u) CONST_FN;
77 bool u_is_zero_width(CodePoint u);
78 unsigned int u_char_width(CodePoint uch) CONST_FN;
79
80 #endif
81