Skip to content

Commit 433c8b8

Browse files
committed
Allow a NULL output string.
1 parent 3234a7c commit 433c8b8

3 files changed

Lines changed: 46 additions & 3 deletions

File tree

include/re/groups.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ struct re_pos;
3737
* The output string will always be less than or equal in
3838
* length to the format string. The output is \0-terminated.
3939
* outn includes the \0.
40+
*
41+
* outs may be NULL in which case outn must be 0, and no
42+
* output is made.
4043
*/
4144
bool
4245
re_interpolate_groups(const char *fmt, char esc,

src/libre/re_interpolate_groups.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
#include <re/groups.h>
1616

1717
#define OUT_CHAR(c) \
18-
do { \
18+
do if (outs != NULL) { \
1919
if (outn < 1) goto overflow; \
2020
*outs++ = (c); \
2121
outn--; \
2222
} while (0)
2323

2424
#define OUT_GROUP(s) \
25-
do { \
25+
do if (outs != NULL) { \
2626
size_t n = strlen((s)); \
2727
if (outn < n) goto overflow; \
2828
(void) memcpy(outs, s, n); \
@@ -48,7 +48,7 @@ re_interpolate_groups(const char *fmt, char esc,
4848
assert(esc != '\0');
4949
assert(group0 != NULL || groupc == 0);
5050
assert(groupc < UINT_MAX / 10 - 1);
51-
assert(outs != NULL);
51+
assert(outs != NULL || outn == 0);
5252

5353
state = STATE_LIT;
5454
group = 0;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2026 Katherine Flavel
3+
*
4+
* See LICENCE for the full copyright terms.
5+
*/
6+
7+
#include <assert.h>
8+
#include <stdbool.h>
9+
#include <string.h>
10+
#include <stdio.h>
11+
12+
#include <re/re.h>
13+
#include <re/groups.h>
14+
15+
static unsigned failed;
16+
17+
static void
18+
test(const char *fmt, bool expected)
19+
{
20+
bool r;
21+
22+
assert(fmt != NULL);
23+
24+
r = re_interpolate_groups(fmt, '$', "<g0>", 0, NULL, "<ne>", NULL, 0, NULL, NULL);
25+
26+
failed += r != expected;
27+
28+
printf("%s/%d => %d%s\n", fmt, 0, r,
29+
r != expected ? " XXX" : "");
30+
}
31+
32+
int main(void) {
33+
test("", true);
34+
test("abc", true);
35+
test("$$", true);
36+
test("$x", false);
37+
38+
return failed;
39+
}
40+

0 commit comments

Comments
 (0)