Skip to content

Instantly share code, notes, and snippets.

@keir-nellyer
Last active February 18, 2016 11:22
Show Gist options
  • Select an option

  • Save keir-nellyer/fbf4ebfc1d2f3bc62cac to your computer and use it in GitHub Desktop.

Select an option

Save keir-nellyer/fbf4ebfc1d2f3bc62cac to your computer and use it in GitHub Desktop.

Revisions

  1. keir-nellyer renamed this gist Feb 18, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. keir-nellyer revised this gist Feb 18, 2016. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions splt.cpp
    Original file line number Diff line number Diff line change
    @@ -53,4 +53,19 @@ vector<string> split(string input, string deliminator) {
    // 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);
    }
    }
  3. keir-nellyer created this gist Feb 16, 2016.
    56 changes: 56 additions & 0 deletions splt.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    /**
    * 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;
    }