summaryrefslogtreecommitdiff
path: root/include/o3tl
diff options
context:
space:
mode:
authorMike Kaganski <mike.kaganski@collabora.com>2023-04-12 16:48:30 +0200
committerMike Kaganski <mike.kaganski@collabora.com>2023-04-12 19:48:43 +0200
commitd4257802159e672035fab164b0cabd9222f18515 (patch)
treeec725d0a73e84ebffde3057510ee68690c1ba89d /include/o3tl
parentd446be648385a451c60ba3bd1657906f72a63e75 (diff)
Use o3tl::trim in strtmpl.hxx
Change-Id: I4de8658c36c21fe07fa45f697cf3145c567a95c0 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/150210 Tested-by: Jenkins Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'include/o3tl')
-rw-r--r--include/o3tl/string_view.hxx58
1 files changed, 25 insertions, 33 deletions
diff --git a/include/o3tl/string_view.hxx b/include/o3tl/string_view.hxx
index 2ccc4f828a03..c3a76433339a 100644
--- a/include/o3tl/string_view.hxx
+++ b/include/o3tl/string_view.hxx
@@ -362,7 +362,6 @@ constexpr bool ends_with(std::u16string_view sv, std::u16string_view x,
namespace internal
{
-// copy of implIsWhitespace from sal/rtl/strtmpl.hxx
inline bool implIsWhitespace(sal_Unicode c)
{
/* Space or Control character? */
@@ -370,12 +369,11 @@ inline bool implIsWhitespace(sal_Unicode c)
return true;
/* Only in the General Punctuation area Space or Control characters are included? */
- if ((c < 0x2000) || (c > 0x206F))
+ if ((c < 0x2000) || (c > 0x2029))
return false;
- if (((c >= 0x2000) && (c <= 0x200B)) || /* All Spaces */
- (c == 0x2028) || /* LINE SEPARATOR */
- (c == 0x2029)) /* PARAGRAPH SEPARATOR */
+ if ((c <= 0x200B) || /* U+2000 - U+200B All Spaces */
+ (c >= 0x2028)) /* U+2028 LINE SEPARATOR, U+2029 PARAGRAPH SEPARATOR */
return true;
return false;
@@ -383,47 +381,41 @@ inline bool implIsWhitespace(sal_Unicode c)
} // namespace internal
// Like OUString::trim, but for std::u16string_view:
-// copy of the trimView code from sal/rtl/strtmpl.hxx
inline std::u16string_view trim(std::u16string_view str)
{
- sal_Int32 nLen = str.size();
- sal_Int32 nPreSpaces = 0;
- sal_Int32 nPostSpaces = 0;
- sal_Int32 nIndex = str.size() - 1;
+ size_t nFirst = 0;
+ size_t nLast = str.size();
- while ((nPreSpaces < nLen) && internal::implIsWhitespace(*(str.data() + nPreSpaces)))
- nPreSpaces++;
+ while ((nFirst < nLast) && internal::implIsWhitespace(str.data()[nFirst]))
+ ++nFirst;
- while ((nIndex > nPreSpaces) && internal::implIsWhitespace(*(str.data() + nIndex)))
- {
- nPostSpaces++;
- nIndex--;
- }
+ if (nFirst == nLast)
+ return {};
+
+ do
+ --nLast;
+ while (internal::implIsWhitespace(str.data()[nLast]));
- return std::u16string_view{ str.data() + nPreSpaces,
- static_cast<size_t>(nLen - nPostSpaces - nPreSpaces) };
+ return { str.data() + nFirst, nLast - nFirst + 1 };
}
// Like OString::trim, but for std::string_view:
-// copy of the trimView code from sal/rtl/strtmpl.hxx
inline std::string_view trim(std::string_view str)
{
- sal_Int32 nLen = str.size();
- sal_Int32 nPreSpaces = 0;
- sal_Int32 nPostSpaces = 0;
- sal_Int32 nIndex = str.size() - 1;
+ size_t nFirst = 0;
+ size_t nLast = str.size();
- while ((nPreSpaces < nLen) && internal::implIsWhitespace(*(str.data() + nPreSpaces)))
- nPreSpaces++;
+ while ((nFirst < nLast) && internal::implIsWhitespace(str.data()[nFirst]))
+ ++nFirst;
- while ((nIndex > nPreSpaces) && internal::implIsWhitespace(*(str.data() + nIndex)))
- {
- nPostSpaces++;
- nIndex--;
- }
+ if (nFirst == nLast)
+ return {};
+
+ do
+ --nLast;
+ while (internal::implIsWhitespace(str.data()[nLast]));
- return std::string_view{ str.data() + nPreSpaces,
- static_cast<size_t>(nLen - nPostSpaces - nPreSpaces) };
+ return { str.data() + nFirst, nLast - nFirst + 1 };
}
// Like OString::toInt32, but for std::string_view: