add string comparison functions

master
Mark Nunberg 7 years ago
parent 7b0b4803ff
commit 121f189a7a

@ -6,6 +6,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <limits.h> #include <limits.h>
#include <string.h> #include <string.h>
#include <strings.h>
#include <redismodule.h> #include <redismodule.h>
#include "util.h" #include "util.h"
@ -40,7 +41,7 @@ int RMUtil_ArgIndex(const char *arg, RedisModuleString **argv, int argc) {
if (l != larg) continue; if (l != larg) continue;
if (carg != NULL && strncasecmp(carg, arg, larg) == 0) { if (carg != NULL && strncasecmp(carg, arg, larg) == 0) {
return offset; return offset;
} } \
} }
return -1; return -1;
} }
@ -246,3 +247,16 @@ int RedisModule_TryGetValue(RedisModuleKey *key, const RedisModuleType *type, vo
return RMUTIL_VALUE_MISMATCH; 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));
}

@ -130,4 +130,19 @@ typedef enum {
*/ */
int RedisModule_TryGetValue(RedisModuleKey *key, const RedisModuleType *type, void **out); 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 #endif

Loading…
Cancel
Save