include/o3tl/string_view.hxx |   58 +++++++++++----------------
 sal/rtl/strtmpl.hxx          |   91 ++++++++++---------------------------------
 2 files changed, 48 insertions(+), 101 deletions(-)

New commits:
commit d4257802159e672035fab164b0cabd9222f18515
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Wed Apr 12 16:48:30 2023 +0200
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Wed Apr 12 19:48:43 2023 +0200

    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.kagan...@collabora.com>

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:
diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 7ec3af82cf94..cbf34fba2ed7 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -35,6 +35,7 @@
 #include "strimp.hxx"
 
 #include <o3tl/safeint.hxx>
+#include <o3tl/string_view.hxx>
 #include <osl/diagnose.h>
 #include <sal/log.hxx>
 #include <rtl/character.hxx>
@@ -153,24 +154,6 @@ inline sal_Int16 implGetDigit(sal_Unicode ch, sal_Int16 
nRadix)
     return (n < nRadix) ? n : -1;
 }
 
-inline bool implIsWhitespace(sal_Unicode c)
-{
-    /* Space or Control character? */
-    if ((c <= 32) && c)
-        return true;
-
-    /* Only in the General Punctuation area Space or Control characters are 
included? */
-    if ((c < 0x2000) || (c > 0x206F))
-        return false;
-
-    if (((c >= 0x2000) && (c <= 0x200B)) ||    /* All Spaces           */
-        (c == 0x2028) ||                       /* LINE SEPARATOR       */
-        (c == 0x2029))                         /* PARAGRAPH SEPARATOR  */
-        return true;
-
-    return false;
-}
-
 /* ======================================================================= */
 /* C-String functions which could be used without the String-Class         */
 /* ======================================================================= */
@@ -384,10 +367,9 @@ sal_Int32 indexOfChar_WithLength                           
  ( const IMPL_RTL_ST
     if (nLen <= 0)
         return -1;
     // take advantage of builtin optimisations
-    using my_string_view = std::basic_string_view<IMPL_RTL_STRCODE>;
-    my_string_view v(pStr, nLen);
-    typename my_string_view::size_type idx = v.find(c);
-    return idx == my_string_view::npos ? -1 : idx;
+    std::basic_string_view v(pStr, nLen);
+    auto idx = v.find(c);
+    return idx == v.npos ? -1 : idx;
 }
 
 /* ----------------------------------------------------------------------- */
