Skip to content

Commit 5623293

Browse files
authored
fmt/pl: add pl_strncasecmp and tests (#1277)
* fmt/pl: add pl_strncasecmp and tests
1 parent 31b484a commit 5623293

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

include/re_fmt.h

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ int pl_strdup(char **dst, const struct pl *src);
4747
int pl_dup(struct pl *dst, const struct pl *src);
4848
int pl_strcmp(const struct pl *pl, const char *str);
4949
int pl_strncmp(const struct pl *pl, const char *str, size_t n);
50+
int pl_strncasecmp(const struct pl *pl, const char *str, size_t n);
5051
int pl_strcasecmp(const struct pl *pl, const char *str);
5152
int pl_cmp(const struct pl *pl1, const struct pl *pl2);
5253
int pl_casecmp(const struct pl *pl1, const struct pl *pl2);

src/fmt/pl.c

+25
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,31 @@ int pl_strncmp(const struct pl *pl, const char *str, size_t n)
558558
}
559559

560560

561+
/**
562+
* Compare n characters of a pointer-length object with a NULL-terminated
563+
* string (case-insensitive)
564+
*
565+
* @param pl Pointer-length object
566+
* @param str NULL-terminated string
567+
* @param n number of characters that should be compared
568+
*
569+
* @return 0 if match, otherwise errorcode
570+
*/
571+
int pl_strncasecmp(const struct pl *pl, const char *str, size_t n)
572+
{
573+
if (!pl_isset(pl) || !str || !n)
574+
return EINVAL;
575+
576+
if (pl->l < n)
577+
return EINVAL;
578+
#ifdef WIN32
579+
return _strnicmp(pl->p, str, n) == 0 ? 0 : EINVAL;
580+
#else
581+
return strncasecmp(pl->p, str, n) == 0 ? 0 : EINVAL;
582+
#endif
583+
}
584+
585+
561586
/**
562587
* Compare a pointer-length object with a NULL-terminated string
563588
* (case-insensitive)

test/fmt.c

+11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
int test_fmt_pl(void)
1919
{
20+
int err;
2021
const struct pl pl = PL("rattarei");
2122
const struct pl pl0 = PL("rattarei");
2223
const struct pl pl0_ = PL("rAtTaReI");
@@ -94,6 +95,16 @@ int test_fmt_pl(void)
9495
if (0 == pl_strcmp(&pl3, str0))
9596
goto out;
9697

98+
/* pl_strncmp() */
99+
err = pl_strncmp(&pl, "rat", 3);
100+
TEST_ERR(err);
101+
err = pl_strncmp(&pl, "RAT", 3);
102+
TEST_EQUALS(EINVAL, err);
103+
104+
/* pl_strncasecmp() */
105+
err = pl_strncasecmp(&pl, "RaT", 3);
106+
TEST_ERR(err);
107+
97108
/* pl_strcasecmp() */
98109
if (EINVAL != pl_strcasecmp(NULL, NULL))
99110
goto out;

0 commit comments

Comments
 (0)