commit 8586e44
default
·
2024-08-23 15:22:10 +0000 UTC
parent d2daba7
Some optimizations.
3 files changed,
+120,
-5
+5,
-2
1@@ -208,6 +208,7 @@ static xs_val *_xs_json_load_lexer(FILE *f, js_type *t)
2 {
3 int c;
4 xs_val *v = NULL;
5+ int offset;
6
7 *t = JS_ERROR;
8
9@@ -236,6 +237,7 @@ static xs_val *_xs_json_load_lexer(FILE *f, js_type *t)
10 *t = JS_STRING;
11
12 v = xs_str_new(NULL);
13+ offset = 0;
14
15 while ((c = fgetc(f)) != '"' && c != EOF && *t != JS_ERROR) {
16 if (c == '\\') {
17@@ -274,11 +276,12 @@ static xs_val *_xs_json_load_lexer(FILE *f, js_type *t)
18 break;
19 }
20
21- v = xs_utf8_cat(v, cp);
22+ v = xs_utf8_insert(v, cp, &offset);
23 }
24 else {
25 char cc = c;
26- v = xs_append_m(v, &cc, 1);
27+ v = xs_insert_m(v, offset, &cc, 1);
28+ offset++;
29 }
30 }
31
+114,
-2
1@@ -9,6 +9,7 @@
2 unsigned int xs_utf8_dec(const char **str);
3 int xs_unicode_width(unsigned int cpoint);
4 int xs_is_surrogate(unsigned int cpoint);
5+ int xs_is_diacritic(unsigned int cpoint);
6 unsigned int xs_surrogate_dec(unsigned int p1, unsigned int p2);
7 unsigned int xs_surrogate_enc(unsigned int cpoint);
8 unsigned int *_xs_unicode_upper_search(unsigned int cpoint);
9@@ -22,7 +23,12 @@
10 int xs_unicode_is_alpha(unsigned int cpoint);
11
12 #ifdef _XS_H
13+ xs_str *xs_utf8_insert(xs_str *str, unsigned int cpoint, int *offset);
14 xs_str *xs_utf8_cat(xs_str *str, unsigned int cpoint);
15+ xs_str *xs_utf8_to_upper(const char *str);
16+ xs_str *xs_utf8_to_lower(const char *str);
17+ xs_str *xs_utf8_to_nfd(const char *str);
18+ xs_str *xs_utf8_to_nfc(const char *str);
19 #endif
20
21 #ifdef XS_IMPLEMENTATION
22@@ -144,6 +150,12 @@ int xs_unicode_width(unsigned int cpoint)
23 }
24
25
26+int xs_is_diacritic(unsigned int cpoint)
27+{
28+ return cpoint >= 0x300 && cpoint <= 0x36f;
29+}
30+
31+
32 /** surrogate pairs **/
33
34 int xs_is_surrogate(unsigned int cpoint)
35@@ -172,14 +184,27 @@ unsigned int xs_surrogate_enc(unsigned int cpoint)
36
37 #ifdef _XS_H
38
39-xs_str *xs_utf8_cat(xs_str *str, unsigned int cpoint)
40+xs_str *xs_utf8_insert(xs_str *str, unsigned int cpoint, int *offset)
41 /* encodes an Unicode codepoint to utf-8 into str */
42 {
43 char tmp[4];
44
45 int c = xs_utf8_enc(tmp, cpoint);
46
47- return xs_append_m(str, tmp, c);
48+ str = xs_insert_m(str, *offset, tmp, c);
49+
50+ *offset += c;
51+
52+ return str;
53+}
54+
55+
56+xs_str *xs_utf8_cat(xs_str *str, unsigned int cpoint)
57+/* encodes an Unicode codepoint to utf-8 into str */
58+{
59+ int offset = strlen(str);
60+
61+ return xs_utf8_insert(str, cpoint, &offset);
62 }
63
64 #endif /* _XS_H */
65@@ -232,6 +257,9 @@ unsigned int *_xs_unicode_lower_search(unsigned int cpoint)
66 unsigned int xs_unicode_to_lower(unsigned int cpoint)
67 /* returns the cpoint to lowercase */
68 {
69+ if (cpoint < 0x80)
70+ return tolower(cpoint);
71+
72 unsigned int *p = _xs_unicode_upper_search(cpoint);
73
74 return p == NULL ? cpoint : p[1];
75@@ -241,6 +269,9 @@ unsigned int xs_unicode_to_lower(unsigned int cpoint)
76 unsigned int xs_unicode_to_upper(unsigned int cpoint)
77 /* returns the cpoint to uppercase */
78 {
79+ if (cpoint < 0x80)
80+ return toupper(cpoint);
81+
82 unsigned int *p = _xs_unicode_lower_search(cpoint);
83
84 return p == NULL ? cpoint : p[0];
85@@ -317,6 +348,87 @@ int xs_unicode_is_alpha(unsigned int cpoint)
86 }
87
88
89+#ifdef _XS_H
90+
91+xs_str *xs_utf8_to_upper(const char *str)
92+{
93+ xs_str *s = xs_str_new(NULL);
94+ unsigned int cpoint;
95+ int offset = 0;
96+
97+ while ((cpoint = xs_utf8_dec(&str))) {
98+ cpoint = xs_unicode_to_upper(cpoint);
99+ s = xs_utf8_insert(s, cpoint, &offset);
100+ }
101+
102+ return s;
103+}
104+
105+
106+xs_str *xs_utf8_to_lower(const char *str)
107+{
108+ xs_str *s = xs_str_new(NULL);
109+ unsigned int cpoint;
110+ int offset = 0;
111+
112+ while ((cpoint = xs_utf8_dec(&str))) {
113+ cpoint = xs_unicode_to_lower(cpoint);
114+ s = xs_utf8_insert(s, cpoint, &offset);
115+ }
116+
117+ return s;
118+}
119+
120+
121+xs_str *xs_utf8_to_nfd(const char *str)
122+{
123+ xs_str *s = xs_str_new(NULL);
124+ unsigned int cpoint;
125+ int offset = 0;
126+
127+ while ((cpoint = xs_utf8_dec(&str))) {
128+ unsigned int base;
129+ unsigned int diac;
130+
131+ if (xs_unicode_nfd(cpoint, &base, &diac)) {
132+ s = xs_utf8_insert(s, base, &offset);
133+ s = xs_utf8_insert(s, diac, &offset);
134+ }
135+ else
136+ s = xs_utf8_insert(s, cpoint, &offset);
137+ }
138+
139+ return s;
140+}
141+
142+
143+xs_str *xs_utf8_to_nfc(const char *str)
144+{
145+ xs_str *s = xs_str_new(NULL);
146+ unsigned int cpoint;
147+ unsigned int base = 0;
148+ int offset = 0;
149+
150+ while ((cpoint = xs_utf8_dec(&str))) {
151+ if (xs_is_diacritic(cpoint)) {
152+ if (xs_unicode_nfc(base, cpoint, &base))
153+ continue;
154+ }
155+
156+ if (base)
157+ s = xs_utf8_insert(s, base, &offset);
158+
159+ base = cpoint;
160+ }
161+
162+ if (base)
163+ s = xs_utf8_insert(s, base, &offset);
164+
165+ return s;
166+}
167+
168+#endif /* _XS_H */
169+
170 #endif /* _XS_UNICODE_TBL_H */
171
172 #endif /* XS_IMPLEMENTATION */
+1,
-1
1@@ -1 +1 @@
2-/* c6eca9593f9b3d6791cba600e5950f682fdb36cf 2024-08-12T16:08:37+02:00 */
3+/* cc9ebd36ae640e4701277327fbba9996143076f6 2024-08-23T17:17:08+02:00 */