@@ -430,10 +412,9 @@ sal_Int32 lastIndexOfChar_WithLength                       
      ( const IMPL_RT
 {
     assert(nLen >= 0);
     // take advantage of builtin optimisations
-    using my_string_view = std::basic_string_view<IMPL_RTL_STRCODE>;
-    my_string_view v(pStr, nLen);
-    typename my_string_view::size_type idx = v.rfind(c);
-    return idx == my_string_view::npos ? -1 : idx;
+    std::basic_string_view v(pStr, nLen);
+    auto idx = v.rfind(c);
+    return idx == v.npos ? -1 : idx;
 }
 
 /* ----------------------------------------------------------------------- */
@@ -484,10 +465,9 @@ sal_Int32 indexOfStr_WithLength                            
 ( const IMPL_RTL_STR
     if ( nSubLen == 0 )
         return -1;
     // take advantage of builtin optimisations
-    using my_string_view = std::basic_string_view<IMPL_RTL_STRCODE>;
-    my_string_view v(pStr, nStrLen);
+    std::basic_string_view v(pStr, nStrLen);
     auto idx = nSubLen == 1 ? v.find(*pSubStr) : v.find(pSubStr, 0, nSubLen);
-    return idx == my_string_view::npos ? -1 : idx;
+    return idx == v.npos ? -1 : idx;
 }
 
 inline sal_Int32 indexOfStr_WithLength(const sal_Unicode* pStr, sal_Int32 
nStrLen,
@@ -547,11 +527,10 @@ sal_Int32 lastIndexOfStr_WithLength                       
      ( const IMPL_RTL
     if ( nSubLen == 0 )
         return -1;
     // take advantage of builtin optimisations
-    using my_string_view = std::basic_string_view<IMPL_RTL_STRCODE>;
-    my_string_view v(pStr, nStrLen);
-    my_string_view needle(pSubStr, nSubLen);
-    typename my_string_view::size_type idx = v.rfind(needle);
-    return idx == my_string_view::npos ? -1 : idx;
+    std::basic_string_view v(pStr, nStrLen);
+    std::basic_string_view needle(pSubStr, nSubLen);
+    auto idx = v.rfind(needle);
+    return idx == v.npos ? -1 : idx;
 }
 
 /* ----------------------------------------------------------------------- */
@@ -564,41 +543,10 @@ template <class S, class Replacer> void replaceChars(S 
str, Replacer replacer)
 
 /* ----------------------------------------------------------------------- */
 
-template <typename IMPL_RTL_STRCODE> sal_Int32 
trim_WithLength(IMPL_RTL_STRCODE*, sal_Int32);
-
-template <typename IMPL_RTL_STRCODE> sal_Int32 trim( IMPL_RTL_STRCODE* pStr )
-{
-    return trim_WithLength( pStr, getLength( pStr ) );
-}
-
-/* ----------------------------------------------------------------------- */
-
-template <typename IMPL_RTL_STRCODE>
-std::basic_string_view<IMPL_RTL_STRCODE> trimView( IMPL_RTL_STRCODE* pStr, 
sal_Int32 nLen )
-{
-    assert(nLen >= 0);
-    sal_Int32 nPreSpaces    = 0;
-    sal_Int32 nPostSpaces   = 0;
-    sal_Int32 nIndex        = nLen-1;
-
-    while ( (nPreSpaces < nLen) && implIsWhitespace( 
IMPL_RTL_USTRCODE(*(pStr+nPreSpaces)) ) )
-        nPreSpaces++;
-
-    while ( (nIndex > nPreSpaces) && implIsWhitespace( 
IMPL_RTL_USTRCODE(*(pStr+nIndex)) ) )
-    {
-        nPostSpaces++;
-        nIndex--;
-    }
-
-    return { pStr + nPreSpaces, static_cast<size_t>(nLen - nPostSpaces - 
nPreSpaces) };
-}
-
-/* ----------------------------------------------------------------------- */
-
 template <typename IMPL_RTL_STRCODE>
 sal_Int32 trim_WithLength( IMPL_RTL_STRCODE* pStr, sal_Int32 nLen )
 {
-    const auto view = trimView(pStr, nLen);
+    const auto view = o3tl::trim(std::basic_string_view(pStr, nLen));
 
     if (static_cast<sal_Int32>(view.size()) != nLen)
     {
@@ -613,6 +561,13 @@ sal_Int32 trim_WithLength( IMPL_RTL_STRCODE* pStr, 
sal_Int32 nLen )
 
 /* ----------------------------------------------------------------------- */
 
+template <typename IMPL_RTL_STRCODE> sal_Int32 trim( IMPL_RTL_STRCODE* pStr )
+{
+    return trim_WithLength( pStr, getLength( pStr ) );
+}
+
+/* ----------------------------------------------------------------------- */
+
 template <typename IMPL_RTL_STRCODE> sal_Int32 valueOfBoolean( 
IMPL_RTL_STRCODE* pStr, sal_Bool b )
 {
     assert(pStr);
@@ -775,7 +730,7 @@ template <typename T, class S> T toInt(S str, sal_Int16 
nRadix)
     const auto end = str.end();
 
     /* Skip whitespaces */
-    while (pStr != end && implIsWhitespace(IMPL_RTL_USTRCODE(*pStr)))
+    while (pStr != end && 
o3tl::internal::implIsWhitespace(IMPL_RTL_USTRCODE(*pStr)))
         pStr++;
     if (pStr == end)
         return 0;
@@ -1274,7 +1229,7 @@ void newTrim                                ( 
IMPL_RTL_STRINGDATA** ppThis,
                                               IMPL_RTL_STRINGDATA* pStr )
 {
     assert(pStr);
-    const auto view = trimView(pStr->buffer, pStr->length);
+    const auto view = o3tl::trim(std::basic_string_view(pStr->buffer, 
pStr->length));
 
     if (static_cast<sal_Int32>(view.size()) == pStr->length)
         assign(ppThis, pStr);

Reply via email to