From 2ce8734f03a3cf1c3d5a0da3626e5cde21af2eb0 Mon Sep 17 00:00:00 2001 From: Mark Nunberg Date: Wed, 12 Jul 2017 10:07:08 +0300 Subject: [PATCH] Add RMUtil_StringConvert --- rmutil/strings.c | 11 +++++++++++ rmutil/strings.h | 10 ++++++++++ 2 files changed, 21 insertions(+) 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