Пример #1
0
__LLBC_NS_BEGIN

void LLBC_SplitString(const LLBC_String &str,
                      const LLBC_String &separator,
                      std::vector<LLBC_String> &destStrList,
                      bool justSplitFirst,
                      char escapeChar)
{
    if (UNLIKELY(str.empty()))
    {
        return;
    }

    if (UNLIKELY(separator.empty()))
    {
        destStrList.push_back(str);
    }

    LLBC_String::size_type curPos = 0;
    LLBC_String::size_type prevPos = 0;

    LLBC_String strInternal = str;
    while ((curPos = strInternal.find(separator, curPos)) != LLBC_String::npos)
    {
        if (curPos != 0 && strInternal[curPos - 1] == escapeChar)
        {
            strInternal.erase(-- curPos, 1);
            curPos += separator.size();
            continue;
        }

        LLBC_String temp = strInternal.substr(prevPos, curPos - prevPos);
        destStrList.push_back(temp);

        if (justSplitFirst)
        {
            destStrList.push_back(strInternal.substr(curPos + separator.size()));
            return;
        }

        curPos += separator.size();
        prevPos = curPos;
    }

    LLBC_String temp = strInternal.substr(prevPos);
    if (!temp.empty())
    {   
        destStrList.push_back(temp);
    }
}