summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--docs/cstr_api.md3
-rw-r--r--examples/forfiltered.c17
-rw-r--r--examples/list.c12
-rw-r--r--include/stc/ccommon.h16
-rw-r--r--include/stc/csmap.h3
-rw-r--r--include/stc/cstr.h10
-rw-r--r--include/stc/csview.h4
7 files changed, 38 insertions, 27 deletions
diff --git a/docs/cstr_api.md b/docs/cstr_api.md
index cbb1a003..dbf6f462 100644
--- a/docs/cstr_api.md
+++ b/docs/cstr_api.md
@@ -59,6 +59,9 @@ char* cstr_append_s(cstr* self, cstr str);
int cstr_append_fmt(cstr* self, const char* fmt, ...); // printf() formatting
char* cstr_append_uninit(cstr* self, size_t len); // return ptr to start of uninited data
+void cstr_push(cstr* self, const char* chr); // append one utf8 char
+void cstr_pop(cstr* self); // pop one utf8 char
+
void cstr_insert(cstr* self, size_t pos, const char* ins);
void cstr_insert_sv(cstr* self, size_t pos, csview ins);
void cstr_insert_s(cstr* self, size_t pos, cstr ins);
diff --git a/examples/forfiltered.c b/examples/forfiltered.c
index d2a62c2a..71327629 100644
--- a/examples/forfiltered.c
+++ b/examples/forfiltered.c
@@ -16,7 +16,7 @@ void demo1(void)
IVec_push(&vec, *v);
c_forfiltered (i, IVec, vec, *i.ref != 80)
- printf(" %d", *i.ref);
+ printf(" %d", *i.ref);
puts("");
c_forfiltered (i, IVec, vec, i.idx >= 3 // drop(3)
@@ -42,13 +42,13 @@ fn main() {
void demo2(void)
{
- c_auto (IVec, vec) {
+ c_auto (IVec, vector) {
int n = 0;
for (size_t x=1;; ++x)
if (n == 5) break;
- else if (x & 1) ++n, IVec_push(&vec, x*x);
+ else if (x & 1) ++n, IVec_push(&vector, x*x);
- c_foreach (i, IVec, vec) printf(" %d", *i.ref);
+ c_foreach (i, IVec, vector) printf(" %d", *i.ref);
puts("");
}
}
@@ -61,7 +61,6 @@ fn main() {
.collect();
let words_containing_i: Vec<&str> = words
.into_iter()
- .take(2)
.filter(|word| word.contains("i"))
.collect();
println!("{:?}", words_containing_i);
@@ -70,12 +69,12 @@ fn main() {
void demo3(void)
{
c_auto (SVec, words, words_containing_i) {
- const char* sentence = "This is a sentence in Rust.";
- c_fortoken(w, sentence, " ")
+ const char* sentence = "This is a sentence in C99.";
+ c_fortoken (w, sentence, " ")
SVec_push(&words, *w.ref);
- c_forfiltered (w, SVec, words, csview_contains(*w.ref, c_sv("i")),
- w.count < 2) // take(2)
+ c_forfiltered (w, SVec, words,
+ csview_contains(*w.ref, c_sv("i")))
SVec_push(&words_containing_i, *w.ref);
c_foreach (w, SVec, words_containing_i)
diff --git a/examples/list.c b/examples/list.c
index 2dc19704..9ed9ec76 100644
--- a/examples/list.c
+++ b/examples/list.c
@@ -10,7 +10,6 @@
#include <stc/crandom.h>
int main() {
- int k;
const int n = 2000000;
c_auto (clist_fx, list)
@@ -26,16 +25,15 @@ int main() {
sum += *i.ref;
printf("sum %f\n\n", sum);
- k = 0;
- c_foreach (i, clist_fx, list)
- if (++k <= 10) printf("%8d: %10f\n", k, *i.ref); else break;
+ c_forloop (i, clist_fx, list, i.idx < 10)
+ printf("%8d: %10f\n", (int)i.idx, *i.ref);
+
puts("sort");
clist_fx_sort(&list); // mergesort O(n*log n)
puts("sorted");
- k = 0;
- c_foreach (i, clist_fx, list)
- if (++k <= 10) printf("%8d: %10f\n", k, *i.ref); else break;
+ c_forloop (i, clist_fx, list, i.idx < 10)
+ printf("%8d: %10f\n", (int)i.idx, *i.ref);
puts("");
clist_fx_clear(&list);
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h
index 01af3111..64c39da0 100644
--- a/include/stc/ccommon.h
+++ b/include/stc/ccommon.h
@@ -162,16 +162,16 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle,
for (C##_iter it = start, *_endref = (C##_iter*)(finish).ref \
; it.ref != (C##_value*)_endref; C##_next(&it))
-#define c_forpred(i, C, cnt, pred) \
- for (struct {C##_iter it; const C##_value *ref; size_t idx, count;} \
- i = {.it=C##_begin(&cnt), .ref=i.it.ref, .idx=0, .count=0} \
- ; i.ref && (pred); C##_next(&i.it), i.ref = i.it.ref, ++i.idx)
-
#define c_forfiltered(...) c_MACRO_OVERLOAD(c_forfiltered, __VA_ARGS__)
#define c_forfiltered4(it, C, cnt, filter) \
- c_forpred(it, C, cnt, true) if (!(filter)) ; else
-#define c_forfiltered5(it, C, cnt, filter, pred) \
- c_forpred(it, C, cnt, pred) if (!((filter) && ++it.count)) ; else
+ c_forloop(it, C, cnt, true) if (!(filter)) ; else
+#define c_forfiltered5(it, C, cnt, filter, cond) \
+ c_forloop(it, C, cnt, cond) if (!((filter) && ++it.count)) ; else
+
+#define c_forloop(i, C, cnt, cond) \
+ for (struct {C##_iter it; const C##_value *ref; size_t idx, count;} \
+ i = {.it=C##_begin(&cnt), .ref=i.it.ref, .idx=0, .count=0} \
+ ; i.ref && (cond); C##_next(&i.it), i.ref = i.it.ref, ++i.idx)
#define c_forpair(key, val, C, cnt) /* structured binding */ \
for (struct {C##_iter _it; const C##_key* key; C##_mapped* val;} _ = {C##_begin(&cnt)} \
diff --git a/include/stc/csmap.h b/include/stc/csmap.h
index c773e70f..bcb85a6b 100644
--- a/include/stc/csmap.h
+++ b/include/stc/csmap.h
@@ -150,7 +150,8 @@ _cx_memb(_value_toraw)(const _cx_value* val) {
STC_INLINE int
_cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) {
- const _cx_rawkey rx = i_keyto(_i_keyref(x)), ry = i_keyto(_i_keyref(y));
+ const _cx_rawkey rx = i_keyto(_i_keyref(x));
+ const _cx_rawkey ry = i_keyto(_i_keyref(y));
return i_cmp((&rx), (&ry));
}
diff --git a/include/stc/cstr.h b/include/stc/cstr.h
index 983c3d4b..1e7a351f 100644
--- a/include/stc/cstr.h
+++ b/include/stc/cstr.h
@@ -349,6 +349,16 @@ STC_INLINE char* cstr_copy(cstr* self, cstr s) {
}
+STC_INLINE char* cstr_push(cstr* self, const char* chr)
+ { return cstr_append_n(self, chr, utf8_chr_size(chr)); }
+
+STC_INLINE void cstr_pop(cstr* self) {
+ csview sv = cstr_sv(self);
+ const char* s = sv.str + sv.size;
+ while ((*--s & 0xC0) == 0x80) ;
+ _cstr_set_size(self, s - sv.str);
+}
+
STC_INLINE char* cstr_append(cstr* self, const char* str)
{ return cstr_append_n(self, str, strlen(str)); }
diff --git a/include/stc/csview.h b/include/stc/csview.h
index f2ac1c42..54a4c4cc 100644
--- a/include/stc/csview.h
+++ b/include/stc/csview.h
@@ -32,8 +32,8 @@
#define csview_npos (SIZE_MAX >> 1)
#define csview_init() csview_null
-#define csview_drop(p) (p)
-#define csview_clone(sv) (sv)
+#define csview_drop c_default_drop
+#define csview_clone c_default_clone
STC_INLINE csview csview_from(const char* str)
{ return c_make(csview){str, strlen(str)}; }