Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fmt/pl: add pl_strncasecmp and tests #1277

Merged
merged 2 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 25 additions & 0 deletions src/fmt/pl.c
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,31 @@ 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;
#ifdef WIN32
return _strnicmp(pl->p, str, n) == 0 ? 0 : EINVAL;
#else
return strncasecmp(pl->p, str, n) == 0 ? 0 : EINVAL;
#endif
}


/**
* 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
Loading