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

Function AR_SPLIT

src/arithmetic/string_funcs/string_funcs.c:665–723  ·  view source on GitHub ↗

returns a list of strings resulting from the splitting of the original string around matches of the given delimiter

Source from the content-addressed store, hash-verified

663
664// returns a list of strings resulting from the splitting of the original string around matches of the given delimiter
665SIValue AR_SPLIT(SIValue *argv, int argc, void *private_data) {
666 if(SIValue_IsNull(argv[0]) || SIValue_IsNull(argv[1])) {
667 return SI_NullVal();
668 }
669
670 char *str = argv[0].stringval;
671 const char *delimiter = argv[1].stringval;
672 size_t str_len = strlen(str);
673 size_t delimiter_len = strlen(delimiter);
674 SIValue tokens = SIArray_New(1);
675
676 if(delimiter_len == 0) {
677 if(strlen(str) == 0) {
678 SIArray_Append(&tokens, SI_ConstStringVal(""));
679 } else {
680 utf8proc_int32_t c;
681 utf8proc_uint8_t token[5];
682 const utf8proc_uint8_t *str_i = (const utf8proc_uint8_t *)str;
683 while(str_i[0] != 0) {
684 str_i += utf8proc_iterate(str_i, -1, &c);
685 int i = utf8proc_encode_char(utf8proc_tolower(c), token);
686 token[i] = '\0';
687 SIArray_Append(&tokens, SI_ConstStringVal((const char *)token));
688 }
689 }
690 } else if(str_len == 0) {
691 SIArray_Append(&tokens, SI_ConstStringVal(""));
692 } else {
693 size_t rest_len = str_len;
694 const char *start = str;
695 bool delimiter_found = false;
696 while(rest_len >= delimiter_len) {
697 // find bytes length from start to delimiter
698 int len = 0;
699 delimiter_found = false;
700 while(len <= rest_len - delimiter_len) {
701 if(strncmp(start + len, delimiter, delimiter_len) == 0) {
702 delimiter_found = true;
703 break;
704 }
705 len++;
706 }
707 if(!delimiter_found) {
708 break;
709 }
710 SIValue si_token = SI_TransferStringVal(rm_strndup(start, len));
711 SIArray_Append(&tokens, si_token);
712 SIValue_Free(si_token);
713 start += len + delimiter_len;
714 rest_len -= len + delimiter_len;
715 }
716 if(rest_len > 0 || delimiter_found) {
717 SIValue si_token = SI_ConstStringVal(start);
718 SIArray_Append(&tokens, si_token);
719 }
720 }
721
722 return tokens;

Callers

nothing calls this directly

Calls 8

SIValue_IsNullFunction · 0.85
SI_NullValFunction · 0.85
SIArray_NewFunction · 0.85
SIArray_AppendFunction · 0.85
SI_ConstStringValFunction · 0.85
SI_TransferStringValFunction · 0.85
rm_strndupFunction · 0.85
SIValue_FreeFunction · 0.85

Tested by

no test coverage detected