summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Lovset <[email protected]>2022-09-06 19:30:51 +0200
committerTyge Lovset <[email protected]>2022-09-06 19:30:51 +0200
commita65d94a54ba98ca1fa743d6c70a9b243afad47da (patch)
tree16839d8497eaf9f3cab4e5eb7c0efd3db507d35a
parent90311ff7eb34e5fc4fc1c2c38b8d0433642e9659 (diff)
downloadSTC-modified-a65d94a54ba98ca1fa743d6c70a9b243afad47da.tar.gz
STC-modified-a65d94a54ba98ca1fa743d6c70a9b243afad47da.zip
Updated docs for cregex and csview.
-rw-r--r--docs/cregex_api.md18
-rw-r--r--docs/csview_api.md7
-rw-r--r--include/stc/csview.h7
3 files changed, 19 insertions, 13 deletions
diff --git a/docs/cregex_api.md b/docs/cregex_api.md
index 43ad1f41..87e25e02 100644
--- a/docs/cregex_api.md
+++ b/docs/cregex_api.md
@@ -130,19 +130,15 @@ In order to use a callback function in the replace call, see `examples/regex_rep
To iterate multiple matches in an input string, you may use:
```c
csview match[5] = {0};
-while (cregex_find(input, &re, match, cre_m_next) == cre_success) {
- c_forrange (int, i, cregex_captures(&re))
- printf("submatch %d: %.*s\n", i, c_ARGsv(match[i]));
- puts("");
-}
+while (cregex_find(input, &re, match, cre_m_next) == cre_success)
+ c_forrange (int, k, cregex_captures(&re))
+ printf("submatch %d: %.*s\n", k, c_ARGsv(match[k]));
```
-There is also a safe macro that simplifies it a bit:
+There is also a safe macro which simplifies this:
```c
-c_foreach_match (m, &re, input) {
- c_forrange (int, i, cregex_captures(&re))
- printf("submatch %d: %.*s\n", i, c_ARGsv(m.ref[i]));
- puts("");
-}
+c_foreach_match (it, &re, input)
+ c_forrange (int, k, cregex_captures(&re))
+ printf("submatch %d: %.*s\n", k, c_ARGsv(it.match[k]));
```
## Using cregex in a project
diff --git a/docs/csview_api.md b/docs/csview_api.md
index 65e9a066..9da64cb2 100644
--- a/docs/csview_api.md
+++ b/docs/csview_api.md
@@ -79,6 +79,13 @@ csview cstr_u8_substr(const cstr* self, size_t bytepos, size_t u8len);
csview cstr_slice(const cstr* self, size_t p1, size_t p2);
csview cstr_slice_ex(const cstr* s, intptr_t p, intptr_t q); // negative p or q count from end
```
+### Iterate tokens: c_foreach_token, c_foreach_token_sv
+
+To iterate tokens in an input string separated by a string:
+```c
+c_foreach_token (i, "hello, one, two, three", ", ")
+ printf("token: %.*s\n", c_ARGsv(i.token));
+```
#### Helper methods
```c
diff --git a/include/stc/csview.h b/include/stc/csview.h
index bfbf1c61..b60f6e38 100644
--- a/include/stc/csview.h
+++ b/include/stc/csview.h
@@ -103,11 +103,14 @@ STC_API csview csview_substr_ex(csview sv, intptr_t pos, size_t n);
STC_API csview csview_slice_ex(csview sv, intptr_t p1, intptr_t p2);
STC_API csview csview_token(csview sv, csview sep, size_t* start);
-#define c_foreach_token(it, input, sep) \
+#define c_foreach_token_sv(it, input, sep) \
for (struct { csview token, _sep, _inp; size_t start; } \
- it = {.token=csview_from(input), ._sep=csview_from(sep), ._inp=it.token, .start=0} \
+ it = {.token=input, ._sep=sep, ._inp=it.token, .start=0} \
; it.start <= it._inp.size && (it.token = csview_token(it._inp, it._sep, &it.start)).str ; )
+#define c_foreach_token(it, input, sep) \
+ c_foreach_token_sv(it, csview_from(input), csview_from(sep))
+
/* csview interaction with cstr: */
#ifdef CSTR_H_INCLUDED