summaryrefslogtreecommitdiff
path: root/sal
diff options
context:
space:
mode:
authorMike Kaganski <mike.kaganski@collabora.com>2023-05-05 20:58:24 +0200
committerMike Kaganski <mike.kaganski@collabora.com>2023-05-06 07:24:57 +0200
commit4cedea47f9f97ec04e25e36cea38a2fb536bb1bc (patch)
tree368e8d8d7355d935592662fab864d27ef389c28e /sal
parent1db728a70cd2378bfd31c5ea76facd223175f823 (diff)
Simplify rtl_(u)string_newReplace implementation
and unify with *ToAscii(Lower/Upper)Case Change-Id: I06999b4f5f34abc8da2860b7f9e279608edb40dc Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151381 Tested-by: Jenkins Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'sal')
-rw-r--r--sal/rtl/string.cxx6
-rw-r--r--sal/rtl/strtmpl.hxx44
-rw-r--r--sal/rtl/ustring.cxx6
3 files changed, 17 insertions, 39 deletions
diff --git a/sal/rtl/string.cxx b/sal/rtl/string.cxx
index 23196e528682..06cde3c4faad 100644
--- a/sal/rtl/string.cxx
+++ b/sal/rtl/string.cxx
@@ -638,19 +638,19 @@ void SAL_CALL rtl_string_newReplaceStrAt_WithLength(rtl_String** ppThis, rtl_Str
void SAL_CALL rtl_string_newReplace(rtl_String** ppThis, rtl_String* pStr, char cOld, char cNew)
SAL_THROW_EXTERN_C()
{
- rtl::str::newReplace(ppThis, pStr, cOld, cNew);
+ rtl::str::newReplaceChars(ppThis, pStr, rtl::str::FromTo(cOld, cNew));
}
void SAL_CALL rtl_string_newToAsciiLowerCase(rtl_String** ppThis, rtl_String* pStr)
SAL_THROW_EXTERN_C()
{
- rtl::str::newReplaceChars<rtl::str::ToAsciiLower>(ppThis, pStr);
+ rtl::str::newReplaceChars(ppThis, pStr, rtl::str::toAsciiLower);
}
void SAL_CALL rtl_string_newToAsciiUpperCase(rtl_String** ppThis, rtl_String* pStr)
SAL_THROW_EXTERN_C()
{
- rtl::str::newReplaceChars<rtl::str::ToAsciiUpper>(ppThis, pStr);
+ rtl::str::newReplaceChars(ppThis, pStr, rtl::str::toAsciiUpper);
}
void SAL_CALL rtl_string_newTrim(rtl_String** ppThis, rtl_String* pStr) SAL_THROW_EXTERN_C()
diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index ba182f87817e..cbbef030198f 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -87,35 +87,20 @@ template <typename C> struct with_length
auto end() const { return p + len; }
};
-struct ToAsciiLower
+template <bool (&fApplicable)(sal_uInt32), sal_uInt32 (&fReplace)(sal_uInt32)> struct CaseReplace
{
- template <typename C> static bool Applicable(C c)
- {
- return rtl::isAsciiUpperCase(UChar(c));
- }
- template <typename C> static C Replace(C c)
- {
- return rtl::toAsciiLowerCase(UChar(c));
- }
-} constexpr toAsciiLower;
-
-struct ToAsciiUpper
-{
- template <typename C> static bool Applicable(C c)
- {
- return rtl::isAsciiLowerCase(UChar(c));
- }
- template <typename C> static C Replace(C c)
- {
- return rtl::toAsciiUpperCase(UChar(c));
- }
-} constexpr toAsciiUpper;
+ static auto Applicable() { return [](auto c) { return fApplicable(UChar(c)); }; }
+ template <typename C> static C Replace(C c) { return fReplace(UChar(c)); }
+};
+constexpr CaseReplace<rtl::isAsciiUpperCase, rtl::toAsciiLowerCase> toAsciiLower;
+constexpr CaseReplace<rtl::isAsciiLowerCase, rtl::toAsciiUpperCase> toAsciiUpper;
template <typename C> struct FromTo
{
C from;
C to;
FromTo(C cFrom, C cTo) : from(cFrom), to(cTo) {}
+ auto Applicable() const { return [this](C c) { return c == from; }; }
C Replace(C c) const { return c == from ? to : c; }
};
@@ -1112,14 +1097,14 @@ void newReplaceStrAt(rtl_tString** ppThis, rtl_tString* pStr, sal_Int32 nIndex,
/* ----------------------------------------------------------------------- */
-template <class Traits, typename rtl_tString>
-void newReplaceChars(rtl_tString** ppThis, rtl_tString* pStr)
+template <typename rtl_tString, class Replacer>
+void newReplaceChars(rtl_tString** ppThis, rtl_tString* pStr, Replacer replacer)
{
assert(ppThis);
assert(pStr);
const auto pEnd = pStr->buffer + pStr->length;
- auto pCharStr = std::find_if(pStr->buffer, pEnd, [](auto c) { return Traits::Applicable(c); });
+ auto pCharStr = std::find_if(pStr->buffer, pEnd, replacer.Applicable());
if (pCharStr != pEnd)
{
rtl_tString* pOrg = *ppThis;
@@ -1133,7 +1118,7 @@ void newReplaceChars(rtl_tString** ppThis, rtl_tString* pStr)
/* replace/copy rest of the string */
do
{
- *pNewCharStr = Traits::Replace(*pCharStr);
+ *pNewCharStr = replacer.Replace(*pCharStr);
pNewCharStr++;
pCharStr++;
} while (pCharStr != pEnd);
@@ -1367,13 +1352,6 @@ void newReplaceAllFromIndex(S** s, S* s1, CharTypeFrom const* from, sal_Int32 fr
RTL_LOG_STRING_NEW(*s);
}
-template <typename rtl_tString>
-void newReplace(rtl_tString** ppThis, rtl_tString* pStr,
- Char_T<rtl_tString> cOld, Char_T<rtl_tString> cNew)
-{
- return newReplaceAllFromIndex(ppThis, pStr, &cOld, 1, &cNew, 1, 0);
-}
-
template <class rtl_tString, typename C1, typename C2>
void newReplaceFirst(rtl_tString** s, rtl_tString* s1, C1 const* from, sal_Int32 fromLength,
C2 const* to, sal_Int32 toLength, sal_Int32& fromIndex)
diff --git a/sal/rtl/ustring.cxx b/sal/rtl/ustring.cxx
index fc23cf37a338..c863be00512b 100644
--- a/sal/rtl/ustring.cxx
+++ b/sal/rtl/ustring.cxx
@@ -1272,19 +1272,19 @@ void SAL_CALL rtl_uString_newReplaceStrAtUtf16L(rtl_uString** ppThis, rtl_uStrin
void SAL_CALL rtl_uString_newReplace(rtl_uString** ppThis, rtl_uString* pStr, sal_Unicode cOld,
sal_Unicode cNew) SAL_THROW_EXTERN_C()
{
- rtl::str::newReplace(ppThis, pStr, cOld, cNew);
+ rtl::str::newReplaceChars(ppThis, pStr, rtl::str::FromTo(cOld, cNew));
}
void SAL_CALL rtl_uString_newToAsciiLowerCase(rtl_uString** ppThis, rtl_uString* pStr)
SAL_THROW_EXTERN_C()
{
- rtl::str::newReplaceChars<rtl::str::ToAsciiLower>(ppThis, pStr);
+ rtl::str::newReplaceChars(ppThis, pStr, rtl::str::toAsciiLower);
}
void SAL_CALL rtl_uString_newToAsciiUpperCase(rtl_uString** ppThis, rtl_uString* pStr)
SAL_THROW_EXTERN_C()
{
- rtl::str::newReplaceChars<rtl::str::ToAsciiUpper>(ppThis, pStr);
+ rtl::str::newReplaceChars(ppThis, pStr, rtl::str::toAsciiUpper);
}
void SAL_CALL rtl_uString_newTrim(rtl_uString** ppThis, rtl_uString* pStr) SAL_THROW_EXTERN_C()