MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey-v1.0 / StringSplit

Method StringSplit

Source/script2.cpp:7017–7106  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7015
7016
7017ResultType Line::StringSplit(char *aArrayName, char *aInputString, char *aDelimiterList, char *aOmitList)
7018{
7019 // Make it longer than Max so that FindOrAddVar() will be able to spot and report var names
7020 // that are too long, either because the base-name is too long, or the name becomes too long
7021 // as a result of appending the array index number:
7022 char var_name[MAX_VAR_NAME_LENGTH + 20];
7023 snprintf(var_name, sizeof(var_name), "%s0", aArrayName);
7024 Var *array0 = g_script.FindOrAddVar(var_name);
7025 if (!array0)
7026 return FAIL; // It will have already displayed the error.
7027
7028 if (!*aInputString) // The input variable is blank, thus there will be zero elements.
7029 return array0->Assign("0"); // Store the count in the 0th element.
7030
7031 DWORD next_element_number;
7032 Var *next_element;
7033
7034 if (*aDelimiterList) // The user provided a list of delimiters, so process the input variable normally.
7035 {
7036 char *contents_of_next_element, *delimiter, *new_starting_pos;
7037 size_t element_length;
7038 for (contents_of_next_element = aInputString, next_element_number = 1; ; ++next_element_number)
7039 {
7040 snprintf(var_name, sizeof(var_name), "%s%u", aArrayName, next_element_number);
7041
7042 // To help performance (in case the linked list of variables is huge), tell it where
7043 // to start the search. Use element #0 rather than the preceding element because,
7044 // for example, Array19 is alphabetially less than Array2, so we can't rely on the
7045 // numerical ordering:
7046 if ( !(next_element = g_script.FindOrAddVar(var_name, VAR_NAME_LENGTH_DEFAULT, array0)) )
7047 return FAIL; // It will have already displayed the error.
7048
7049 if (delimiter = StrChrAny(contents_of_next_element, aDelimiterList)) // A delimiter was found.
7050 {
7051 element_length = delimiter - contents_of_next_element;
7052 if (*aOmitList && element_length > 0)
7053 {
7054 contents_of_next_element = omit_leading_any(contents_of_next_element, aOmitList, element_length);
7055 element_length = delimiter - contents_of_next_element; // Update in case above changed it.
7056 if (element_length)
7057 element_length = omit_trailing_any(contents_of_next_element, aOmitList, delimiter - 1);
7058 }
7059 // If there are no chars to the left of the delim, or if they were all in the list of omitted
7060 // chars, the variable will be assigned the empty string:
7061 if (!next_element->Assign(contents_of_next_element, (VarSizeType)element_length))
7062 return FAIL;
7063 contents_of_next_element = delimiter + 1; // Omit the delimiter since it's never included in contents.
7064 }
7065 else // the entire length of contents_of_next_element is what will be stored
7066 {
7067 element_length = strlen(contents_of_next_element);
7068 if (*aOmitList && element_length > 0)
7069 {
7070 new_starting_pos = omit_leading_any(contents_of_next_element, aOmitList, element_length);
7071 element_length -= (new_starting_pos - contents_of_next_element); // Update in case above changed it.
7072 contents_of_next_element = new_starting_pos;
7073 if (element_length)
7074 // If this is true, the string must contain at least one char that isn't in the list

Callers

nothing calls this directly

Calls 6

snprintfFunction · 0.85
StrChrAnyFunction · 0.85
omit_leading_anyFunction · 0.85
omit_trailing_anyFunction · 0.85
FindOrAddVarMethod · 0.80
AssignMethod · 0.80

Tested by

no test coverage detected