summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-08-09 02:48:33 +0200
committerTyge Løvset <[email protected]>2022-08-09 02:48:33 +0200
commit2df3fb58bb21be5afcfa78fd0b5d1aee033ebe9e (patch)
tree48278f967e32afeca21f3791ba9cbc8d5e0cd348
parent0861b814130ab77b0e228e6f41a8c898959d69f1 (diff)
downloadSTC-modified-2df3fb58bb21be5afcfa78fd0b5d1aee033ebe9e.tar.gz
STC-modified-2df3fb58bb21be5afcfa78fd0b5d1aee033ebe9e.zip
Internal stuff.
-rw-r--r--include/stc/cstr.h13
-rw-r--r--include/stc/utf8.h6
-rw-r--r--src/utf8code.c35
3 files changed, 21 insertions, 33 deletions
diff --git a/include/stc/cstr.h b/include/stc/cstr.h
index 3231c09d..aae52ea0 100644
--- a/include/stc/cstr.h
+++ b/include/stc/cstr.h
@@ -172,9 +172,16 @@ STC_INLINE size_t cstr_capacity(cstr s)
// utf8 methods defined in/depending on src/utf8code.c:
-extern cstr cstr_casefold_sv(csview sv);
-extern cstr cstr_tolower_sv(csview sv);
-extern cstr cstr_toupper_sv(csview sv);
+extern cstr cstr_tocase(csview sv, int k);
+
+STC_INLINE cstr cstr_casefold_sv(csview sv)
+ { return cstr_tocase(sv, 0); }
+
+STC_INLINE cstr cstr_tolower_sv(csview sv)
+ { return cstr_tocase(sv, 1); }
+
+STC_INLINE cstr cstr_toupper_sv(csview sv)
+ { return cstr_tocase(sv, 2); }
STC_INLINE cstr cstr_tolower(const char* str)
{ return cstr_tolower_sv(c_sv(str, strlen(str))); }
diff --git a/include/stc/utf8.h b/include/stc/utf8.h
index 2dd51927..c20b80cb 100644
--- a/include/stc/utf8.h
+++ b/include/stc/utf8.h
@@ -5,8 +5,7 @@
#include <ctype.h>
// utf8 methods defined in src/utf8code.c:
-extern bool utf8_islower(uint32_t c);
-extern bool utf8_isupper(uint32_t c);
+
extern bool utf8_isspace(uint32_t c);
extern bool utf8_isdigit(uint32_t c);
extern bool utf8_isxdigit(uint32_t c);
@@ -20,6 +19,9 @@ extern int utf8_icmp_sv(csview s1, csview s2);
extern unsigned utf8_encode(char *out, uint32_t c);
extern uint32_t utf8_peek(const char *s, int u8pos);
+STC_INLINE bool utf8_isupper(uint32_t c) { return utf8_tolower(c) != c; }
+STC_INLINE bool utf8_islower(uint32_t c) { return utf8_toupper(c) != c; }
+
/* following functions uses src/utf8code.c */
/* decode next utf8 codepoint. https://bjoern.hoehrmann.de/utf-8/decoder/dfa */
diff --git a/src/utf8code.c b/src/utf8code.c
index ebd434ea..45f1613d 100644
--- a/src/utf8code.c
+++ b/src/utf8code.c
@@ -116,14 +116,6 @@ int utf8_icmp_sv(const csview s1, const csview s2) {
return s1.size - s2.size;
}
-bool utf8_isupper(uint32_t c) {
- return utf8_tolower(c) != c;
-}
-
-bool utf8_islower(uint32_t c) {
- return utf8_toupper(c) != c;
-}
-
bool utf8_isspace(uint32_t c) {
static uint16_t t[] = {0x20, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x85, 0xA0,
0x1680, 0x2028, 0x2029, 0x202F, 0x205F, 0x3000};
@@ -156,16 +148,15 @@ bool utf8_isalpha(uint32_t c) {
return utf8_islower(c) || utf8_isupper(c);
}
-static struct fncase {
+static struct {
int (*conv_asc)(int);
uint32_t (*conv_utf)(uint32_t);
}
-fn_tofold = {tolower, utf8_casefold},
-fn_tolower = {tolower, utf8_tolower},
-fn_toupper = {toupper, utf8_toupper};
-
+fn_tocase[] = {{tolower, utf8_casefold},
+ {tolower, utf8_tolower},
+ {toupper, utf8_toupper}};
-static cstr cstr_tocase(csview sv, struct fncase fn) {
+cstr cstr_tocase(csview sv, int k) {
cstr out = cstr_null;
char *buf = cstr_reserve(&out, sv.size*3/2);
uint32_t cp; size_t sz = 0;
@@ -174,9 +165,9 @@ static cstr cstr_tocase(csview sv, struct fncase fn) {
while (*sv.str) {
do { utf8_decode(&d, (uint8_t)*sv.str++); } while (d.state);
if (d.codep < 128)
- buf[sz++] = (char)fn.conv_asc(d.codep);
+ buf[sz++] = (char)fn_tocase[k].conv_asc(d.codep);
else {
- cp = fn.conv_utf(d.codep);
+ cp = fn_tocase[k].conv_utf(d.codep);
sz += utf8_encode(buf + sz, cp);
}
}
@@ -184,15 +175,3 @@ static cstr cstr_tocase(csview sv, struct fncase fn) {
cstr_shrink_to_fit(&out);
return out;
}
-
-cstr cstr_casefold_sv(csview sv) {
- return cstr_tocase(sv, fn_tofold);
-}
-
-cstr cstr_tolower_sv(csview sv) {
- return cstr_tocase(sv, fn_tolower);
-}
-
-cstr cstr_toupper_sv(csview sv) {
- return cstr_tocase(sv, fn_toupper);
-}