summaryrefslogtreecommitdiffhomepage
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
parent0468971b404cee5582d360d7d1c66bb4148e1614 (diff)
downloadSTC-modified-233e354798d19dbac03ee0ff9c5e366bfefead6c.tar.gz
STC-modified-233e354798d19dbac03ee0ff9c5e366bfefead6c.zip
Misc. small internal API renaming/refactoring and additions.
-rw-r--r--examples/regex2.c4
-rw-r--r--examples/regex_match.c5
-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
-rw-r--r--src/cregex.c20
10 files changed, 76 insertions, 84 deletions
diff --git a/examples/regex2.c b/examples/regex2.c
index d27b0406..1a8ac31d 100644
--- a/examples/regex2.c
+++ b/examples/regex2.c
@@ -1,5 +1,4 @@
#include <stc/cregex.h>
-#include <stc/csview.h>
#include <stc/cstr.h>
int main()
@@ -24,8 +23,7 @@ int main()
{
c_forrange (j, cregex_captures(re))
{
- csview cap = {m[j].str, m[j].len};
- printf(" submatch %" PRIuMAX ": " c_PRIsv "\n", j, c_ARGsv(cap));
+ printf(" submatch %" PRIuMAX ": " c_PRIsv "\n", j, c_ARGsv(m[j]));
}
puts("");
}
diff --git a/examples/regex_match.c b/examples/regex_match.c
index cc5bb483..899fc072 100644
--- a/examples/regex_match.c
+++ b/examples/regex_match.c
@@ -1,5 +1,4 @@
#include <stdio.h>
-#include <stc/csview.h>
#include <stc/cregex.h>
#include <stc/crandom.h>
#define i_val double
@@ -20,13 +19,13 @@ int main()
printf("%d\n", res);
cregmatch m[10];
if (cregex_find(&re, s, 10, m, 0) > 0) {
- printf("Found digits at position %" PRIuMAX "-%" PRIuMAX "\n", m[0].str - s, m[0].str - s + m[0].len);
+ printf("Found digits at position %" PRIuMAX "-%" PRIuMAX "\n", m[0].str - s, m[0].str - s + m[0].size);
} else {
printf("Could not find any digits\n");
}
while (cregex_find(&re, s, 10, m, creg_next) > 0) {
- printf("%.*s ; ", (int)m[0].len, m[0].str);
+ printf(c_PRIsv " ; ", c_ARGsv(m[0]));
}
puts("");
}
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++);
diff --git a/src/cregex.c b/src/cregex.c
index 2518736a..0f585f5d 100644
--- a/src/cregex.c
+++ b/src/cregex.c
@@ -177,13 +177,13 @@ chartorune(Rune *rune, const char *s)
utf8_decode_t ctx = {UTF8_OK};
const uint8_t *b = (const uint8_t*)s;
utf8_decode(&ctx, *b++);
- switch (ctx.len) {
+ switch (ctx.size) {
case 4: utf8_decode(&ctx, *b++);
case 3: utf8_decode(&ctx, *b++);
case 2: utf8_decode(&ctx, *b++);
}
*rune = ctx.codep;
- return ctx.len;
+ return ctx.size;
}
static const char*
@@ -230,7 +230,7 @@ _renewmatch(Resub *mp, int ms, Resublist *sp, int nsubids)
if (mp==NULL || ms<=0)
return;
if (mp[0].str == NULL || sp->m[0].str < mp[0].str ||
- (sp->m[0].str == mp[0].str && sp->m[0].len > mp[0].len)) {
+ (sp->m[0].str == mp[0].str && sp->m[0].size > mp[0].size)) {
for (i=0; i<ms && i<=nsubids; i++)
mp[i] = sp->m[i];
}
@@ -910,7 +910,7 @@ regexec1(const Reprog *progp, /* program to run */
if (mp)
for (i=0; i<ms; i++) {
mp[i].str = NULL;
- mp[i].len = 0;
+ mp[i].size = 0;
}
j->relist[0][0].inst = NULL;
j->relist[1][0].inst = NULL;
@@ -968,7 +968,7 @@ regexec1(const Reprog *progp, /* program to run */
tlp->se.m[inst->r.subid].str = s;
continue;
case RBRA:
- tlp->se.m[inst->r.subid].len = s - tlp->se.m[inst->r.subid].str;
+ tlp->se.m[inst->r.subid].size = s - tlp->se.m[inst->r.subid].str;
continue;
case ANY:
ok = (r != '\n');
@@ -1017,7 +1017,7 @@ regexec1(const Reprog *progp, /* program to run */
match = !(mflags & creg_fullmatch) ||
((s == j->eol || r == 0 || r == '\n') &&
(tlp->se.m[0].str == bol || tlp->se.m[0].str[-1] == '\n'));
- tlp->se.m[0].len = s - tlp->se.m[0].str;
+ tlp->se.m[0].size = s - tlp->se.m[0].str;
if (mp != NULL)
_renewmatch(mp, ms, &tlp->se, progp->nsubids);
break;
@@ -1082,9 +1082,9 @@ regexec9(const Reprog *progp, /* program to run */
if (mp && mp->str && ms>0) {
if (mflags & creg_startend)
- j.starts = mp->str, j.eol = mp->str + mp->len;
+ j.starts = mp->str, j.eol = mp->str + mp->size;
else if (mflags & creg_next)
- j.starts = mp->str + mp->len;
+ j.starts = mp->str + mp->size;
}
j.starttype = 0;
@@ -1133,7 +1133,7 @@ void cregex_replace(
case '5': case '6': case '7': case '8': case '9':
i = *sp - '0';
if (mp[i].str != NULL && mp != NULL && ms > i)
- for (ssp = mp[i].str; ssp < (mp[i].str + mp[i].len); ssp++)
+ for (ssp = mp[i].str; ssp < (mp[i].str + mp[i].size); ssp++)
if (dp < ep)
*dp++ = *ssp;
break;
@@ -1151,7 +1151,7 @@ void cregex_replace(
}
} else if (*sp == '&') {
if (mp[0].str != NULL && mp != NULL && ms > 0)
- for (ssp = mp[0].str; ssp < (mp[0].str + mp[0].len); ssp++)
+ for (ssp = mp[0].str; ssp < (mp[0].str + mp[0].size); ssp++)
if (dp < ep)
*dp++ = *ssp;
} else {