Changeset 929 for cpp/frams/util


Ignore:
Timestamp:
05/25/20 15:23:43 (5 years ago)
Author:
Maciej Komosinski
Message:

Introduced strSplit() - a function that splits an SString and stores results in any class that implements push_back(const SString&), e.g. std::vector<SString>

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/util/sstringutils.h

    r904 r929  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2020  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    2828
    2929SString trim(const SString& s); ///< remove leading/trailing whitespace
    30 SString concatPath(const SString& in1,const SString& in2); ///< concatenate path components inserting PATH_SEPARATOR_CHAR if not already present
     30SString concatPath(const SString& in1, const SString& in2); ///< concatenate path components inserting PATH_SEPARATOR_CHAR if not already present
    3131bool removeCR(SString& s); ///< remove '\r' return true if changed
    3232bool matchWildcard(const SString& word, const SString& pattern);///< '*' in pattern matches any substring
     
    3535bool parseUIDString(const char* str, char prefix, uint64_t &uid, bool err);
    3636
     37// SplitResult can be any class implementing push_back(const SString&), e.g. std::vector<SString>
     38template <class SplitResult> void strSplit(const SString& text, const SString& separator, bool merge_separators, SplitResult& result)
     39{
     40        int i, next = 0;
     41        bool first = true;
     42        if (!separator.len()) { result.push_back(text); return; }
     43        while (1)
     44        {
     45                i = text.indexOf(separator.c_str(), next);
     46                if (i < 0)
     47                {
     48                        if ((next <= text.len()) || first)
     49                                result.push_back(text.substr(next));
     50                        return;
     51                }
     52                if ((!merge_separators) || (i > next) || (i == 0))
     53                {
     54                        result.push_back(text.substr(next, i - next));
     55                        first = false;
     56                }
     57                next = i + separator.len();
     58        }
     59}
     60
     61
    3762#endif
Note: See TracChangeset for help on using the changeset viewer.