Ad
Code
Diff
  • #include <string>
    #include <vector>
    #include <string_view>
    
    auto split(std::string_view s, const char d, bool strip = false) noexcept {
        std::vector<std::string> r;
        r.reserve(16);
        char b[64];
        int n = 0;
        
        for (auto c : s) {
            if (c == d) {
                if (!strip || n) {
                    if (n < 64) r.emplace_back(b, n);
                    else r.emplace_back(std::string(b, n));
                }
                n = 0;
            } else b[n++] = c;
        }
        
        if (!strip || n) {
            if (n < 64) r.emplace_back(b, n);
            else r.emplace_back(std::string(b, n));
        }
        
        return r;
    }
    • #include <string>
    • #include <vector>
    • #include <string_view>
    • using std::string;
    • using std::vector;
    • vector<string> split(std::string_view str, char sep, bool strip = true)
    • {
    • vector<string> result;
    • string buf;
    • for (char c : str) {
    • if (c == sep) {
    • if (!strip || !buf.empty()) {
    • result.emplace_back(buf);
    • buf.clear();
    • }
    • } else buf.push_back(c);
    • }
    • if (!strip || !buf.empty()) result.emplace_back(buf);
    • return result;
    • }
    • auto split(std::string_view s, const char d, bool strip = false) noexcept {
    • std::vector<std::string> r;
    • r.reserve(16);
    • char b[64];
    • int n = 0;
    • for (auto c : s) {
    • if (c == d) {
    • if (!strip || n) {
    • if (n < 64) r.emplace_back(b, n);
    • else r.emplace_back(std::string(b, n));
    • }
    • n = 0;
    • } else b[n++] = c;
    • }
    • if (!strip || n) {
    • if (n < 64) r.emplace_back(b, n);
    • else r.emplace_back(std::string(b, n));
    • }
    • return r;
    • }