From fbff7af0a1a31e82c3a3eb6dac77d5a48ef3371d Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Fri, 29 Apr 2022 20:52:46 +0200 Subject: split comphelper::string::strip functions into String and view version which is more obvious, from the perspective of the caller, and lets us avoid creating a new String if nothing needs to be stripped Change-Id: I66a980eaf4aa818251bec49bdb16c2dddb0745e7 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/133657 Tested-by: Jenkins Reviewed-by: Noel Grandin --- comphelper/source/misc/string.cxx | 86 ++++++++++++++++++++++++++++++++++----- 1 file changed, 76 insertions(+), 10 deletions(-) (limited to 'comphelper') diff --git a/comphelper/source/misc/string.cxx b/comphelper/source/misc/string.cxx index acdb6c88adcb..a11a305c5daf 100644 --- a/comphelper/source/misc/string.cxx +++ b/comphelper/source/misc/string.cxx @@ -61,16 +61,43 @@ namespace return rIn.substr(i); } + template T tmpl_stripStartString(const T &rIn, + const C cRemove) + { + if (rIn.isEmpty()) + return rIn; + + sal_Int32 i = 0; + + while (i < rIn.getLength()) + { + if (rIn[i] != cRemove) + break; + ++i; + } + + return rIn.copy(i); + } } -OString stripStart(std::string_view rIn, char c) +OString stripStart(const OString& rIn, char c) { - return OString(tmpl_stripStart(rIn, c)); + return tmpl_stripStartString(rIn, c); } -OUString stripStart(std::u16string_view rIn, sal_Unicode c) +std::string_view stripStart(std::string_view rIn, char c) { - return OUString(tmpl_stripStart(rIn, c)); + return tmpl_stripStart(rIn, c); +} + +OUString stripStart(const OUString& rIn, sal_Unicode c) +{ + return tmpl_stripStartString(rIn, c); +} + +std::u16string_view stripStart(std::u16string_view rIn, sal_Unicode c) +{ + return tmpl_stripStart(rIn, c); } namespace @@ -92,25 +119,64 @@ namespace return rIn.substr(0, i); } + template T tmpl_stripEndString(const T &rIn, + const C cRemove) + { + if (rIn.isEmpty()) + return rIn; + + sal_Int32 i = rIn.getLength(); + + while (i > 0) + { + if (rIn[i-1] != cRemove) + break; + --i; + } + + return rIn.copy(0, i); + } +} + +OString stripEnd(const OString& rIn, char c) +{ + return tmpl_stripEndString(rIn, c); } -OString stripEnd(std::string_view rIn, char c) +std::string_view stripEnd(std::string_view rIn, char c) { - return OString(tmpl_stripEnd(rIn, c)); + return tmpl_stripEnd(rIn, c); } -OUString stripEnd(std::u16string_view rIn, sal_Unicode c) +OUString stripEnd(const OUString& rIn, sal_Unicode c) { - return OUString(tmpl_stripEnd(rIn, c)); + return tmpl_stripEndString(rIn, c); } -OString strip(std::string_view rIn, char c) +std::u16string_view stripEnd(std::u16string_view rIn, sal_Unicode c) +{ + return tmpl_stripEnd(rIn, c); +} + +OString strip(const OString& rIn, char c) +{ + auto x = tmpl_stripStartString(rIn, c); + return stripEnd(x, c); +} + +std::string_view strip(std::string_view rIn, char c) { auto x = tmpl_stripStart(rIn, c); return stripEnd(x, c); } -OUString strip(std::u16string_view rIn, sal_Unicode c) +OUString strip(const OUString& rIn, sal_Unicode c) +{ + auto x = tmpl_stripStartString(rIn, c); + return stripEnd(x, c); +} + +std::u16string_view strip(std::u16string_view rIn, sal_Unicode c) { auto x = tmpl_stripStart(rIn, c); return stripEnd(x, c); -- cgit v1.2.3