elide temporary string in comphelper::strip

Change-Id: I093768c617490679d295cce37cc3ffe67c8e940a
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/135871
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
This commit is contained in:
Noel Grandin 2022-06-15 11:26:55 +02:00
parent 8611bf0202
commit bfb0aa8ca3

View file

@ -161,28 +161,75 @@ std::u16string_view stripEnd(std::u16string_view rIn, sal_Unicode c)
return tmpl_stripEnd<std::u16string_view, sal_Unicode>(rIn, c);
}
namespace
{
template <typename T, typename C> T tmpl_strip(const T &rIn,
const C cRemove)
{
if (rIn.empty())
return rIn;
typename T::size_type end = rIn.size();
while (end > 0)
{
if (rIn[end-1] != cRemove)
break;
--end;
}
typename T::size_type start = 0;
while (start < end)
{
if (rIn[start] != cRemove)
break;
++start;
}
return rIn.substr(start, end - start);
}
template <typename T, typename C> T tmpl_stripString(const T &rIn,
const C cRemove)
{
if (rIn.isEmpty())
return rIn;
sal_Int32 end = rIn.getLength();
while (end > 0)
{
if (rIn[end-1] != cRemove)
break;
--end;
}
sal_Int32 start = 0;
while (start < end)
{
if (rIn[start] != cRemove)
break;
++start;
}
return rIn.copy(start, end - start);
}
}
OString strip(const OString& rIn, char c)
{
auto x = tmpl_stripStartString<OString, char>(rIn, c);
return stripEnd(x, c);
return tmpl_stripString<OString, char>(rIn, c);
}
std::string_view strip(std::string_view rIn, char c)
{
auto x = tmpl_stripStart<std::string_view, char>(rIn, c);
return stripEnd(x, c);
return tmpl_strip<std::string_view, char>(rIn, c);
}
OUString strip(const OUString& rIn, sal_Unicode c)
{
auto x = tmpl_stripStartString<OUString, sal_Unicode>(rIn, c);
return stripEnd(x, c);
return tmpl_stripString<OUString, sal_Unicode>(rIn, c);
}
std::u16string_view strip(std::u16string_view rIn, sal_Unicode c)
{
auto x = tmpl_stripStart<std::u16string_view, sal_Unicode>(rIn, c);
return stripEnd(x, c);
return tmpl_strip<std::u16string_view, sal_Unicode>(rIn, c);
}
namespace