diff --git a/str.c b/str.c index e78a8c80..7b165d31 100644 --- a/str.c +++ b/str.c @@ -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; diff --git a/str.h b/str.h index 1dafd4f1..06888275 100644 --- a/str.h +++ b/str.h @@ -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 + */ +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 + */ +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