forked from swilly22/redis-graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue_cmp.c
34 lines (28 loc) · 1.07 KB
/
value_cmp.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
#include "value.h"
#include "value_cmp.h"
#include <stdio.h>
#include <sys/param.h>
// Comparators for all simple types
GENERIC_CMP_FUNC_IMPL(cmp_float, floatval);
GENERIC_CMP_FUNC_IMPL(cmp_int, intval);
GENERIC_CMP_FUNC_IMPL(cmp_long, longval);
GENERIC_CMP_FUNC_IMPL(cmp_double, doubleval);
GENERIC_CMP_FUNC_IMPL(cmp_uint, uintval);
int cmp_string(void *p1, void *p2) {
SIValue *v1 = p1, *v2 = p2;
if (SIValue_IsInf(v1) || SIValue_IsNegativeInf(v2))
return 1;
if (SIValue_IsInf(v2) || SIValue_IsNegativeInf(v1))
return -1;
// compare the longest length possible, which is the shortest length of the
// two strings
int cmp = strncasecmp(v1->stringval.str, v2->stringval.str,
MIN(v2->stringval.len, v1->stringval.len));
// if the strings are equal at the common length but are not of the same
// length, the longer string wins
if (cmp == 0 && v1->stringval.len != v2->stringval.len) {
return v1->stringval.len > v2->stringval.len ? 1 : -1;
}
// if they are not equal, or equal and same length - return the original cmp
return cmp;
}