forked from swilly22/redis-graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings.c
88 lines (70 loc) · 2.17 KB
/
strings.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <string.h>
#include <sys/param.h>
#include <ctype.h>
#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;
}
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;
}
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;
}
}
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;
}
}
void RMUtil_StringConcat(const Vector* rmStrings, const char* delimiter, char** concat) {
size_t length = 0;
// Compute length
for(int i = 0; i < Vector_Size(rmStrings); i++) {
RedisModuleString* string;
Vector_Get(rmStrings, i, &string);
size_t len;
RedisModule_StringPtrLen(string, &len);
length += len;
}
length += strlen(delimiter) * Vector_Size(rmStrings) + 1;
*concat = calloc(length, sizeof(char));
for(int i = 0; i < Vector_Size(rmStrings); i++) {
RedisModuleString* rmString;
Vector_Get(rmStrings, i, &rmString);
const char* string = RedisModule_StringPtrLen(rmString, NULL);
strcat(*concat, string);
strcat(*concat, delimiter);
}
// Discard last delimiter.
(*concat)[strlen(*concat) - strlen(delimiter)] = 0;
}