Add strnchr() to str.c

This commit is contained in:
Andrew Bettison 2013-02-27 15:58:32 +10:30
parent 6eb08ae805
commit 14b859616e
2 changed files with 18 additions and 0 deletions

11
str.c
View File

@ -88,6 +88,17 @@ char *str_toupper_inplace(char *str)
return str;
}
int strnchr(const char *s, size_t n, char c)
{
for (; n; --n, ++s) {
if (*s == c)
return s;
if (!*s)
break;
}
return NULL;
}
int str_startswith(const char *str, const char *substring, const char **afterp)
{
while (*substring && *substring == *str)

7
str.h
View File

@ -86,6 +86,13 @@ size_t str_fromprint(unsigned char *dst, const char *src);
#define alloca_toprint(dstlen,buf,len) toprint((char *)alloca((dstlen) == -1 ? toprint_len((const char *)(buf),(len), "``") + 1 : (dstlen)), (dstlen), (const char *)(buf), (len), "``")
#define alloca_str_toprint(str) toprint_str((char *)alloca(toprint_str_len(str, "``") + 1), -1, (str), "``")
/* Like strchr(3), but only looks for 'c' in the first 'n' characters of 's', stopping at the first
* nul char in 's'.
*
* @author Andrew Bettison <andrew@servalproject.com>
*/
int strnchr(const char *s, size_t n, char c);
/* Check if a given nul-terminated string 'str' starts with a given nul-terminated sub-string. If
* so, return 1 and, if afterp is not NULL, set *afterp to point to the character in 'str'
* immediately following the substring. Otherwise return 0.