/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
namespace o3tl
{ // Like OUString::equalsAscii/OUString::equalsAsciiL, but for std::u16string_view: inlinebool equalsAscii(std::u16string_view s1, std::string_view s2)
{ return s1.size() == s2.size()
&& rtl_ustr_ascii_shortenedCompare_WithLength(s1.data(), s1.size(), s2.data(), s2.size())
== 0;
}
// Like OUString::compareToAscii, but for std::u16string_view and std::string_view: inlineint compareToAscii(std::u16string_view s1, std::string_view s2)
{ return rtl_ustr_asciil_reverseCompare_WithLength(s1.data(), s1.size(), s2.data(), s2.size());
};
// Like OUString::equalsIgnoreAsciiCase, but for two std::u16string_view: inlinebool equalsIgnoreAsciiCase(std::u16string_view s1, std::u16string_view s2)
{ if (s1.size() != s2.size()) returnfalse; if (s1.data() == s2.data()) returntrue; return rtl_ustr_compareIgnoreAsciiCase_WithLength(s1.data(), s1.size(), s2.data(), s2.size())
== 0;
};
// Like OUString::compareToIgnoreAsciiCase, but for two std::u16string_view: inlineint compareToIgnoreAsciiCase(std::u16string_view s1, std::u16string_view s2)
{ return rtl_ustr_compareIgnoreAsciiCase_WithLength(s1.data(), s1.size(), s2.data(), s2.size());
};
// Like OUString::matchIgnoreAsciiCase, but for two std::u16string_view: inlinebool matchIgnoreAsciiCase(std::u16string_view s1, std::u16string_view s2,
sal_Int32 fromIndex = 0)
{ return rtl_ustr_shortenedCompareIgnoreAsciiCase_WithLength(
s1.data() + fromIndex, s1.size() - fromIndex, s2.data(), s2.size(), s2.size())
== 0;
}
// Like OUString::matchIgnoreAsciiCase, but for std::u16string_view and std::string_view: inlinebool matchIgnoreAsciiCase(std::u16string_view s1, std::string_view s2,
sal_Int32 fromIndex = 0)
{ return rtl_ustr_ascii_shortenedCompareIgnoreAsciiCase_WithLength(
s1.data() + fromIndex, s1.size() - fromIndex, s2.data(), s2.size())
== 0;
}
// Like OUString::endsWithIgnoreAsciiCase, but for std::u16string_view inlinebool endsWithIgnoreAsciiCase(std::u16string_view s1, std::u16string_view s2,
std::u16string_view* rest = nullptr)
{ autoconst b = s2.size() <= s1.size() && matchIgnoreAsciiCase(s1, s2, s1.size() - s2.size()); if (b && rest != nullptr)
{
*rest = s1.substr(0, s1.size() - s2.size());
} return b;
}
// Similar to O[U]String::getToken, returning the first token of a std::[u16]string_view starting // at a given position. // // Attention: There are two sets of o3tl::getToken overloads here. This first set has an interface // based on std::size_t length parameters, and its semantics don't match those of // O[U]String::getToken exactly (buf if needed, it can be extended to return the n'th token instead // of just the first, and/or support an initial position of npos, to make the semantics match). template <typename charT, typename traits = std::char_traits<charT>> inline std::basic_string_view<charT, traits> getToken(std::basic_string_view<charT, traits> sv,
charT delimiter, std::size_t& position)
{
assert(position <= sv.size()); autoconst n = sv.find(delimiter, position);
std::basic_string_view<charT, traits> t; if (n == std::string_view::npos)
{
t = sv.substr(position);
position = std::string_view::npos;
} else
{
t = sv.substr(position, n - position);
position = n + 1;
} return t;
} // The following two overloads prevent overload resolution mistakes that would occur with their // template counterpart, when sv is of a type that is implicitly convertible to basic_string_view // (like OString or OUString), in which case overload resolution would erroneously choose the // three-argument overloads (taking sv, nToken, cTok) from the second set of // o3tl::getToken overloads below: inline std::string_view getToken(std::string_view sv, char delimiter, std::size_t& position)
{ return getToken<char>(sv, delimiter, position);
} inline std::u16string_view getToken(std::u16string_view sv, char16_t delimiter,
std::size_t& position)
{ return getToken<char16_t>(sv, delimiter, position);
}
// Similar to O[U]String::getToken. // // Attention: There are two sets of o3tl::getToken overloads here. This second set has an // interface based on sal_Int32 length parameters, and is meant to be a drop-in replacement for // O[U]String::getToken. template <typename charT, typename traits = std::char_traits<charT>> inline std::basic_string_view<charT, traits> getToken(std::basic_string_view<charT, traits> pStr,
sal_Int32 nToken, charT cTok,
sal_Int32& rnIndex)
{
assert(o3tl::IntCmp(rnIndex) <= o3tl::IntCmp(pStr.size()));
// Return an empty string and set rnIndex to -1 if either nToken or rnIndex is // negative: if (rnIndex >= 0 && nToken >= 0)
{ const charT* pOrgCharStr = pStr.data(); const charT* pCharStr = pOrgCharStr + rnIndex;
sal_Int32 nLen = pStr.size() - rnIndex;
sal_Int32 nTokCount = 0; const charT* pCharStrStart = pCharStr; while (nLen > 0)
{ if (*pCharStr == cTok)
{
nTokCount++;
if (nTokCount > nToken) break; if (nTokCount == nToken)
pCharStrStart = pCharStr + 1;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.