diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2022-04-03 21:28:08 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2022-04-04 13:48:11 +0200 |
commit | 5de24375515bc2932d299047e9cb8c9c9bf3ee1d (patch) | |
tree | 81c451b7ccbf90d6d3e219f26b62e98993fff7df /comphelper/source/misc | |
parent | c2e8a96a8107a37901e475c65a8e61211fc3b132 (diff) |
use string_view in comphelper::string::split
Change-Id: I4afe8aee85905ee35ba195b00b454aefa0ba38af
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/132486
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'comphelper/source/misc')
-rw-r--r-- | comphelper/source/misc/string.cxx | 55 |
1 files changed, 51 insertions, 4 deletions
diff --git a/comphelper/source/misc/string.cxx b/comphelper/source/misc/string.cxx index a3ee9bc58521..acdb6c88adcb 100644 --- a/comphelper/source/misc/string.cxx +++ b/comphelper/source/misc/string.cxx @@ -261,15 +261,62 @@ OUString convertCommaSeparated( return buf.makeStringAndClear(); } +/* copy of getToken from sal/, modified to take a string_view */ +static sal_Int32 getToken( OUString& ppThis, + std::u16string_view pStr, + sal_Int32 nToken, + sal_Unicode cTok, + sal_Int32 nIndex ) +{ + assert(nIndex <= static_cast<sal_Int32>(pStr.size())); + + // Set ppThis to an empty string and return -1 if either nToken or nIndex is + // negative: + if (nIndex >= 0 && nToken >= 0) + { + const auto* pOrgCharStr = pStr.data(); + const auto* pCharStr = pOrgCharStr + nIndex; + sal_Int32 nLen = pStr.size() - nIndex; + sal_Int32 nTokCount = 0; + const auto* pCharStrStart = pCharStr; + while (nLen > 0) + { + if (*pCharStr == cTok) + { + nTokCount++; + + if (nTokCount > nToken) + break; + if (nTokCount == nToken) + pCharStrStart = pCharStr + 1; + } + + pCharStr++; + nLen--; + } + if (nTokCount >= nToken) + { + ppThis = OUString(pCharStrStart, pCharStr - pCharStrStart); + if (nLen > 0) + return pCharStr - pOrgCharStr + 1; + else + return -1; + } + } + + ppThis.clear(); + return -1; +} + std::vector<OUString> - split(const OUString& rStr, sal_Unicode cSeparator) + split(std::u16string_view rStr, sal_Unicode cSeparator) { std::vector< OUString > vec; sal_Int32 idx = 0; do { - OUString kw = - rStr.getToken(0, cSeparator, idx); + OUString kw; + idx = getToken(kw, rStr, 0, cSeparator, idx); kw = kw.trim(); if (!kw.isEmpty()) { @@ -282,7 +329,7 @@ std::vector<OUString> } uno::Sequence< OUString > - convertCommaSeparated( OUString const& i_rString ) + convertCommaSeparated( std::u16string_view i_rString ) { std::vector< OUString > vec = split(i_rString, ','); return comphelper::containerToSequence(vec); |