Backport from xs.

This commit is contained in:
default 2024-04-14 19:26:49 +02:00
parent 81cf1e21a6
commit 0275658a36
6 changed files with 32 additions and 24 deletions

4
xs.h
View File

@ -45,6 +45,10 @@ typedef char xs_data;
/* not really all, just very much */ /* not really all, just very much */
#define XS_ALL 0xfffffff #define XS_ALL 0xfffffff
#ifndef xs_countof
#define xs_countof(a) (sizeof((a)) / sizeof((*a)))
#endif
void *xs_free(void *ptr); void *xs_free(void *ptr);
void *_xs_realloc(void *ptr, size_t size, const char *file, int line, const char *func); void *_xs_realloc(void *ptr, size_t size, const char *file, int line, const char *func);
#define xs_realloc(ptr, size) _xs_realloc(ptr, size, __FILE__, __LINE__, __FUNCTION__) #define xs_realloc(ptr, size) _xs_realloc(ptr, size, __FILE__, __LINE__, __FUNCTION__)

View File

@ -328,7 +328,7 @@ static xs_val *_xs_json_load_lexer(FILE *f, js_type *t)
int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c) int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c)
/* loads the next scalar value from the JSON stream */ /* loads the next scalar value from the JSON stream */
/* if the value ahead is complex, value is NULL and pt is filled */ /* if the value ahead is compound, value is NULL and pt is set */
{ {
js_type t; js_type t;
@ -348,7 +348,7 @@ int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c)
} }
if (*value == NULL) { if (*value == NULL) {
/* possible complex type ahead */ /* possible compound type ahead */
if (t == JS_OBRACK) if (t == JS_OBRACK)
*pt = XSTYPE_LIST; *pt = XSTYPE_LIST;
else else
@ -365,7 +365,7 @@ int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c)
xs_list *xs_json_load_array(FILE *f) xs_list *xs_json_load_array(FILE *f)
/* loads a JSON array (after the initial OBRACK) */ /* loads a full JSON array (after the initial OBRACK) */
{ {
xstype t; xstype t;
xs_list *l = xs_list_new(); xs_list *l = xs_list_new();
@ -406,7 +406,7 @@ xs_list *xs_json_load_array(FILE *f)
int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, xstype *pt, int *c) int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, xstype *pt, int *c)
/* loads the next key and scalar value from the JSON stream */ /* loads the next key and scalar value from the JSON stream */
/* if the value ahead is complex, value is NULL and pt is filled */ /* if the value ahead is compound, value is NULL and pt is set */
{ {
js_type t; js_type t;
@ -453,7 +453,7 @@ int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, xstype *pt,
xs_dict *xs_json_load_object(FILE *f) xs_dict *xs_json_load_object(FILE *f)
/* loads a JSON object (after the initial OCURLY) */ /* loads a full JSON object (after the initial OCURLY) */
{ {
xstype t; xstype t;
xs_dict *d = xs_dict_new(); xs_dict *d = xs_dict_new();

View File

@ -55,19 +55,23 @@ const char *xs_mime_by_ext(const char *file)
const char *ext = strrchr(file, '.'); const char *ext = strrchr(file, '.');
if (ext) { if (ext) {
const char **p = xs_mime_types;
xs *uext = xs_tolower_i(xs_dup(ext + 1)); xs *uext = xs_tolower_i(xs_dup(ext + 1));
int b = 0;
int t = xs_countof(xs_mime_types) / 2 - 2;
while (*p) { while (t >= b) {
int c; int n = (b + t) / 2;
const char *p = xs_mime_types[n * 2];
if ((c = strcmp(*p, uext)) == 0) int c = strcmp(uext, p);
return p[1];
if (c < 0)
t = n - 1;
else else
if (c > 0) if (c > 0)
break; b = n + 1;
else
p += 2; return xs_mime_types[(n * 2) + 1];
} }
} }

View File

@ -27,8 +27,8 @@
#ifdef XS_IMPLEMENTATION #ifdef XS_IMPLEMENTATION
#ifndef countof #ifndef xs_countof
#define countof(a) (sizeof((a)) / sizeof((*a))) #define xs_countof(a) (sizeof((a)) / sizeof((*a)))
#endif #endif
int _xs_utf8_enc(char buf[4], unsigned int cpoint) int _xs_utf8_enc(char buf[4], unsigned int cpoint)
@ -125,7 +125,7 @@ int xs_unicode_width(unsigned int cpoint)
/* returns the width in columns of a Unicode codepoint (somewhat simplified) */ /* returns the width in columns of a Unicode codepoint (somewhat simplified) */
{ {
int b = 0; int b = 0;
int t = countof(xs_unicode_width_table) / 3 - 1; int t = xs_countof(xs_unicode_width_table) / 3 - 1;
while (t >= b) { while (t >= b) {
int n = (b + t) / 2; int n = (b + t) / 2;
@ -193,7 +193,7 @@ unsigned int *_xs_unicode_upper_search(unsigned int cpoint)
/* searches for an uppercase codepoint in the case fold table */ /* searches for an uppercase codepoint in the case fold table */
{ {
int b = 0; int b = 0;
int t = countof(xs_unicode_case_fold_table) / 2 + 1; int t = xs_countof(xs_unicode_case_fold_table) / 2 + 1;
while (t >= b) { while (t >= b) {
int n = (b + t) / 2; int n = (b + t) / 2;
@ -216,7 +216,7 @@ unsigned int *_xs_unicode_lower_search(unsigned int cpoint)
/* searches for a lowercase codepoint in the case fold table */ /* searches for a lowercase codepoint in the case fold table */
{ {
unsigned int *p = xs_unicode_case_fold_table; unsigned int *p = xs_unicode_case_fold_table;
unsigned int *e = p + countof(xs_unicode_case_fold_table); unsigned int *e = p + xs_countof(xs_unicode_case_fold_table);
while (p < e) { while (p < e) {
if (cpoint == p[1]) if (cpoint == p[1])
@ -251,7 +251,7 @@ int xs_unicode_nfd(unsigned int cpoint, unsigned int *base, unsigned int *diac)
/* applies unicode Normalization Form D */ /* applies unicode Normalization Form D */
{ {
int b = 0; int b = 0;
int t = countof(xs_unicode_nfd_table) / 3 - 1; int t = xs_countof(xs_unicode_nfd_table) / 3 - 1;
while (t >= b) { while (t >= b) {
int n = (b + t) / 2; int n = (b + t) / 2;
@ -279,7 +279,7 @@ int xs_unicode_nfc(unsigned int base, unsigned int diac, unsigned int *cpoint)
/* applies unicode Normalization Form C */ /* applies unicode Normalization Form C */
{ {
unsigned int *p = xs_unicode_nfd_table; unsigned int *p = xs_unicode_nfd_table;
unsigned int *e = p + countof(xs_unicode_nfd_table); unsigned int *e = p + xs_countof(xs_unicode_nfd_table);
while (p < e) { while (p < e) {
if (p[1] == base && p[2] == diac) { if (p[1] == base && p[2] == diac) {
@ -298,7 +298,7 @@ int xs_unicode_is_alpha(unsigned int cpoint)
/* checks if a codepoint is an alpha (i.e. a letter) */ /* checks if a codepoint is an alpha (i.e. a letter) */
{ {
int b = 0; int b = 0;
int t = countof(xs_unicode_alpha_table) / 2 - 1; int t = xs_countof(xs_unicode_alpha_table) / 2 - 1;
while (t >= b) { while (t >= b) {
int n = (b + t) / 2; int n = (b + t) / 2;

View File

@ -56,7 +56,7 @@ xs_dict *xs_url_vars(const char *str)
l = args; l = args;
while (xs_list_iter(&l, &v)) { while (xs_list_iter(&l, &v)) {
xs *kv = xs_split_n(v, "=", 2); xs *kv = xs_split_n(v, "=", 1);
if (xs_list_len(kv) == 2) { if (xs_list_len(kv) == 2) {
const char *key = xs_list_get(kv, 0); const char *key = xs_list_get(kv, 0);

View File

@ -1 +1 @@
/* 0df383371d207b0adfda40912ee54b37e5b01152 2024-03-15T03:45:39+01:00 */ /* f712d1336ef427c3b56305364b2687578537543f 2024-04-14T19:11:53+02:00 */