#include "split.hpp"

string_vector
split(std::string str, const std::string &sep, bool ignore_empty)
{
    std::vector<std::string> vec;

    bool end_sep = (sep.length() <= str.length() &&
                    str.substr(str.length() - sep.length()) == sep);

    for (;;) {
        const size_t find = str.find(sep);
        if (find == std::string::npos) {
            break;
        }
        std::string const bit = str.substr(0, find);
        if (!ignore_empty || !bit.empty()) {
            vec.push_back(bit);
        }
        str.erase(0, find + sep.length());
    }

    if (!str.empty()) {
        vec.push_back(str);
    }
    if (!ignore_empty && end_sep) {
        vec.push_back("");
    }
    return vec;
}
