Skip to content

Commit

Permalink
pl: add pl_strncmp similar to strncmp of cstdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
cspiel1 authored and maximilianfridrich committed Apr 19, 2024
1 parent 3a60c39 commit 7db1098
Show file tree
Hide file tree
Showing 2 changed files with 23 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 @@ -46,6 +46,7 @@ int pl_strcpy(const struct pl *pl, char *str, size_t size);
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_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 @@ -536,6 +536,28 @@ int pl_strcmp(const struct pl *pl, const char *str)
}


/**
* Compare n characters of a pointer-length object with a NULL-terminated
* string (case-sensitive)
*
* @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_strncmp(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 strncmp(pl->p, str, n) == 0 ? 0 : EINVAL;
}


/**
* Compare a pointer-length object with a NULL-terminated string
* (case-insensitive)
Expand Down

0 comments on commit 7db1098

Please sign in to comment.