From b75e44afb38bef99bc932942ff4ba4286b774507 Mon Sep 17 00:00:00 2001 From: default Date: Thu, 15 Feb 2024 17:44:28 +0100 Subject: [PATCH] Backport from xs. --- xs.h | 35 ++++++++++++----------------------- xs_unicode.h | 10 +++++++++- xs_version.h | 2 +- 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/xs.h b/xs.h index fc475ec..fb766ef 100644 --- a/xs.h +++ b/xs.h @@ -54,6 +54,7 @@ void _xs_destroy(char **var); xstype xs_type(const xs_val *data); int xs_size(const xs_val *data); int xs_is_null(const xs_val *data); +int xs_cmp(const xs_val *v1, const xs_val *v2); xs_val *xs_dup(const xs_val *data); xs_val *xs_expand(xs_val *data, int offset, int size); xs_val *xs_collapse(xs_val *data, int offset, int size); @@ -91,7 +92,6 @@ int xs_list_len(const xs_list *list); xs_val *xs_list_get(const xs_list *list, int num); xs_list *xs_list_del(xs_list *list, int num); xs_list *xs_list_insert(xs_list *list, int num, const xs_val *data); -xs_list *xs_list_insert_sorted(xs_list *list, const char *str); xs_list *xs_list_set(xs_list *list, int num, const xs_val *data); xs_list *xs_list_dequeue(xs_list *list, xs_val **data, int last); #define xs_list_pop(list, data) xs_list_dequeue(list, data, 1) @@ -350,6 +350,17 @@ int xs_is_null(const xs_val *data) } +int xs_cmp(const xs_val *v1, const xs_val *v2) +/* compares two values */ +{ + int s1 = xs_size(v1); + int s2 = xs_size(v2); + int d = s1 - s2; + + return d == 0 ? memcmp(v1, v2, s1) : d; +} + + xs_val *xs_dup(const xs_val *data) /* creates a duplicate of data */ { @@ -767,28 +778,6 @@ xs_list *xs_list_insert(xs_list *list, int num, const xs_val *data) } -xs_list *xs_list_insert_sorted(xs_list *list, const xs_str *str) -/* inserts a string in the list in its ordered position */ -{ - XS_ASSERT_TYPE(list, XSTYPE_LIST); - XS_ASSERT_TYPE(str, XSTYPE_STRING); - - char *p, *v; - int offset = xs_size(list); - - p = list; - while (xs_list_iter(&p, &v)) { - /* if this element is greater or equal, insert here */ - if (strcmp(v, str) >= 0) { - offset = v - list; - break; - } - } - - return _xs_list_write_litem(list, offset - 1, str, xs_size(str)); -} - - xs_list *xs_list_set(xs_list *list, int num, const xs_val *data) /* sets the element at #num position */ { diff --git a/xs_unicode.h b/xs_unicode.h index 036c843..47e1101 100644 --- a/xs_unicode.h +++ b/xs_unicode.h @@ -5,6 +5,7 @@ #define _XS_UNICODE_H int _xs_utf8_enc(char buf[4], unsigned int cpoint); + int xs_is_utf8_cont_byte(char c); unsigned int xs_utf8_dec(char **str); int xs_unicode_width(unsigned int cpoint); int xs_is_surrogate(unsigned int cpoint); @@ -58,6 +59,13 @@ int _xs_utf8_enc(char buf[4], unsigned int cpoint) } +int xs_is_utf8_cont_byte(char c) +/* returns true if c is an utf8 continuation byte */ +{ + return ((c & 0xc0) == 0x80); +} + + unsigned int xs_utf8_dec(char **str) /* decodes an utf-8 char inside str and updates the pointer */ { @@ -86,7 +94,7 @@ unsigned int xs_utf8_dec(char **str) } /* process the continuation bytes */ - while (cb > 0 && *p && (*p & 0xc0) == 0x80) + while (cb > 0 && *p && xs_is_utf8_cont_byte(*p)) cpoint |= (*p++ & 0x3f) << (--cb * 6); /* incomplete or broken? */ diff --git a/xs_version.h b/xs_version.h index c592d93..781e4ba 100644 --- a/xs_version.h +++ b/xs_version.h @@ -1 +1 @@ -/* 72caf9fc60c0b90f39a3c62b238d1c9663d109bc 2024-01-07T09:26:08+01:00 */ +/* e277e59e0ccbe2111aaf884c85ccadeebabd5281 2024-01-30T13:56:36+01:00 */