diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2022-06-15 11:26:55 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2022-06-15 15:52:32 +0200 |
commit | bfb0aa8ca36956690c963e3edfb1a2d78e908ba5 (patch) | |
tree | c7a7e127fcf585288ef1170e2cebefc75835ac28 /comphelper | |
parent | 8611bf020248d2b94e673465e22bd1db80f7eafd (diff) |
elide temporary string in comphelper::strip
Change-Id: I093768c617490679d295cce37cc3ffe67c8e940a
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/135871
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'comphelper')
-rw-r--r-- | comphelper/source/misc/string.cxx | 63 |
1 files changed, 55 insertions, 8 deletions
diff --git a/comphelper/source/misc/string.cxx b/comphelper/source/misc/string.cxx index 3b875a78e9a4..4fcd00078bfe 100644 --- a/comphelper/source/misc/string.cxx +++ b/comphelper/source/misc/string.cxx @@ -161,28 +161,75 @@ std::u16string_view stripEnd(std::u16string_view rIn, sal_Unicode c) return tmpl_stripEnd<std::u16string_view, sal_Unicode>(rIn, c); } +namespace +{ + template <typename T, typename C> T tmpl_strip(const T &rIn, + const C cRemove) + { + if (rIn.empty()) + return rIn; + + typename T::size_type end = rIn.size(); + while (end > 0) + { + if (rIn[end-1] != cRemove) + break; + --end; + } + + typename T::size_type start = 0; + while (start < end) + { + if (rIn[start] != cRemove) + break; + ++start; + } + + return rIn.substr(start, end - start); + } + template <typename T, typename C> T tmpl_stripString(const T &rIn, + const C cRemove) + { + if (rIn.isEmpty()) + return rIn; + + sal_Int32 end = rIn.getLength(); + while (end > 0) + { + if (rIn[end-1] != cRemove) + break; + --end; + } + sal_Int32 start = 0; + while (start < end) + { + if (rIn[start] != cRemove) + break; + ++start; + } + + return rIn.copy(start, end - start); + } +} + OString strip(const OString& rIn, char c) { - auto x = tmpl_stripStartString<OString, char>(rIn, c); - return stripEnd(x, c); + return tmpl_stripString<OString, char>(rIn, c); } std::string_view strip(std::string_view rIn, char c) { - auto x = tmpl_stripStart<std::string_view, char>(rIn, c); - return stripEnd(x, c); + return tmpl_strip<std::string_view, char>(rIn, c); } OUString strip(const OUString& rIn, sal_Unicode c) { - auto x = tmpl_stripStartString<OUString, sal_Unicode>(rIn, c); - return stripEnd(x, c); + return tmpl_stripString<OUString, sal_Unicode>(rIn, c); } std::u16string_view strip(std::u16string_view rIn, sal_Unicode c) { - auto x = tmpl_stripStart<std::u16string_view, sal_Unicode>(rIn, c); - return stripEnd(x, c); + return tmpl_strip<std::u16string_view, sal_Unicode>(rIn, c); } namespace |