New "str.h" functions: strn_str_cmp(), strn_str_casecmp()

This commit is contained in:
Andrew Bettison 2013-02-13 15:52:44 +10:30
parent fcb6600cd6
commit 5013424ab1
2 changed files with 34 additions and 0 deletions

18
str.c
View File

@ -131,6 +131,24 @@ int strncase_startswith(const char *str, size_t len, const char *substring, cons
return 1;
}
int strn_str_cmp(const char *str1, size_t len1, const char *str2)
{
int r = strncmp(str1, str2, len1);
if (r)
return r;
size_t len2 = strlen(str2);
return len1 < len2 ? -1 : len1 > len2 ? 1 : 0;
}
int strn_str_casecmp(const char *str1, size_t len1, const char *str2)
{
int r = strncasecmp(str1, str2, len1);
if (r)
return r;
size_t len2 = strlen(str2);
return len1 < len2 ? -1 : len1 > len2 ? 1 : 0;
}
int parse_argv(char *cmdline, char delim, char **argv, int max_argv)
{
int argc=0;

16
str.h
View File

@ -119,6 +119,22 @@ int strcase_startswith(const char *str, const char *substring, const char **afte
*/
int strncase_startswith(const char *str, size_t len, const char *substring, const char **afterp);
/* Compare the given string 'str1' of a given length 'len1' with a given nul-terminated string
* 'str2'. Equivalent to { str1[len1] = '\0'; return strcmp(str1, str2); } except without modifying
* str1[].
*
* @author Andrew Bettison <andrew@servalproject.com>
*/
int strn_str_cmp(const char *str1, size_t len1, const char *str2);
/* Compare case-insenstivively the given string 'str1' of a given length 'len1' with a given
* nul-terminated string 'str2'. Equivalent to { str1[len1] = '\0'; return strcasecmp(str1, str2);
* } except without modifying str1[].
*
* @author Andrew Bettison <andrew@servalproject.com>
*/
int strn_str_casecmp(const char *str1, size_t len1, const char *str2);
/* like strstr(3), but doesn't depend on null termination.
*
* @author Paul Gardner-Stephen <paul@servalproject.org>