summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Lovset <[email protected]>2023-05-20 07:24:22 +0200
committerTyge Lovset <[email protected]>2023-05-20 07:24:22 +0200
commited9ccf1dcca8d3651e13ff1686148b4b23773721 (patch)
tree0594df357a1b4239a0d38c686099242cc16b0b44
parent26513bb1352ab4e4ffe931aabd80868216afc551 (diff)
downloadSTC-modified-ed9ccf1dcca8d3651e13ff1686148b4b23773721.tar.gz
STC-modified-ed9ccf1dcca8d3651e13ff1686148b4b23773721.zip
Bug fix (NB!): cregex_captures() now returns num of cap. group *excluding* the full match group (0).
-rw-r--r--docs/cregex_api.md10
-rw-r--r--include/stc/cregex.h2
-rw-r--r--src/cregex.c6
3 files changed, 9 insertions, 9 deletions
diff --git a/docs/cregex_api.md b/docs/cregex_api.md
index 9a15a869..e702c47c 100644
--- a/docs/cregex_api.md
+++ b/docs/cregex_api.md
@@ -29,7 +29,7 @@ cregex cregex_from(const char* pattern, int cflags = CREG_DEFAULT);
// return CREG_OK, or negative error code on failure
int cregex_compile(cregex *self, const char* pattern, int cflags = CREG_DEFAULT);
- // num. of capture groups in regex. 0 if RE is invalid. First group is the full match
+ // num. of capture groups in regex, excluding the 0th group which is the full match
int cregex_captures(const cregex* self);
// return CREG_OK, CREG_NOMATCH, or CREG_MATCHERROR
@@ -139,14 +139,14 @@ To iterate multiple matches in an input string, you may use
```c
csview match[5] = {0};
while (cregex_find(&re, input, match, CREG_M_NEXT) == CREG_OK)
- c_forrange (k, cregex_captures(&re))
- printf("submatch %lld: %.*s\n", k, c_SV(match[k]));
+ for (int k = 1; i <= cregex_captures(&re); ++k)
+ printf("submatch %d: %.*s\n", k, c_SV(match[k]));
```
There is also a for-loop macro to simplify it:
```c
c_formatch (it, &re, input)
- c_forrange (k, cregex_captures(&re))
- printf("submatch %lld: %.*s\n", k, c_SV(it.match[k]));
+ for (int k = 1; i <= cregex_captures(&re); ++k)
+ printf("submatch %d: %.*s\n", k, c_SV(it.match[k]));
```
## Using cregex in a project
diff --git a/include/stc/cregex.h b/include/stc/cregex.h
index a48b4c49..f90acbf4 100644
--- a/include/stc/cregex.h
+++ b/include/stc/cregex.h
@@ -104,7 +104,7 @@ STC_INLINE cregex cregex_from_2(const char* pattern, int cflags) {
return re;
}
-/* number of capture groups in a regex pattern including full the match capture, 0 if regex is invalid */
+/* number of capture groups in a regex pattern, excluding the full match capture (0) */
int cregex_captures(const cregex* re);
/* return CREG_OK, CREG_NOMATCH or CREG_MATCHERROR. */
diff --git a/src/cregex.c b/src/cregex.c
index 981a256a..5ba07550 100644
--- a/src/cregex.c
+++ b/src/cregex.c
@@ -1251,12 +1251,12 @@ cregex_compile_3(cregex *self, const char* pattern, int cflags) {
int
cregex_captures(const cregex* self) {
- return self->prog ? 1 + self->prog->nsubids : 0;
+ return self->prog ? self->prog->nsubids : 0;
}
int
cregex_find_4(const cregex* re, const char* input, csview match[], int mflags) {
- int res = _regexec(re->prog, input, cregex_captures(re), match, mflags);
+ int res = _regexec(re->prog, input, cregex_captures(re) + 1, match, mflags);
switch (res) {
case 1: return CREG_OK;
case 0: return CREG_NOMATCH;
@@ -1281,7 +1281,7 @@ cregex_replace_sv_6(const cregex* re, csview input, const char* replace, int cou
cstr out = cstr_NULL;
cstr subst = cstr_NULL;
csview match[CREG_MAX_CAPTURES];
- int nmatch = cregex_captures(re);
+ int nmatch = cregex_captures(re) + 1;
if (!count) count = INT32_MAX;
bool copy = !(rflags & CREG_R_STRIP);