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>
This commit is contained in:
Mike Kaganski
2023-05-05 20:58:24 +02:00
parent 1db728a70c
commit 4cedea47f9
3 changed files with 17 additions and 39 deletions

View File

@@ -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()

View File

@@ -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)

View File

@@ -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()