From af5796d5bbcba94f5b2def9881419287d4eeb733 Mon Sep 17 00:00:00 2001 From: Dvir Volk Date: Tue, 27 Jun 2017 13:26:19 +0300 Subject: [PATCH] committing local changes to rmutil --- rmutil/alloc.h | 3 +- rmutil/sdsalloc.h | 5 +++ rmutil/strings.c | 77 ++++++++++++++++++++++++----------------------- rmutil/strings.h | 6 ++-- rmutil/util.c | 2 -- rmutil/vector.c | 6 ++-- rmutil/vector.h | 14 ++++----- 7 files changed, 61 insertions(+), 52 deletions(-) diff --git a/rmutil/alloc.h b/rmutil/alloc.h index 1a363d2..5e91652 100644 --- a/rmutil/alloc.h +++ b/rmutil/alloc.h @@ -41,10 +41,11 @@ char *rmalloc_strndup(const char *s, size_t n); #define strndup(s, n) rmalloc_strndup(s, n) #else + +#endif /* REDIS_MODULE_TARGET */ /* This function shold be called if you are working with malloc-patched code * ouside of redis, usually for unit tests. Call it once when entering your unit * tests' main() */ void RMUTil_InitAlloc(); -#endif /* REDIS_MODULE_TARGET */ #endif /* __RMUTIL_ALLOC__ */ diff --git a/rmutil/sdsalloc.h b/rmutil/sdsalloc.h index 6ba2e4c..1538fdf 100644 --- a/rmutil/sdsalloc.h +++ b/rmutil/sdsalloc.h @@ -36,6 +36,11 @@ * the include of your alternate allocator if needed (not needed in order * to use the default libc allocator). */ +#if defined(__MACH__) +#include +#else +#include +#endif //#include "zmalloc.h" #define s_malloc malloc #define s_realloc realloc diff --git a/rmutil/strings.c b/rmutil/strings.c index 0ebbb3f..db20852 100644 --- a/rmutil/strings.c +++ b/rmutil/strings.c @@ -3,65 +3,68 @@ #include #include "strings.h" - #include "sds.h" // RedisModuleString *RMUtil_CreateFormattedString(RedisModuleCtx *ctx, const char *fmt, ...) { // sds s = sdsempty(); - + // va_list ap; // va_start(ap, fmt); // s = sdscatvprintf(s, fmt, ap); // va_end(ap); - + // RedisModuleString *ret = RedisModule_CreateString(ctx, (const char *)s, sdslen(s)); // sdsfree(s); // return ret; // } int RMUtil_StringEquals(RedisModuleString *s1, RedisModuleString *s2) { - - - const char *c1, *c2; - size_t l1, l2; - c1 = RedisModule_StringPtrLen(s1, &l1); - c2 = RedisModule_StringPtrLen(s2, &l2); - if (l1 != l2) return 0; - return strncmp(c1, c2, l1) == 0; + const char *c1, *c2; + size_t l1, l2; + c1 = RedisModule_StringPtrLen(s1, &l1); + c2 = RedisModule_StringPtrLen(s2, &l2); + if (l1 != l2) return 0; + + return strncmp(c1, c2, l1) == 0; } int RMUtil_StringEqualsC(RedisModuleString *s1, const char *s2) { - - - const char *c1; - size_t l1, l2 = strlen(s2); - c1 = RedisModule_StringPtrLen(s1, &l1); - if (l1 != l2) return 0; - - return strncmp(c1, s2, l1) == 0; + + const char *c1; + size_t l1, l2 = strlen(s2); + c1 = RedisModule_StringPtrLen(s1, &l1); + if (l1 != l2) return 0; + + return strncmp(c1, s2, l1) == 0; +} +int RMUtil_StringEqualsCaseC(RedisModuleString *s1, const char *s2) { + + const char *c1; + size_t l1, l2 = strlen(s2); + c1 = RedisModule_StringPtrLen(s1, &l1); + if (l1 != l2) return 0; + + return strncasecmp(c1, s2, l1) == 0; } void RMUtil_StringToLower(RedisModuleString *s) { - - size_t l; - char *c = (char *)RedisModule_StringPtrLen(s, &l); - size_t i; - for (i = 0; i < l; i++) { - *c = tolower(*c); - ++c; - } - + + size_t l; + char *c = (char *)RedisModule_StringPtrLen(s, &l); + size_t i; + for (i = 0; i < l; i++) { + *c = tolower(*c); + ++c; + } } void RMUtil_StringToUpper(RedisModuleString *s) { - size_t l; - char *c = (char *)RedisModule_StringPtrLen(s, &l); - size_t i; - for (i = 0; i < l; i++) { - *c = toupper(*c); - ++c; - } - - + size_t l; + char *c = (char *)RedisModule_StringPtrLen(s, &l); + size_t i; + for (i = 0; i < l; i++) { + *c = toupper(*c); + ++c; + } } diff --git a/rmutil/strings.h b/rmutil/strings.h index d637025..c447957 100644 --- a/rmutil/strings.h +++ b/rmutil/strings.h @@ -8,16 +8,18 @@ * Note that RedisModuleString objects CANNOT be used as formatting arguments. */ // DEPRECATED since it was added to the RedisModule API. Replaced with a macro below -//RedisModuleString *RMUtil_CreateFormattedString(RedisModuleCtx *ctx, const char *fmt, ...); +// RedisModuleString *RMUtil_CreateFormattedString(RedisModuleCtx *ctx, const char *fmt, ...); #define RMUtil_CreateFormattedString RedisModule_CreateStringPrintf - /* Return 1 if the two strings are equal. Case *sensitive* */ int RMUtil_StringEquals(RedisModuleString *s1, RedisModuleString *s2); /* Return 1 if the string is equal to a C NULL terminated string. Case *sensitive* */ int RMUtil_StringEqualsC(RedisModuleString *s1, const char *s2); +/* Return 1 if the string is equal to a C NULL terminated string. Case *insensitive* */ +int RMUtil_StringEqualsCaseC(RedisModuleString *s1, const char *s2); + /* Converts a redis string to lowercase in place without reallocating anything */ void RMUtil_StringToLower(RedisModuleString *s); diff --git a/rmutil/util.c b/rmutil/util.c index 8ca572a..be088eb 100644 --- a/rmutil/util.c +++ b/rmutil/util.c @@ -71,7 +71,6 @@ RMUtilInfo *RMUtil_GetRedisInfo(RedisModuleCtx *ctx) { char *key = strsep(&line, ":"); info->entries[i].key = key; info->entries[i].val = line; - //printf("Got info '%s' = '%s'\n", key, line); i++; if (i >= cap) { cap *= 2; @@ -123,7 +122,6 @@ int RMUtilInfo_GetDouble(RMUtilInfo *info, const char *key, double *d) { } *d = strtod(p, NULL); - //printf("p: %s, d: %f\n", p, *d); if ((errno == ERANGE && (*d == HUGE_VAL || *d == -HUGE_VAL)) || (errno != 0 && *d == 0)) { return 0; } diff --git a/rmutil/vector.c b/rmutil/vector.c index 5402821..8fe5ddc 100644 --- a/rmutil/vector.c +++ b/rmutil/vector.c @@ -6,7 +6,7 @@ inline int __vector_PushPtr(Vector *v, void *elem) { Vector_Resize(v, v->cap ? v->cap * 2 : 1); } - __vector_PutPtr(v, v->top, elem); + __vector_PutPtr(v, v->top++, elem); return v->top; } @@ -43,7 +43,7 @@ inline int __vector_PutPtr(Vector *v, size_t pos, void *elem) { } else { memset(v->data + pos * v->elemSize, 0, v->elemSize); } - // move the end offset to pos + 1 if we grew + // move the end offset to pos if we grew if (pos >= v->top) { v->top = pos + 1; } @@ -80,6 +80,8 @@ void Vector_Free(Vector *v) { free(v); } + +/* return the used size of the vector, regardless of capacity */ inline int Vector_Size(Vector *v) { return v->top; } /* return the actual capacity */ diff --git a/rmutil/vector.h b/rmutil/vector.h index f4aa51c..a3b606f 100644 --- a/rmutil/vector.h +++ b/rmutil/vector.h @@ -10,10 +10,10 @@ * Works like C++ std::vector with an underlying resizable buffer */ typedef struct { - char *data; - size_t elemSize; - size_t cap; - size_t top; + char *data; + size_t elemSize; + size_t cap; + size_t top; } Vector; @@ -47,13 +47,11 @@ int Vector_Pop(Vector *v, void *ptr); * Put an element at pos. * Note: If pos is outside the vector capacity, we resize it accordingly */ -#define Vector_Put(v, pos, elem) \ - __vector_PutPtr(v, pos, elem ? &(typeof(elem)){elem} : NULL) +#define Vector_Put(v, pos, elem) __vector_PutPtr(v, pos, elem ? &(typeof(elem)){elem} : NULL) /* Push an element at the end of v, resizing it if needed. This macro wraps * __vector_PushPtr */ -#define Vector_Push(v, elem) \ - __vector_PushPtr(v, elem ? &(typeof(elem)){elem} : NULL) +#define Vector_Push(v, elem) __vector_PushPtr(v, elem ? &(typeof(elem)){elem} : NULL) int __vector_PushPtr(Vector *v, void *elem);