Skip to content

Instantly share code, notes, and snippets.

@keir-nellyer
Last active February 18, 2016 11:22
Show Gist options
  • Save keir-nellyer/fbf4ebfc1d2f3bc62cac to your computer and use it in GitHub Desktop.
Save keir-nellyer/fbf4ebfc1d2f3bc62cac to your computer and use it in GitHub Desktop.
C++ Split Function
/**
* This method takes a string input and splits it at every instance of the defined deliminator
* None of the results will include the deliminator, here are some example inputs and their corresponding output
* Assume deliminator is ", " every time
*
* hey, this is a message, :) ["hey", "this is a message", ":)"]
* , hello! ["hello"]
* how, are you?, ["how", "are you?"]
*/
vector<string> split(string input, string deliminator) {
vector<string> output(2);
unsigned long i = 0;
unsigned long inputLength = input.length();
unsigned long delimLength = deliminator.length();
unsigned long startIndex = 0;
unsigned long delimIndex = 0;
while ((delimIndex = input.find(deliminator, startIndex)) != string::npos) {
// check the deliminator is not at the very beginning of the string to avoid errors
unsigned long substrLength = delimIndex - (delimIndex != 0 ? startIndex : 0);
// upsize the vector (if needed) to accommodate new values
upsizeVector(output, i);
// get the left side of the split
output.at(i) = input.substr(startIndex, substrLength);
// move the start index to after the first deliminator
startIndex = delimIndex + delimLength;
// calculate the index of the next deliminator
// if not found, this will read to the end of the string
unsigned long endIndex = input.find(deliminator, startIndex);
if (endIndex == string::npos) endIndex = inputLength;
// check we actually have a string to append (ie we're not at the end of the string)
if (startIndex != endIndex) {
upsizeVector(output, ++i);
// get the right side of the split
output.at(i) = input.substr(startIndex, endIndex);
// calculate the next start index
startIndex = endIndex + (endIndex != inputLength ? delimLength : 0);
}
// we MUST increment this even if we have reached the end of the string
// failing to do so will result in the last element from the split being missing
i++;
}
// dispose of un-used space in the Vector
output.resize(i);
return output;
}
/**
* Takes in a vector and the index value about to be written to.
* If the index is higher than the max_size of the vector, the vector is scaled up (+10).
* Otherwise this function does nothing.
* The vector may need down-sized after values have been written.
*/
template <typename T>
void upsizeVector(vector<T> &v, unsigned long index) {
if (index >= v.size()) {
// up-size by 10, so we don't constantly up-size
// the developer is responsible for down-sizing this later
v.resize(v.size() + 10);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment