diff --git a/rmutil/strings.c b/rmutil/strings.c index db20852..3933013 100644 --- a/rmutil/strings.c +++ b/rmutil/strings.c @@ -2,6 +2,7 @@ #include #include #include "strings.h" +#include "alloc.h" #include "sds.h" @@ -68,3 +69,13 @@ void RMUtil_StringToUpper(RedisModuleString *s) { ++c; } } + +void RMUtil_StringConvert(RedisModuleString **rs, const char **ss, size_t n, int options) { + for (size_t ii = 0; ii < n; ++ii) { + const char *p = RedisModule_StringPtrLen(rs[ii], NULL); + if (options & RMUTIL_STRINGCONVERT_COPY) { + p = strdup(p); + } + ss[ii] = p; + } +} \ No newline at end of file diff --git a/rmutil/strings.h b/rmutil/strings.h index c447957..eaef71e 100644 --- a/rmutil/strings.h +++ b/rmutil/strings.h @@ -25,4 +25,14 @@ void RMUtil_StringToLower(RedisModuleString *s); /* Converts a redis string to uppercase in place without reallocating anything */ void RMUtil_StringToUpper(RedisModuleString *s); + +// If set, copy the strings using strdup rather than simply storing pointers. +#define RMUTIL_STRINGCONVERT_COPY 1 + +/** + * Convert one or more RedisModuleString objects into `const char*`. + * Both rs and ss are arrays, and should be of length. + * Options may be 0 or `RMUTIL_STRINGCONVERT_COPY` + */ +void RMUtil_StringConvert(RedisModuleString **rs, const char **ss, size_t n, int options); #endif