diff --git a/rmutil/util.c b/rmutil/util.c index bd5f1b6..8ca572a 100644 --- a/rmutil/util.c +++ b/rmutil/util.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include "util.h" @@ -40,7 +41,7 @@ int RMUtil_ArgIndex(const char *arg, RedisModuleString **argv, int argc) { if (l != larg) continue; if (carg != NULL && strncasecmp(carg, arg, larg) == 0) { return offset; - } + } \ } return -1; } @@ -245,4 +246,17 @@ int RedisModule_TryGetValue(RedisModuleKey *key, const RedisModuleType *type, vo } else { return RMUTIL_VALUE_MISMATCH; } +} + +int RedisModule_Strncasecmp(const RedisModuleString *rs1, const char *s2, size_t n) { + size_t n2; + const char *s1 = RedisModule_StringPtrLen(rs1, &n2); + if (n != n2) { + return -1; + } + return strncasecmp(s1, s2, n); +} + +int RedisModule_Strcasecmp(const RedisModuleString *s1, const char *s2) { + return RedisModule_Strncasecmp(s1, s2, strlen(s2)); } \ No newline at end of file diff --git a/rmutil/util.h b/rmutil/util.h index 3b3be76..e6cfcb6 100644 --- a/rmutil/util.h +++ b/rmutil/util.h @@ -130,4 +130,19 @@ typedef enum { */ int RedisModule_TryGetValue(RedisModuleKey *key, const RedisModuleType *type, void **out); +/** + * Compares a RedisModuleString against an actual string buffer, avoiding + * keeping a temporary value for RedisModule_StringPtrLen. + * @param s1 the RedisModuleString + * @param s2 the buffer + * @param n the length of the C string. + * Returns 0 if the strings are equal. + */ +int RedisModule_Strncasecmp(const RedisModuleString *s1, const char *s2, size_t n); + +/** + * Exactly like RedisModule_Strncasecmp, except that `s2` is NUL-terminated + */ +int RedisModule_Strcasecmp(const RedisModuleString *s, const char *s2); + #endif