summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--examples/shape.c20
-rw-r--r--include/stc/ccommon.h11
-rw-r--r--include/stc/crandom.h2
-rw-r--r--src/cregex.c45
4 files changed, 38 insertions, 40 deletions
diff --git a/examples/shape.c b/examples/shape.c
index e5ac0e58..b052d921 100644
--- a/examples/shape.c
+++ b/examples/shape.c
@@ -7,14 +7,14 @@
#define c_dyn_cast(T, s) \
(&T##_api == (s)->api ? (T*)(s) : (T*)0)
-#define c_vtable(Api, T) \
+#define c_vtable(Api, T, base) \
c_static_assert(offsetof(T, base) == 0); \
static Api T##_api
// Shape definition
// ============================================================
-typedef struct {
+typedef struct {
float x, y;
} Point;
@@ -35,7 +35,7 @@ struct Shape {
void Shape_drop(Shape* shape)
{
- printf("base destructed\n");
+ printf("shape destructed\n");
}
void Shape_delete(Shape* shape)
@@ -50,11 +50,11 @@ void Shape_delete(Shape* shape)
// ============================================================
typedef struct {
- Shape base;
+ Shape shape;
Point p[3];
} Triangle;
-c_vtable(struct ShapeAPI, Triangle);
+c_vtable(struct ShapeAPI, Triangle, shape);
Triangle* Triangle_new(Point a, Point b, Point c)
@@ -84,11 +84,11 @@ static struct ShapeAPI Triangle_api = {
#include <stc/cstack.h>
typedef struct {
- Shape base;
+ Shape shape;
PointVec points;
} Polygon;
-c_vtable(struct ShapeAPI, Polygon);
+c_vtable(struct ShapeAPI, Polygon, shape);
Polygon* Polygon_new(void)
@@ -151,9 +151,9 @@ int main(void)
c_apply(p, Polygon_addPoint(pol2, *p), Point,
{{5, 7}, {12, 7}, {12, 20}, {82, 33}, {17, 56}});
- Shapes_push(&shapes, &tri1->base);
- Shapes_push(&shapes, &pol1->base);
- Shapes_push(&shapes, &pol2->base);
+ Shapes_push(&shapes, &tri1->shape);
+ Shapes_push(&shapes, &pol1->shape);
+ Shapes_push(&shapes, &pol2->shape);
c_foreach (i, Shapes, shapes)
testShape(*i.ref);
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h
index 9c00255a..3a6d8f4e 100644
--- a/include/stc/ccommon.h
+++ b/include/stc/ccommon.h
@@ -203,8 +203,8 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle,
#define c_apply(v, action, T, ...) do { \
typedef T _c_T; \
- const _c_T _c_arr[] = __VA_ARGS__, *v = _c_arr, \
- *_c_end = v + c_arraylen(_c_arr); \
+ _c_T _c_arr[] = __VA_ARGS__, *v = _c_arr; \
+ const _c_T *_c_end = v + c_arraylen(_c_arr); \
while (v != _c_end) { action; ++v; } \
} while (0)
@@ -215,6 +215,7 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle,
} while (0)
#define c_pair(v) (v)->first, (v)->second
+#define c_drop(C, ...) c_apply(_p, C##_drop(*_p), C*, {__VA_ARGS__})
#define c_find_if(C, cnt, it, pred) \
c_find_in(C, C##_begin(&cnt), C##_end(&cnt), it, pred)
@@ -229,12 +230,6 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle,
if (it.ref == _end.ref) it.ref = NULL; \
} while (0)
-#define c_drop(C, ...) do { \
- C* _c_arr[] = {__VA_ARGS__}; \
- for (size_t _c_i = 0; _c_i < c_arraylen(_c_arr); ++_c_i) \
- C##_drop(_c_arr[_c_i]); \
-} while (0)
-
#if defined(__SIZEOF_INT128__)
#define c_umul128(a, b, lo, hi) \
do { __uint128_t _z = (__uint128_t)(a)*(b); \
diff --git a/include/stc/crandom.h b/include/stc/crandom.h
index 1b94fd66..e99be1ca 100644
--- a/include/stc/crandom.h
+++ b/include/stc/crandom.h
@@ -191,4 +191,4 @@ STC_DEF double stc64_normalf(stc64_t* rng, stc64_normalf_t* dist) {
#undef i_static
#undef i_header
#undef i_implement
-#undef i_extern \ No newline at end of file
+#undef i_extern
diff --git a/src/cregex.c b/src/cregex.c
index 8ec56fae..b326b4fc 100644
--- a/src/cregex.c
+++ b/src/cregex.c
@@ -123,6 +123,7 @@ enum {
IRUNE,
ASC_an , ASC_AN, /* alphanum */
ASC_al , ASC_AL, /* alpha */
+ ASC_as , ASC_AS, /* ascii */
ASC_bl , ASC_BL, /* blank */
ASC_ct , ASC_CT, /* ctrl */
ASC_d , ASC_D, /* digit */
@@ -130,7 +131,7 @@ enum {
ASC_w , ASC_W, /* word */
ASC_gr , ASC_GR, /* graphic */
ASC_pr , ASC_PR, /* print */
- ASC_pt , ASC_PT, /* punct */
+ ASC_pu , ASC_PU, /* punct */
ASC_lo , ASC_LO, /* lower */
ASC_up , ASC_UP, /* upper */
ASC_xd , ASC_XD, /* hex */
@@ -510,10 +511,7 @@ static Reprog*
optimize(Parser *par, Reprog *pp)
{
Reinst *inst, *target;
- size_t size;
- Reprog *npp;
Reclass *cl;
- ptrdiff_t diff;
/*
* get rid of NOOP chains
@@ -530,12 +528,15 @@ optimize(Parser *par, Reprog *pp)
* necessary. Reallocate to the actual space used
* and then relocate the code.
*/
- size = sizeof(Reprog) + (par->freep - pp->firstinst)*sizeof(Reinst);
- npp = (Reprog *)realloc(pp, size);
- if (npp==NULL || npp==pp)
- return pp;
- diff = (char *)npp - (char *)pp;
+ uintptr_t ipp = (uintptr_t)pp;
+ size_t size = sizeof(Reprog) + (par->freep - pp->firstinst)*sizeof(Reinst);
+ Reprog *npp = (Reprog *)realloc(pp, size);
+ ptrdiff_t diff = (uintptr_t)npp - ipp;
+
+ if ((npp == NULL) | (diff == 0))
+ return (Reprog *)ipp;
par->freep = (Reinst *)((char *)par->freep + diff);
+
for (inst = npp->firstinst; inst < par->freep; inst++) {
switch (inst->type) {
case OR:
@@ -608,7 +609,7 @@ nextc(Parser *par, Rune *rp)
{"{Blank}", 7, ASC_bl},
{"{Graph}", 7, ASC_gr},
{"{Print}", 7, ASC_pr},
- {"{Punct}", 7, ASC_pt},
+ {"{Punct}", 7, ASC_pu},
};
int inv = *rp == 'P';
for (unsigned i = 0; i < (sizeof cls/sizeof *cls); ++i)
@@ -734,11 +735,11 @@ bldcclass(Parser *par)
}
if (rune == '[' && *par->exprp == ':') {
static struct { const char* c; int n, r; } cls[] = {
- {"alnum:]", 7, ASC_an}, {"alpha:]", 7, ASC_al}, {"blank:]", 7, ASC_bl},
- {"cntrl:]", 7, ASC_ct}, {"digit:]", 7, ASC_d}, {"graph:]", 7, ASC_gr},
- {"lower:]", 7, ASC_lo}, {"print:]", 7, ASC_pr}, {"punct:]", 7, ASC_pt},
- {"space:]", 7, ASC_s}, {"upper:]", 7, ASC_up}, {"xdigit:]", 8, ASC_xd},
- {"word:]", 6, ASC_w},
+ {"alnum:]", 7, ASC_an}, {"alpha:]", 7, ASC_al}, {"ascii:]", 7, ASC_as},
+ {"blank:]", 7, ASC_bl}, {"cntrl:]", 7, ASC_ct}, {"digit:]", 7, ASC_d},
+ {"graph:]", 7, ASC_gr}, {"lower:]", 7, ASC_lo}, {"print:]", 7, ASC_pr},
+ {"punct:]", 7, ASC_pu}, {"space:]", 7, ASC_s}, {"upper:]", 7, ASC_up},
+ {"xdigit:]", 8, ASC_xd}, {"word:]", 6, ASC_w},
};
int inv = par->exprp[1] == '^', off = 1 + inv;
for (unsigned i = 0; i < (sizeof cls/sizeof *cls); ++i)
@@ -796,14 +797,14 @@ static Reprog*
regcomp1(Reprog *progp, Parser *par, const char *s, int cflags)
{
Token token;
- Reprog *volatile pp;
/* get memory for the program. estimated max usage */
const int instcap = 5 + 6*strlen(s);
- pp = (Reprog *)realloc(progp, sizeof(Reprog) + instcap*sizeof(Reinst));
+ Reprog* pp = (Reprog *)realloc(progp, sizeof(Reprog) + instcap*sizeof(Reinst));
if (pp == NULL) {
- pp = progp;
- rcerror(par, creg_outofmemory);
+ par->errors = creg_outofmemory;
+ free(progp);
+ return NULL;
}
pp->flags.caseless = (cflags & creg_caseless) != 0;
pp->flags.dotall = (cflags & creg_dotall) != 0;
@@ -884,6 +885,8 @@ runematch(Rune s, Rune r, bool icase)
case ASC_al: return inv ^ (isalpha(r) != 0);
case ASC_AN: inv = 1;
case ASC_an: return inv ^ (isalnum(r) != 0);
+ case ASC_AS: return (r >= 128);
+ case ASC_as: return (r < 128);
case ASC_BL: inv = 1;
case ASC_bl: return inv ^ ((r == ' ') | (r == '\t'));
case ASC_CT: inv = 1;
@@ -892,8 +895,8 @@ runematch(Rune s, Rune r, bool icase)
case ASC_gr: return inv ^ (isgraph(r) != 0);
case ASC_PR: inv = 1;
case ASC_pr: return inv ^ (isprint(r) != 0);
- case ASC_PT: inv = 1;
- case ASC_pt: return inv ^ (ispunct(r) != 0);
+ case ASC_PU: inv = 1;
+ case ASC_pu: return inv ^ (ispunct(r) != 0);
case ASC_LO: inv = 1;
case ASC_lo: return inv ^ (islower(r) != 0);
case ASC_UP: inv = 1;