Skip to content

Commit 980013e

Browse files
rscharfegitster
authored andcommitted
t-ctype: allow NUL anywhere in the specification string
Replace the custom function is_in() for looking up a character in the specification string with memchr(3) and sizeof. This is shorter, simpler and allows NUL anywhere in the string, which may come in handy if we ever want to support more character classes that contain it. Getting the string size using sizeof only works in a macro and with a string constant. Use ARRAY_SIZE and compile-time checks to make sure we are not passed a string pointer. Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3c2a3fd commit 980013e

File tree

1 file changed

+4
-14
lines changed

1 file changed

+4
-14
lines changed

t/unit-tests/t-ctype.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
#include "test-lib.h"
22

3-
static int is_in(const char *s, int ch)
4-
{
5-
/*
6-
* We can't find NUL using strchr. Accept it as the first
7-
* character in the spec -- there are no empty classes.
8-
*/
9-
if (ch == '\0')
10-
return ch == *s;
11-
if (*s == '\0')
12-
s++;
13-
return !!strchr(s, ch);
14-
}
15-
163
/* Macro to test a character type */
174
#define TEST_CTYPE_FUNC(func, string) \
185
static void test_ctype_##func(void) { \
6+
size_t len = ARRAY_SIZE(string) - 1 + \
7+
BUILD_ASSERT_OR_ZERO(ARRAY_SIZE(string) > 0) + \
8+
BUILD_ASSERT_OR_ZERO(sizeof(string[0]) == sizeof(char)); \
199
for (int i = 0; i < 256; i++) { \
20-
if (!check_int(func(i), ==, is_in(string, i))) \
10+
if (!check_int(func(i), ==, !!memchr(string, i, len))) \
2111
test_msg(" i: 0x%02x", i); \
2212
} \
2313
if (!check(!func(EOF))) \

0 commit comments

Comments
 (0)