commit 0275658
default
·
2024-04-14 17:26:49 +0000 UTC
parent 81cf1e2
Backport from xs.
6 files changed,
+32,
-24
M
xs.h
+4,
-0
1@@ -45,6 +45,10 @@ typedef char xs_data;
2 /* not really all, just very much */
3 #define XS_ALL 0xfffffff
4
5+#ifndef xs_countof
6+#define xs_countof(a) (sizeof((a)) / sizeof((*a)))
7+#endif
8+
9 void *xs_free(void *ptr);
10 void *_xs_realloc(void *ptr, size_t size, const char *file, int line, const char *func);
11 #define xs_realloc(ptr, size) _xs_realloc(ptr, size, __FILE__, __LINE__, __FUNCTION__)
+5,
-5
1@@ -328,7 +328,7 @@ static xs_val *_xs_json_load_lexer(FILE *f, js_type *t)
2
3 int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c)
4 /* loads the next scalar value from the JSON stream */
5-/* if the value ahead is complex, value is NULL and pt is filled */
6+/* if the value ahead is compound, value is NULL and pt is set */
7 {
8 js_type t;
9
10@@ -348,7 +348,7 @@ int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c)
11 }
12
13 if (*value == NULL) {
14- /* possible complex type ahead */
15+ /* possible compound type ahead */
16 if (t == JS_OBRACK)
17 *pt = XSTYPE_LIST;
18 else
19@@ -365,7 +365,7 @@ int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c)
20
21
22 xs_list *xs_json_load_array(FILE *f)
23-/* loads a JSON array (after the initial OBRACK) */
24+/* loads a full JSON array (after the initial OBRACK) */
25 {
26 xstype t;
27 xs_list *l = xs_list_new();
28@@ -406,7 +406,7 @@ xs_list *xs_json_load_array(FILE *f)
29
30 int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, xstype *pt, int *c)
31 /* loads the next key and scalar value from the JSON stream */
32-/* if the value ahead is complex, value is NULL and pt is filled */
33+/* if the value ahead is compound, value is NULL and pt is set */
34 {
35 js_type t;
36
37@@ -453,7 +453,7 @@ int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, xstype *pt,
38
39
40 xs_dict *xs_json_load_object(FILE *f)
41-/* loads a JSON object (after the initial OCURLY) */
42+/* loads a full JSON object (after the initial OCURLY) */
43 {
44 xstype t;
45 xs_dict *d = xs_dict_new();
+13,
-9
1@@ -55,19 +55,23 @@ const char *xs_mime_by_ext(const char *file)
2 const char *ext = strrchr(file, '.');
3
4 if (ext) {
5- const char **p = xs_mime_types;
6- xs *uext = xs_tolower_i(xs_dup(ext + 1));
7+ xs *uext = xs_tolower_i(xs_dup(ext + 1));
8+ int b = 0;
9+ int t = xs_countof(xs_mime_types) / 2 - 2;
10
11- while (*p) {
12- int c;
13+ while (t >= b) {
14+ int n = (b + t) / 2;
15+ const char *p = xs_mime_types[n * 2];
16
17- if ((c = strcmp(*p, uext)) == 0)
18- return p[1];
19+ int c = strcmp(uext, p);
20+
21+ if (c < 0)
22+ t = n - 1;
23 else
24 if (c > 0)
25- break;
26-
27- p += 2;
28+ b = n + 1;
29+ else
30+ return xs_mime_types[(n * 2) + 1];
31 }
32 }
33
+8,
-8
1@@ -27,8 +27,8 @@
2
3 #ifdef XS_IMPLEMENTATION
4
5-#ifndef countof
6-#define countof(a) (sizeof((a)) / sizeof((*a)))
7+#ifndef xs_countof
8+#define xs_countof(a) (sizeof((a)) / sizeof((*a)))
9 #endif
10
11 int _xs_utf8_enc(char buf[4], unsigned int cpoint)
12@@ -125,7 +125,7 @@ int xs_unicode_width(unsigned int cpoint)
13 /* returns the width in columns of a Unicode codepoint (somewhat simplified) */
14 {
15 int b = 0;
16- int t = countof(xs_unicode_width_table) / 3 - 1;
17+ int t = xs_countof(xs_unicode_width_table) / 3 - 1;
18
19 while (t >= b) {
20 int n = (b + t) / 2;
21@@ -193,7 +193,7 @@ unsigned int *_xs_unicode_upper_search(unsigned int cpoint)
22 /* searches for an uppercase codepoint in the case fold table */
23 {
24 int b = 0;
25- int t = countof(xs_unicode_case_fold_table) / 2 + 1;
26+ int t = xs_countof(xs_unicode_case_fold_table) / 2 + 1;
27
28 while (t >= b) {
29 int n = (b + t) / 2;
30@@ -216,7 +216,7 @@ unsigned int *_xs_unicode_lower_search(unsigned int cpoint)
31 /* searches for a lowercase codepoint in the case fold table */
32 {
33 unsigned int *p = xs_unicode_case_fold_table;
34- unsigned int *e = p + countof(xs_unicode_case_fold_table);
35+ unsigned int *e = p + xs_countof(xs_unicode_case_fold_table);
36
37 while (p < e) {
38 if (cpoint == p[1])
39@@ -251,7 +251,7 @@ int xs_unicode_nfd(unsigned int cpoint, unsigned int *base, unsigned int *diac)
40 /* applies unicode Normalization Form D */
41 {
42 int b = 0;
43- int t = countof(xs_unicode_nfd_table) / 3 - 1;
44+ int t = xs_countof(xs_unicode_nfd_table) / 3 - 1;
45
46 while (t >= b) {
47 int n = (b + t) / 2;
48@@ -279,7 +279,7 @@ int xs_unicode_nfc(unsigned int base, unsigned int diac, unsigned int *cpoint)
49 /* applies unicode Normalization Form C */
50 {
51 unsigned int *p = xs_unicode_nfd_table;
52- unsigned int *e = p + countof(xs_unicode_nfd_table);
53+ unsigned int *e = p + xs_countof(xs_unicode_nfd_table);
54
55 while (p < e) {
56 if (p[1] == base && p[2] == diac) {
57@@ -298,7 +298,7 @@ int xs_unicode_is_alpha(unsigned int cpoint)
58 /* checks if a codepoint is an alpha (i.e. a letter) */
59 {
60 int b = 0;
61- int t = countof(xs_unicode_alpha_table) / 2 - 1;
62+ int t = xs_countof(xs_unicode_alpha_table) / 2 - 1;
63
64 while (t >= b) {
65 int n = (b + t) / 2;
M
xs_url.h
+1,
-1
1@@ -56,7 +56,7 @@ xs_dict *xs_url_vars(const char *str)
2
3 l = args;
4 while (xs_list_iter(&l, &v)) {
5- xs *kv = xs_split_n(v, "=", 2);
6+ xs *kv = xs_split_n(v, "=", 1);
7
8 if (xs_list_len(kv) == 2) {
9 const char *key = xs_list_get(kv, 0);
+1,
-1
1@@ -1 +1 @@
2-/* 0df383371d207b0adfda40912ee54b37e5b01152 2024-03-15T03:45:39+01:00 */
3+/* f712d1336ef427c3b56305364b2687578537543f 2024-04-14T19:11:53+02:00 */