diff options
| author | 2023-09-13 18:19:19 +0200 | |
|---|---|---|
| committer | 2023-09-13 18:19:19 +0200 | |
| commit | 4105e737e6ec9b9d8aca508ac5bedb69947b202d (patch) | |
| tree | f90686ed12ef4ea2b50e65ddeee42bf3650144eb /xs_unicode.h | |
| parent | Updated RELEASE_NOTES. (diff) | |
| download | penes-snac2-4105e737e6ec9b9d8aca508ac5bedb69947b202d.tar.gz penes-snac2-4105e737e6ec9b9d8aca508ac5bedb69947b202d.tar.xz penes-snac2-4105e737e6ec9b9d8aca508ac5bedb69947b202d.zip | |
Backport from xs.
Diffstat (limited to 'xs_unicode.h')
| -rw-r--r-- | xs_unicode.h | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/xs_unicode.h b/xs_unicode.h index 35cd9f7..c7d6190 100644 --- a/xs_unicode.h +++ b/xs_unicode.h | |||
| @@ -16,6 +16,7 @@ | |||
| 16 | unsigned int xs_unicode_to_lower(unsigned int cpoint); | 16 | unsigned int xs_unicode_to_lower(unsigned int cpoint); |
| 17 | int xs_unicode_nfd(unsigned int cpoint, unsigned int *base, unsigned int *diac); | 17 | int xs_unicode_nfd(unsigned int cpoint, unsigned int *base, unsigned int *diac); |
| 18 | int xs_unicode_nfc(unsigned int base, unsigned int diac, unsigned int *cpoint); | 18 | int xs_unicode_nfc(unsigned int base, unsigned int diac, unsigned int *cpoint); |
| 19 | int xs_unicode_is_alpha(unsigned int cpoint); | ||
| 19 | 20 | ||
| 20 | #ifdef XS_IMPLEMENTATION | 21 | #ifdef XS_IMPLEMENTATION |
| 21 | 22 | ||
| @@ -101,6 +102,15 @@ unsigned int xs_utf8_dec(char **str) | |||
| 101 | } | 102 | } |
| 102 | 103 | ||
| 103 | 104 | ||
| 105 | static int int_range_cmp(const void *p1, const void *p2) | ||
| 106 | { | ||
| 107 | const unsigned int *a = p1; | ||
| 108 | const unsigned int *b = p2; | ||
| 109 | |||
| 110 | return *a < b[0] ? -1 : *a > b[1] ? 1 : 0; | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 104 | /* intentionally dead simple */ | 114 | /* intentionally dead simple */ |
| 105 | 115 | ||
| 106 | static unsigned int xs_unicode_width_table[] = { | 116 | static unsigned int xs_unicode_width_table[] = { |
| @@ -119,20 +129,12 @@ static unsigned int xs_unicode_width_table[] = { | |||
| 119 | int xs_unicode_width(unsigned int cpoint) | 129 | int xs_unicode_width(unsigned int cpoint) |
| 120 | /* returns the width in columns of a Unicode codepoint (somewhat simplified) */ | 130 | /* returns the width in columns of a Unicode codepoint (somewhat simplified) */ |
| 121 | { | 131 | { |
| 122 | unsigned int *p = xs_unicode_width_table; | 132 | unsigned int *r = bsearch(&cpoint, xs_unicode_width_table, |
| 123 | unsigned int *e = p + sizeof(xs_unicode_width_table) / sizeof(unsigned int); | 133 | sizeof(xs_unicode_width_table) / (sizeof(unsigned int) * 3), |
| 124 | 134 | sizeof(unsigned int) * 3, | |
| 125 | while (p < e) { | 135 | int_range_cmp); |
| 126 | if (cpoint < p[0]) | ||
| 127 | return 1; | ||
| 128 | |||
| 129 | if (cpoint >= p[0] && cpoint <= p[1]) | ||
| 130 | return p[2]; | ||
| 131 | |||
| 132 | p += 3; | ||
| 133 | } | ||
| 134 | 136 | ||
| 135 | return 0; | 137 | return r ? r[2] : 1; |
| 136 | } | 138 | } |
| 137 | 139 | ||
| 138 | 140 | ||
| @@ -232,6 +234,18 @@ int xs_unicode_nfc(unsigned int base, unsigned int diac, unsigned int *cpoint) | |||
| 232 | } | 234 | } |
| 233 | 235 | ||
| 234 | 236 | ||
| 237 | int xs_unicode_is_alpha(unsigned int cpoint) | ||
| 238 | /* checks if a codepoint is an alpha (i.e. a letter) */ | ||
| 239 | { | ||
| 240 | unsigned int *r = bsearch(&cpoint, xs_unicode_alpha_table, | ||
| 241 | sizeof(xs_unicode_alpha_table) / (sizeof(unsigned int) * 2), | ||
| 242 | sizeof(unsigned int) * 2, | ||
| 243 | int_range_cmp); | ||
| 244 | |||
| 245 | return !!r; | ||
| 246 | } | ||
| 247 | |||
| 248 | |||
| 235 | #endif /* _XS_UNICODE_TBL_H */ | 249 | #endif /* _XS_UNICODE_TBL_H */ |
| 236 | 250 | ||
| 237 | #endif /* XS_IMPLEMENTATION */ | 251 | #endif /* XS_IMPLEMENTATION */ |