summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--docs/ccommon_api.md11
-rw-r--r--include/stc/ccommon.h11
2 files changed, 14 insertions, 8 deletions
diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md
index 7a0eba88..2d26fbe3 100644
--- a/docs/ccommon_api.md
+++ b/docs/ccommon_api.md
@@ -2,6 +2,7 @@
The following macros are recommended to use, and they safe/have no side-effects.
+## Scope macros (RAII)
### c_auto, c_with, c_scope, c_defer
General ***defer*** mechanics for resource acquisition. These macros allows you to specify the
freeing of the resources at the point where the acquisition takes place.
@@ -89,6 +90,7 @@ int main()
printf("%s\n", cstr_str(i.ref));
}
```
+
### The **checkauto** utility program (for RAII)
The **checkauto** program will check the source code for any misuses of the `c_auto*` macros which
may lead to resource leakages. The `c_auto*`- macros are implemented as one-time executed **for-loops**,
@@ -137,9 +139,10 @@ from a `c_auto` scope:
...
} // for
```
+## Loop abstaction macros
### c_forlist
-Iterate compound literal array elements
+Iterate compound literal array elements. Additional to `i.ref`, you can access `i.data`, `i.size`, and `i.index` of the input list/element.
```c
// apply multiple push_backs
c_forlist (i, int, {1, 2, 3})
@@ -149,9 +152,13 @@ c_forlist (i, int, {1, 2, 3})
c_forlist (i, cmap_ii_raw, { {4, 5}, {6, 7} })
cmap_ii_insert(&map, i.ref->first, i.ref->second);
-// even string literals pushed to a stack of cstr:
+// string literals pushed to a stack of cstr:
c_forlist (i, const char*, {"Hello", "crazy", "world"})
cstack_str_emplace(&stk, *i.ref);
+
+// reverse the list:
+c_forlist (i, int, {1, 2, 3})
+ cvec_i_push_back(&vec, i.data[i.size - 1 - i.index]);
```
### c_foreach, c_forpair
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h
index 02f41dd5..2b3cfd76 100644
--- a/include/stc/ccommon.h
+++ b/include/stc/ccommon.h
@@ -163,7 +163,7 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle,
; it.ref != (C##_value*)_endref; C##_next(&it))
#ifndef c_FLT_STACK
-#define c_FLT_STACK 20
+#define c_FLT_STACK 16
#endif
#define c_flt_take(i, n) (++(i).stack[(i).top++] <= (n))
#define c_flt_drop(i, n) (++(i).stack[(i).top++] > (n))
@@ -206,8 +206,8 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle,
typedef long long crange_value;
struct {crange_value val, end, step; } typedef crange;
struct {crange_value *ref, end, step; } typedef crange_iter;
-#define crange_from(...) c_MACRO_OVERLOAD(crange_from, __VA_ARGS__)
#define crange_init() crange_from3(0, INTMAX_MAX, 1)
+#define crange_from(...) c_MACRO_OVERLOAD(crange_from, __VA_ARGS__)
#define crange_from1(start) crange_from3(start, INTMAX_MAX, 1)
#define crange_from2(start, end) crange_from3(start, end, 1)
STC_INLINE crange crange_from3(crange_value start, crange_value finish, crange_value step)
@@ -220,10 +220,9 @@ STC_INLINE void crange_next(crange_iter* it)
{ *it->ref += it->step; if ((it->step > 0) == (*it->ref > it->end)) it->ref = NULL; }
#define c_forlist(it, T, ...) \
- for (struct {T* data; T* ref; size_t index;} \
- it = {.data=(T[])__VA_ARGS__, .ref=it.data} \
- ; it.ref != &it.data[c_arraylen(((T[])__VA_ARGS__))] \
- ; ++it.ref, ++it.index)
+ for (struct {T* data; T* ref; int size, index;} \
+ it = {.data=(T[])__VA_ARGS__, .ref=it.data, .size=c_arraylen(((T[])__VA_ARGS__))} \
+ ; it.index < it.size; ++it.ref, ++it.index)
#define c_with(...) c_MACRO_OVERLOAD(c_with, __VA_ARGS__)
#define c_with2(declvar, drop) for (declvar, **_c_i = NULL; !_c_i; ++_c_i, drop)