Skip to content

Commit

Permalink
fmt/pl: add pl_strncasecmp and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sreimers committed Feb 18, 2025
1 parent 31b484a commit 4df77fc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/re_fmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ int pl_strdup(char **dst, const struct pl *src);
int pl_dup(struct pl *dst, const struct pl *src);
int pl_strcmp(const struct pl *pl, const char *str);
int pl_strncmp(const struct pl *pl, const char *str, size_t n);
int pl_strncasecmp(const struct pl *pl, const char *str, size_t n);
int pl_strcasecmp(const struct pl *pl, const char *str);
int pl_cmp(const struct pl *pl1, const struct pl *pl2);
int pl_casecmp(const struct pl *pl1, const struct pl *pl2);
Expand Down
22 changes: 22 additions & 0 deletions src/fmt/pl.c
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,28 @@ int pl_strncmp(const struct pl *pl, const char *str, size_t n)
}


/**
* Compare n characters of a pointer-length object with a NULL-terminated
* string (case-insensitive)
*
* @param pl Pointer-length object
* @param str NULL-terminated string
* @param n number of characters that should be compared
*
* @return 0 if match, otherwise errorcode
*/
int pl_strncasecmp(const struct pl *pl, const char *str, size_t n)
{
if (!pl_isset(pl) || !str || !n)
return EINVAL;

if (pl->l < n)
return EINVAL;

return strncasecmp(pl->p, str, n) == 0 ? 0 : EINVAL;
}


/**
* Compare a pointer-length object with a NULL-terminated string
* (case-insensitive)
Expand Down
11 changes: 11 additions & 0 deletions test/fmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

int test_fmt_pl(void)
{
int err;
const struct pl pl = PL("rattarei");
const struct pl pl0 = PL("rattarei");
const struct pl pl0_ = PL("rAtTaReI");
Expand Down Expand Up @@ -94,6 +95,16 @@ int test_fmt_pl(void)
if (0 == pl_strcmp(&pl3, str0))
goto out;

/* pl_strncmp() */
err = pl_strncmp(&pl, "rat", 3);
TEST_ERR(err);
err = pl_strncmp(&pl, "RAT", 3);
TEST_EQUALS(EINVAL, err);

/* pl_strncasecmp() */
err = pl_strncasecmp(&pl, "RaT", 3);
TEST_ERR(err);

/* pl_strcasecmp() */
if (EINVAL != pl_strcasecmp(NULL, NULL))
goto out;
Expand Down

0 comments on commit 4df77fc

Please sign in to comment.