From 4df77fcfc3f68c8a6d4053c51a3b027a364d3f65 Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Tue, 18 Feb 2025 17:32:30 +0100 Subject: [PATCH 1/2] fmt/pl: add pl_strncasecmp and tests --- include/re_fmt.h | 1 + src/fmt/pl.c | 22 ++++++++++++++++++++++ test/fmt.c | 11 +++++++++++ 3 files changed, 34 insertions(+) diff --git a/include/re_fmt.h b/include/re_fmt.h index bb64be49b..506b12b27 100644 --- a/include/re_fmt.h +++ b/include/re_fmt.h @@ -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); diff --git a/src/fmt/pl.c b/src/fmt/pl.c index 0139614d0..52836c965 100644 --- a/src/fmt/pl.c +++ b/src/fmt/pl.c @@ -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) diff --git a/test/fmt.c b/test/fmt.c index e6812621d..4fc293344 100644 --- a/test/fmt.c +++ b/test/fmt.c @@ -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"); @@ -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; From bf8d0b2ad45be86b4a17903e1ab0acaaccf527c9 Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Tue, 18 Feb 2025 17:45:38 +0100 Subject: [PATCH 2/2] fix win32 --- src/fmt/pl.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/fmt/pl.c b/src/fmt/pl.c index 52836c965..57cf08d2d 100644 --- a/src/fmt/pl.c +++ b/src/fmt/pl.c @@ -575,8 +575,11 @@ int pl_strncasecmp(const struct pl *pl, const char *str, size_t n) 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 }