From 04af8ac89ecdc98361f52b792ff0ce1c3ccb4517 Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Thu, 3 Mar 2022 10:18:20 +0300 Subject: Use a more conventional begin() / end() syntax Also would potentially enable use in std range algorithms that take different end sentinel type. Change-Id: Ia75005caa7ce44302fc8612f5717a41d11087622 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130902 Tested-by: Jenkins Reviewed-by: Mike Kaganski --- sal/rtl/strtmpl.hxx | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) (limited to 'sal') diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx index 945aaa54a6e2..7234afd39ec1 100644 --- a/sal/rtl/strtmpl.hxx +++ b/sal/rtl/strtmpl.hxx @@ -30,7 +30,6 @@ #include "strimp.hxx" -#include #include #include #include @@ -56,8 +55,15 @@ template struct null_terminated { assert(pStr); } - auto getIter() const { return p; } - static auto getEndDetector() { return [](C* iter) { return *iter == 0; }; } + auto begin() const { return p; } + struct EndDetector + { + friend bool operator==(EndDetector, C* iter) { return *iter == 0; } + friend bool operator==(C* iter, EndDetector) { return *iter == 0; } + friend bool operator!=(EndDetector, C* iter) { return *iter != 0; } + friend bool operator!=(C* iter, EndDetector) { return *iter != 0; } + }; + static auto end() { return EndDetector{}; } }; template struct with_length @@ -70,8 +76,8 @@ template struct with_length { assert(len >= 0); } - auto getIter() const { return p; } - auto getEndDetector() const { return [pEnd = p + len](C* iter) { return iter == pEnd; }; } + auto begin() const { return p; } + auto end() const { return p + len; } }; template void Copy(C* _pDest, const C* _pSrc, sal_Int32 _nCount) @@ -166,17 +172,17 @@ sal_Int32 compare(S1 s1, S2 s2, Compare, Shorten_t shortenedLength) { static_assert(std::is_same_v || std::is_same_v); assert(shortenedLength >= 0); - auto pStr1 = s1.getIter(); - const auto atEnd1 = s1.getEndDetector(); - auto pStr2 = s2.getIter(); - const auto atEnd2 = s2.getEndDetector(); + auto pStr1 = s1.begin(); + const auto end1 = s1.end(); + auto pStr2 = s2.begin(); + const auto end2 = s2.end(); for (;;) { if (shortenedLength == 0) return 0; - if (atEnd2(pStr2)) - return atEnd1(pStr1) ? 0 : 1; - if (atEnd1(pStr1)) + if (pStr2 == end2) + return pStr1 == end1 ? 0 : 1; + if (pStr1 == end1) return -1; if (const sal_Int32 nRet = Compare::compare(*pStr1, *pStr2)) return nRet; @@ -753,13 +759,13 @@ template T toInt(S str, sal_Int16 nRadix) if ( (nRadix < RTL_STR_MIN_RADIX) || (nRadix > RTL_STR_MAX_RADIX) ) nRadix = 10; - auto pStr = str.getIter(); - const auto atEnd = str.getEndDetector(); + auto pStr = str.begin(); + const auto end = str.end(); /* Skip whitespaces */ - while (!atEnd(pStr) && rtl_ImplIsWhitespace(IMPL_RTL_USTRCODE(*pStr))) + while (pStr != end && rtl_ImplIsWhitespace(IMPL_RTL_USTRCODE(*pStr))) pStr++; - if (atEnd(pStr)) + if (pStr == end) return 0; const bool bNeg = HandleSignChar(pStr); @@ -767,7 +773,7 @@ template T toInt(S str, sal_Int16 nRadix) assert(nDiv > 0); std::make_unsigned_t n = 0; - while (!atEnd(pStr)) + while (pStr != end) { sal_Int16 nDigit = rtl_ImplGetDigit(IMPL_RTL_USTRCODE(*pStr), nRadix); if ( nDigit < 0 ) -- cgit v1.2.3