MCPcopy Create free account
hub / github.com/RedisGraph/RedisGraph / AR_LEFT

Function AR_LEFT

src/arithmetic/string_funcs/string_funcs.c:26–56  ·  view source on GitHub ↗

returns a string containing the specified number of leftmost characters of the original string.

Source from the content-addressed store, hash-verified

24
25// returns a string containing the specified number of leftmost characters of the original string.
26SIValue AR_LEFT(SIValue *argv, int argc, void *private_data) {
27 if(SIValue_IsNull(argv[0])) return SI_NullVal();
28
29 int64_t newlen = -1;
30 if(SI_TYPE(argv[1]) == T_INT64) {
31 newlen = argv[1].longval;
32 }
33 if(newlen < 0) {
34 ErrorCtx_SetError("length must be a non-negative integer");
35 return SI_NullVal();
36 }
37
38 if(strlen(argv[0].stringval) <= newlen) {
39 // No need to truncate this string based on the requested length
40 return SI_DuplicateStringVal(argv[0].stringval);
41 }
42
43 // determine new string byte size
44 utf8proc_int32_t c;
45 int64_t newlen_bytes = 0;
46 const char *str = argv[0].stringval;
47 for (int i = 0; i < newlen; i++) {
48 newlen_bytes += utf8proc_iterate((const utf8proc_uint8_t *)(str+newlen_bytes), -1, &c);
49 }
50
51 char *left_str = rm_malloc((newlen_bytes + 1) * sizeof(char));
52 strncpy(left_str, str, newlen_bytes * sizeof(char));
53 left_str[newlen_bytes] = '\0';
54
55 return SI_TransferStringVal(left_str);
56}
57
58// returns the original string with leading whitespace removed.
59SIValue AR_LTRIM(SIValue *argv, int argc, void *private_data) {

Callers

nothing calls this directly

Calls 6

SIValue_IsNullFunction · 0.85
SI_NullValFunction · 0.85
ErrorCtx_SetErrorFunction · 0.85
SI_DuplicateStringValFunction · 0.85
rm_mallocFunction · 0.85
SI_TransferStringValFunction · 0.85

Tested by

no test coverage detected