summaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-03-24 23:25:16 +0100
committerTyge Løvset <[email protected]>2022-03-24 23:25:16 +0100
commit233e354798d19dbac03ee0ff9c5e366bfefead6c (patch)
tree153f8c3c3e0a0193af828cba81d2f7a16e6eb442 /include
parent0468971b404cee5582d360d7d1c66bb4148e1614 (diff)
downloadSTC-modified-233e354798d19dbac03ee0ff9c5e366bfefead6c.tar.gz
STC-modified-233e354798d19dbac03ee0ff9c5e366bfefead6c.zip
Misc. small internal API renaming/refactoring and additions.
Diffstat (limited to 'include')
-rw-r--r--include/stc/carc.h7
-rw-r--r--include/stc/cbox.h13
-rw-r--r--include/stc/ccommon.h44
-rw-r--r--include/stc/cmap.h4
-rw-r--r--include/stc/cregex.h9
-rw-r--r--include/stc/template.h34
-rw-r--r--include/stc/utf8.h20
7 files changed, 63 insertions, 68 deletions
diff --git a/include/stc/carc.h b/include/stc/carc.h
index 7ea1eb8e..76535db8 100644
--- a/include/stc/carc.h
+++ b/include/stc/carc.h
@@ -104,7 +104,7 @@ _cx_memb(_from_ptr)(_cx_value* p) {
}
STC_INLINE _cx_self
-_cx_memb(_from)(i_val val) {
+_cx_memb(_from)(i_val val) { // c++: std::make_shared<i_val>(val)
_cx_self ptr; _cx_carc_rep *rep = c_alloc(_cx_carc_rep);
*(ptr.use_count = &rep->counter) = 1;
*(ptr.get = &rep->value) = val;
@@ -145,9 +145,8 @@ _cx_memb(_reset_from)(_cx_self* self, i_val val) {
}
#if !c_option(c_no_clone) && !defined _i_no_raw
- STC_INLINE _cx_self _cx_memb(_new)(_cx_raw raw) {
- return _cx_memb(_from)(i_valfrom(raw));
- }
+ STC_INLINE _cx_self
+ _cx_memb(_new)(_cx_raw raw) { return _cx_memb(_from)(i_valfrom(raw)); }
#endif
// does not use i_valfrom, so we can bypass c_no_clone
diff --git a/include/stc/cbox.h b/include/stc/cbox.h
index 7b1cf377..fe4bca9d 100644
--- a/include/stc/cbox.h
+++ b/include/stc/cbox.h
@@ -85,14 +85,13 @@ STC_INLINE _cx_self
_cx_memb(_from_ptr)(i_val* p) { return c_make(_cx_self){p}; }
STC_INLINE _cx_self
-_cx_memb(_from)(i_val val) {
- return c_make(_cx_self){c_new(i_val, val)};
+_cx_memb(_from)(i_val val) { // c++: std::make_unique<i_val>(val)
+ _cx_self ptr = {c_alloc(i_val)};
+ *ptr.get = val; return ptr;
}
STC_INLINE i_val
-_cx_memb(_toraw)(const _cx_self* self) {
- return *self->get;
-}
+_cx_memb(_toraw)(const _cx_self* self) { return *self->get; }
// destructor
STC_INLINE void
@@ -121,9 +120,7 @@ _cx_memb(_reset_from)(_cx_self* self, i_val val) {
#if !c_option(c_no_clone)
#if !defined _i_no_raw
STC_INLINE _cx_self
- _cx_memb(_new)(_cx_raw raw) {
- return c_make(_cx_self){c_new(i_val, i_valfrom(raw))};
- }
+ _cx_memb(_new)(_cx_raw raw) { return _cx_memb(_from)(i_valfrom(raw)); }
#endif
STC_INLINE _cx_self
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h
index f3939b16..0838fbf3 100644
--- a/include/stc/ccommon.h
+++ b/include/stc/ccommon.h
@@ -42,19 +42,19 @@
/* Macro overloading feature support based on: https://rextester.com/ONP80107 */
#define c_MACRO_OVERLOAD(name, ...) \
- c_PASTE(name, c_NUM_ARGS(__VA_ARGS__))(__VA_ARGS__)
-#define c_CONCAT(a, b) a ## b
-#define c_PASTE(a, b) c_CONCAT(a, b)
-#define c_EXPAND(...) __VA_ARGS__
-#define c_NUM_ARGS(...) _c_APPLY_ARG_N((__VA_ARGS__, _c_RSEQ_N))
+ c_paste(name, c_numargs(__VA_ARGS__))(__VA_ARGS__)
+#define c_concat(a, b) a ## b
+#define c_paste(a, b) c_concat(a, b)
+#define c_expand(...) __VA_ARGS__
+#define c_numargs(...) _c_APPLY_ARG_N((__VA_ARGS__, _c_RSEQ_N))
-#define _c_APPLY_ARG_N(args) c_EXPAND(_c_ARG_N args)
+#define _c_APPLY_ARG_N(args) c_expand(_c_ARG_N args)
#define _c_RSEQ_N 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
#define _c_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, \
_14, _15, _16, N, ...) N
#define c_static_assert(cond) \
- typedef char c_PASTE(_static_assert_line_, __LINE__)[(cond) ? 1 : -1]
+ typedef char c_paste(_static_assert_line_, __LINE__)[(cond) ? 1 : -1]
#define c_container_of(ptr, type, member) \
((type *)((char *)(ptr) - offsetof(type, member)))
@@ -166,23 +166,25 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle, size_t slen, cons
for (type i=start, _c_inc=step, _c_end=(stop) - (0 < _c_inc) \
; (i <= _c_end) == (0 < _c_inc); i += _c_inc)
-#define c_autovar(declvar, ...) for (declvar, **_c_ii = NULL; !_c_ii; ++_c_ii, __VA_ARGS__)
-#define c_autoscope(init, ...) for (int _c_ii = (init, 0); !_c_ii; ++_c_ii, __VA_ARGS__)
+#define c_autovar(...) c_MACRO_OVERLOAD(c_autovar, __VA_ARGS__)
+#define c_autovar2(declvar, drop) for (declvar, **_c_ii = NULL; !_c_ii; ++_c_ii, drop)
+#define c_autovar3(declvar, cond, drop) for (declvar, **_c_ii = NULL; !_c_ii && (cond); ++_c_ii, drop)
+#define c_autoscope(init, drop) for (int _c_ii = (init, 0); !_c_ii; ++_c_ii, drop)
#define c_autodefer(...) for (int _c_ii = 0; !_c_ii; ++_c_ii, __VA_ARGS__)
#define c_breakauto continue
#define c_auto(...) c_MACRO_OVERLOAD(c_auto, __VA_ARGS__)
#define c_auto2(C, a) \
- c_autovar(C a = C##_init(), C##_drop(&a))
+ c_autovar2(C a = C##_init(), C##_drop(&a))
#define c_auto3(C, a, b) \
- c_autovar(c_EXPAND(C a = C##_init(), b = C##_init()), \
- C##_drop(&b), C##_drop(&a))
+ c_autovar2(c_expand(C a = C##_init(), b = C##_init()), \
+ (C##_drop(&b), C##_drop(&a)))
#define c_auto4(C, a, b, c) \
- c_autovar(c_EXPAND(C a = C##_init(), b = C##_init(), c = C##_init()), \
- C##_drop(&c), C##_drop(&b), C##_drop(&a))
+ c_autovar2(c_expand(C a = C##_init(), b = C##_init(), c = C##_init()), \
+ (C##_drop(&c), C##_drop(&b), C##_drop(&a)))
#define c_auto5(C, a, b, c, d) \
- c_autovar(c_EXPAND(C a = C##_init(), b = C##_init(), c = C##_init(), d = C##_init()), \
- C##_drop(&d), C##_drop(&c), C##_drop(&b), C##_drop(&a))
+ c_autovar2(c_expand(C a = C##_init(), b = C##_init(), c = C##_init(), d = C##_init()), \
+ (C##_drop(&d), C##_drop(&c), C##_drop(&b), C##_drop(&a)))
#define c_autobuf(b, type, n) c_autobuf_N(b, type, n, 256)
#define c_autobuf_N(b, type, n, BYTES) \
@@ -191,14 +193,16 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle, size_t slen, cons
; b; b != _c_b ? c_free(b) : (void)0, b = NULL)
#define c_apply(v, method, T, ...) do { \
- T _c_arr[] = __VA_ARGS__; \
+ typedef T _c_T; \
+ _c_T _c_arr[] = __VA_ARGS__; \
for (size_t index = 0; index < c_arraylen(_c_arr); ++index) \
- { T v = _c_arr[index]; method; } \
+ { _c_T v = _c_arr[index]; method; } \
} while (0)
#define c_apply_arr(v, method, T, arr, n) do { \
- T* _c_arr = arr; size_t _n = n; \
+ typedef T _c_T; \
+ _c_T* _c_arr = arr; size_t _n = n; \
for (size_t index = 0; index < _n; ++index) \
- { T v = _c_arr[index]; method; } \
+ { _c_T v = _c_arr[index]; method; } \
} while (0)
#define c_apply_cnt(v, method, C, ...) do { \
size_t index = 0; \
diff --git a/include/stc/cmap.h b/include/stc/cmap.h
index a5ca353e..cac11234 100644
--- a/include/stc/cmap.h
+++ b/include/stc/cmap.h
@@ -289,7 +289,7 @@ STC_DEF chash_bucket_t
_cx_memb(_bucket_)(const _cx_self* self, const _cx_rawkey* rkeyptr) {
const uint64_t _hash = i_hash(rkeyptr, sizeof *rkeyptr);
uint8_t _hx; _cx_size _cap = self->bucket_count;
- chash_bucket_t b = {c_PASTE(fastrange_,i_size)(_hash, _cap), (uint8_t)(_hash | 0x80)};
+ chash_bucket_t b = {c_paste(fastrange_,i_size)(_hash, _cap), (uint8_t)(_hash | 0x80)};
const uint8_t* _hashx = self->_hashx;
while ((_hx = _hashx[b.idx])) {
if (_hx == b.hx) {
@@ -373,7 +373,7 @@ _cx_memb(_erase_entry)(_cx_self* self, _cx_value* _val) {
if (! _hashx[j])
break;
_cx_rawkey _raw = i_keyto(_i_keyref(_slot + j));
- k = c_PASTE(fastrange_,i_size)(i_hash(&_raw, sizeof _raw), _cap);
+ k = c_paste(fastrange_,i_size)(i_hash(&_raw, sizeof _raw), _cap);
if ((j < i) ^ (k <= i) ^ (k > j)) /* is k outside (i, j]? */
_slot[i] = _slot[j], _hashx[i] = _hashx[j], i = j;
}
diff --git a/include/stc/cregex.h b/include/stc/cregex.h
index 4b9bde51..7357d9d9 100644
--- a/include/stc/cregex.h
+++ b/include/stc/cregex.h
@@ -31,9 +31,7 @@ THE SOFTWARE.
* This is a extended version of regexp9, supporting UTF8 input, common
* shorthand character classes, ++.
*/
-
-#include <stddef.h>
-#include <stdint.h>
+#include "csview.h"
typedef enum {
creg_nomatch = -1,
@@ -68,10 +66,7 @@ typedef struct {
struct Reprog* prog;
} cregex;
-typedef struct {
- const char* str;
- size_t len;
-} cregmatch;
+typedef csview cregmatch;
static inline cregex cregex_init(void) {
cregex rx = {NULL}; return rx;
diff --git a/include/stc/template.h b/include/stc/template.h
index 12c015f6..dc6ac908 100644
--- a/include/stc/template.h
+++ b/include/stc/template.h
@@ -25,9 +25,9 @@
#ifndef STC_TEMPLATE_H_INCLUDED
#define STC_TEMPLATE_H_INCLUDED
- #define _cx_self c_PASTE(_i_prefix, i_tag)
- #define _cx_memb(name) c_PASTE(_cx_self, name)
- #define _cx_deftypes(macro, SELF, ...) c_EXPAND(macro(SELF, __VA_ARGS__))
+ #define _cx_self c_paste(_i_prefix, i_tag)
+ #define _cx_memb(name) c_paste(_cx_self, name)
+ #define _cx_deftypes(macro, SELF, ...) c_expand(macro(SELF, __VA_ARGS__))
#define _cx_value _cx_memb(_value)
#define _cx_key _cx_memb(_key)
#define _cx_mapped _cx_memb(_mapped)
@@ -68,34 +68,34 @@
#endif
#elif defined i_key_arcbox
#define i_key_bind i_key_arcbox
- #define i_keyraw c_PASTE(i_key_arcbox, _value)
+ #define i_keyraw c_paste(i_key_arcbox, _value)
#endif
#ifdef i_key_bind
#define i_key i_key_bind
#ifndef i_keyraw
#ifndef i_keyfrom
- #define i_keyfrom c_PASTE(i_key, _clone)
+ #define i_keyfrom c_paste(i_key, _clone)
#endif
#else
#ifndef i_keyfrom
- #define i_keyfrom c_PASTE(i_key, _from)
+ #define i_keyfrom c_paste(i_key, _from)
#endif
#ifndef i_keyto
- #define i_keyto c_PASTE(i_key, _toraw)
+ #define i_keyto c_paste(i_key, _toraw)
#endif
#endif
#ifndef i_cmp
- #define i_cmp c_PASTE(i_keyraw, _cmp)
+ #define i_cmp c_paste(i_keyraw, _cmp)
#endif
#ifndef i_eq
- #define i_eq c_PASTE(i_keyraw, _eq)
+ #define i_eq c_paste(i_keyraw, _eq)
#endif
#ifndef i_hash
- #define i_hash c_PASTE(i_keyraw, _hash)
+ #define i_hash c_paste(i_keyraw, _hash)
#endif
#ifndef i_keydrop
- #define i_keydrop c_PASTE(i_key, _drop)
+ #define i_keydrop c_paste(i_key, _drop)
#endif
#endif
@@ -127,28 +127,28 @@
#endif
#elif defined i_val_arcbox
#define i_val_bind i_val_arcbox
- #define i_valraw c_PASTE(i_val_arcbox, _value)
+ #define i_valraw c_paste(i_val_arcbox, _value)
#endif
#ifdef i_val_bind
#define i_val i_val_bind
#ifndef i_valraw
#ifndef i_valfrom
- #define i_valfrom c_PASTE(i_val, _clone)
+ #define i_valfrom c_paste(i_val, _clone)
#endif
#else
#ifndef i_valfrom
- #define i_valfrom c_PASTE(i_val, _from)
+ #define i_valfrom c_paste(i_val, _from)
#endif
#ifndef i_valto
- #define i_valto c_PASTE(i_val, _toraw)
+ #define i_valto c_paste(i_val, _toraw)
#endif
#endif
#if !defined i_cmp && !defined i_key
- #define i_cmp c_PASTE(i_valraw, _cmp)
+ #define i_cmp c_paste(i_valraw, _cmp)
#endif
#ifndef i_valdrop
- #define i_valdrop c_PASTE(i_val, _drop)
+ #define i_valdrop c_paste(i_val, _drop)
#endif
#endif
diff --git a/include/stc/utf8.h b/include/stc/utf8.h
index 0a1ba161..7c4fdfc7 100644
--- a/include/stc/utf8.h
+++ b/include/stc/utf8.h
@@ -28,7 +28,7 @@ int main()
/* number of codepoints in the utf8 string s, or SIZE_MAX if invalid utf8: */
enum { UTF8_OK = 0, UTF8_ERROR = 4 };
-typedef struct { uint32_t state, codep, len; } utf8_decode_t;
+typedef struct { uint32_t state, codep, size; } utf8_decode_t;
/* decode next utf8 codepoint. */
STC_API size_t utf8_encode(char *out, uint32_t c);
@@ -53,7 +53,7 @@ STC_INLINE uint32_t utf8_peek(const char *s) {
STC_INLINE size_t utf8_codep_size(const char *s) {
utf8_decode_t d = {UTF8_OK, 0};
utf8_next(&d, (const uint8_t*)s);
- return d.len;
+ return d.size;
}
// --------------------------- IMPLEMENTATION ---------------------------------
@@ -65,19 +65,19 @@ STC_DEF uint32_t utf8_decode(utf8_decode_t *d, const uint8_t b)
{
switch (d->state) {
case UTF8_OK:
- if (b < 0x80) d->codep = b, d->len = 1;
- else if (b < 0xC2) d->state = UTF8_ERROR, d->len = 0;
- else if (b < 0xE0) d->state = 1, d->codep = b & 0x1F, d->len = 2;
- else if (b < 0xF0) d->state = 2, d->codep = b & 0x0F, d->len = 3;
- else if (b < 0xF5) d->state = 3, d->codep = b & 0x07, d->len = 4;
- else d->state = UTF8_ERROR, d->len = 0;
+ if (b < 0x80) d->codep = b, d->size = 1;
+ else if (b < 0xC2) d->state = UTF8_ERROR, d->size = 0;
+ else if (b < 0xE0) d->state = 1, d->codep = b & 0x1F, d->size = 2;
+ else if (b < 0xF0) d->state = 2, d->codep = b & 0x0F, d->size = 3;
+ else if (b < 0xF5) d->state = 3, d->codep = b & 0x07, d->size = 4;
+ else d->state = UTF8_ERROR, d->size = 0;
break;
case 1: case 2: case 3:
if ((b & 0xC0) == 0x80) {
d->state -= 1;
d->codep = (d->codep << 6) | (b & 0x3F);
} else
- d->state = UTF8_ERROR, d->len = 0;
+ d->state = UTF8_ERROR, d->size = 0;
}
return d->state;
}
@@ -107,7 +107,7 @@ STC_DEF size_t utf8_encode(char *out, uint32_t c)
STC_DEF const uint8_t* utf8_next(utf8_decode_t *d, const uint8_t* u) {
utf8_decode(d, *u++);
- switch (d->len) {
+ switch (d->size) {
case 4: utf8_decode(d, *u++);
case 3: utf8_decode(d, *u++);
case 2: utf8_decode(d, *u++);