20#include <rtl/character.hxx>
21#include <rtl/ustring.h>
28inline bool equalsAscii(std::u16string_view s1, std::string_view s2)
30 return s1.size() == s2.size()
31 && rtl_ustr_ascii_shortenedCompare_WithLength(s1.data(), s1.size(), s2.data(), s2.size())
38 return rtl_ustr_asciil_reverseCompare_WithLength(s1.data(), s1.size(), s2.data(), s2.size());
44 if (s1.size() != s2.size())
46 if (s1.data() == s2.data())
48 return rtl_ustr_compareIgnoreAsciiCase_WithLength(s1.data(), s1.size(), s2.data(), s2.size())
54 return s1.size() == s2.size()
55 && (rtl_ustr_ascii_shortenedCompareIgnoreAsciiCase_WithLength(s1.data(), s1.size(),
62 if (s1.size() != s2.size())
64 if (s1.data() == s2.data())
66 return rtl_str_compareIgnoreAsciiCase_WithLength(s1.data(), s1.size(), s2.data(), s2.size())
73 return rtl_ustr_compareIgnoreAsciiCase_WithLength(s1.data(), s1.size(), s2.data(), s2.size());
78 sal_Int32 fromIndex = 0)
80 return rtl_ustr_shortenedCompareIgnoreAsciiCase_WithLength(
81 s1.data() + fromIndex, s1.size() - fromIndex, s2.data(), s2.size(), s2.size())
87 sal_Int32 fromIndex = 0)
89 return rtl_ustr_ascii_shortenedCompareIgnoreAsciiCase_WithLength(
90 s1.data() + fromIndex, s1.size() - fromIndex, s2.data(), s2.size())
96 std::u16string_view* rest =
nullptr)
99 if (b && rest !=
nullptr)
101 *rest = s1.substr(0, s1.size() - s2.size());
107 std::u16string_view* rest =
nullptr)
109 auto const b = s2.size() <= s1.size()
110 && rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths(
111 s1.data() + s1.size() - s2.size(), s2.size(), s2.data(), s2.size())
113 if (b && rest !=
nullptr)
115 *rest = s1.substr(0, s1.size() - s2.size());
127template <
typename charT,
typename traits = std::
char_traits<
charT>>
128inline std::basic_string_view<charT, traits>
getToken(std::basic_string_view<charT, traits> sv,
129 charT delimiter, std::size_t& position)
132 auto const n = sv.find(delimiter,
position);
133 std::basic_string_view<charT, traits>
t;
134 if (
n == std::string_view::npos)
151inline std::string_view
getToken(std::string_view sv,
char delimiter, std::size_t& position)
153 return getToken<char>(sv, delimiter,
position);
155inline std::u16string_view
getToken(std::u16string_view sv,
char16_t delimiter,
156 std::size_t& position)
158 return getToken<char16_t>(sv, delimiter,
position);
166template <
typename charT,
typename traits = std::
char_traits<
charT>>
167inline std::basic_string_view<charT, traits>
getToken(std::basic_string_view<charT, traits> pStr,
168 sal_Int32 nToken, charT cTok,
175 if (rnIndex >= 0 &&
nToken >= 0)
177 const charT* pOrgCharStr = pStr.data();
178 const charT* pCharStr = pOrgCharStr + rnIndex;
179 sal_Int32 nLen = pStr.size() - rnIndex;
180 sal_Int32 nTokCount = 0;
181 const charT* pCharStrStart = pCharStr;
184 if (*pCharStr == cTok)
191 pCharStrStart = pCharStr + 1;
200 rnIndex = pCharStr - pOrgCharStr + 1;
203 return std::basic_string_view<charT, traits>(pCharStrStart, pCharStr - pCharStrStart);
208 return std::basic_string_view<charT, traits>();
213inline std::string_view
getToken(std::string_view sv, sal_Int32 nToken,
char cTok,
216 return getToken<char>(sv,
nToken, cTok, rnIndex);
218inline std::u16string_view
getToken(std::u16string_view sv, sal_Int32 nToken,
char16_t cTok,
221 return getToken<char16_t>(sv,
nToken, cTok, rnIndex);
223inline std::string_view
getToken(std::string_view sv, sal_Int32 nToken,
char cTok)
228inline std::u16string_view
getToken(std::u16string_view sv, sal_Int32 nToken,
char16_t cTok)
236template <
typename charT,
typename traits = std::
char_traits<
charT>>
237constexpr bool starts_with(std::basic_string_view<charT, traits> sv,
238 std::basic_string_view<charT, traits> x)
noexcept
240#if defined __cpp_lib_starts_ends_with
241 return sv.starts_with(
x);
243 return sv.substr(0,
x.size()) ==
x;
246template <
typename charT,
typename traits = std::
char_traits<
charT>>
247constexpr bool starts_with(std::basic_string_view<charT, traits> sv, charT x)
noexcept
249#if defined __cpp_lib_starts_ends_with
250 return sv.starts_with(
x);
252 return !sv.empty() && traits::eq(sv.front(),
x);
255template <
typename charT,
typename traits = std::
char_traits<
charT>>
256constexpr bool starts_with(std::basic_string_view<charT, traits> sv, charT
const* x)
258#if defined __cpp_lib_starts_ends_with
259 return sv.starts_with(
x);
261 return starts_with(sv, std::basic_string_view<charT, traits>(
x));
264template <
typename charT,
typename traits = std::
char_traits<
charT>>
265constexpr bool ends_with(std::basic_string_view<charT, traits> sv,
266 std::basic_string_view<charT, traits> x)
noexcept
268#if defined __cpp_lib_ends_ends_with
269 return sv.ends_with(
x);
271 return sv.size() >=
x.size()
272 && sv.compare(sv.size() -
x.size(), std::basic_string_view<charT, traits>::npos,
x) == 0;
275template <
typename charT,
typename traits = std::
char_traits<
charT>>
276constexpr bool ends_with(std::basic_string_view<charT, traits> sv, charT x)
noexcept
278#if defined __cpp_lib_ends_ends_with
279 return sv.ends_with(
x);
281 return !sv.empty() && traits::eq(sv.back(),
x);
284template <
typename charT,
typename traits = std::
char_traits<
charT>>
285constexpr bool ends_with(std::basic_string_view<charT, traits> sv, charT
const* x)
287#if defined __cpp_lib_ends_ends_with
288 return sv.ends_with(
x);
290 return ends_with(sv, std::basic_string_view<charT, traits>(
x));
297constexpr bool starts_with(std::string_view sv, std::string_view x)
noexcept
299 return starts_with<char>(sv,
x);
301constexpr bool starts_with(std::u16string_view sv, std::u16string_view x)
noexcept
303 return starts_with<char16_t>(sv,
x);
305constexpr bool ends_with(std::string_view sv, std::string_view x)
noexcept
307 return ends_with<char>(sv,
x);
309constexpr bool ends_with(std::u16string_view sv, std::u16string_view x)
noexcept
311 return ends_with<char16_t>(sv,
x);
317template <
typename charT,
typename traits = std::
char_traits<
charT>>
318constexpr bool starts_with(std::basic_string_view<charT, traits> sv,
319 std::basic_string_view<charT, traits> x,
320 std::basic_string_view<charT, traits>* rest)
noexcept
322 assert(rest !=
nullptr);
326 *rest = sv.substr(
x.length());
330template <
typename charT,
typename traits = std::
char_traits<
charT>>
331constexpr bool starts_with(std::basic_string_view<charT, traits> sv, charT x,
332 std::basic_string_view<charT, traits>* rest)
noexcept
334 assert(rest !=
nullptr);
338 *rest = sv.substr(1);
342template <
typename charT,
typename traits = std::
char_traits<
charT>>
343constexpr bool starts_with(std::basic_string_view<charT, traits> sv, charT
const* x,
344 std::basic_string_view<charT, traits>* rest)
346 assert(rest !=
nullptr);
350 *rest = sv.substr(traits::length(
x));
354template <
typename charT,
typename traits = std::
char_traits<
charT>>
355constexpr bool ends_with(std::basic_string_view<charT, traits> sv,
356 std::basic_string_view<charT, traits> x,
357 std::basic_string_view<charT, traits>* rest)
noexcept
359 assert(rest !=
nullptr);
363 *rest = sv.substr(0, sv.length() -
x.length());
367template <
typename charT,
typename traits = std::
char_traits<
charT>>
368constexpr bool ends_with(std::basic_string_view<charT, traits> sv, charT x,
369 std::basic_string_view<charT, traits>* rest)
noexcept
371 assert(rest !=
nullptr);
375 *rest = sv.substr(0, sv.length() - 1);
379template <
typename charT,
typename traits = std::
char_traits<
charT>>
380constexpr bool ends_with(std::basic_string_view<charT, traits> sv, charT
const* x,
381 std::basic_string_view<charT, traits>* rest)
383 assert(rest !=
nullptr);
387 *rest = sv.substr(0, sv.length() - traits::length(
x));
395constexpr bool starts_with(std::string_view sv, std::string_view x, std::string_view* rest)
noexcept
397 return starts_with<char>(sv,
x, rest);
399constexpr bool starts_with(std::u16string_view sv, std::u16string_view x,
400 std::u16string_view* rest)
noexcept
402 return starts_with<char16_t>(sv,
x, rest);
404constexpr bool ends_with(std::string_view sv, std::string_view x, std::string_view* rest)
noexcept
406 return ends_with<char>(sv,
x, rest);
408constexpr bool ends_with(std::u16string_view sv, std::u16string_view x,
409 std::u16string_view* rest)
noexcept
411 return ends_with<char16_t>(sv,
x, rest);
423 if ((c < 0x2000) || (c > 0x2029))
435template <
typename charT,
typename traits = std::
char_traits<
charT>>
436std::basic_string_view<charT, traits>
trim(std::basic_string_view<charT, traits> str)
438 auto pFirst = str.data();
439 auto pLast = pFirst + str.size();
451 return std::basic_string_view<charT, traits>(pFirst, pLast - pFirst + 1);
456inline auto trim(std::string_view str) {
return trim<>(str); }
457inline auto trim(std::u16string_view str) {
return trim<>(str); }
460inline sal_Int32
toInt32(std::u16string_view str, sal_Int16 radix = 10)
462 sal_Int64
n = rtl_ustr_toInt64_WithLength(str.data(), radix, str.size());
467inline sal_Int32
toInt32(std::string_view str, sal_Int16 radix = 10)
469 sal_Int64
n = rtl_str_toInt64_WithLength(str.data(), radix, str.size());
476inline sal_uInt32
toUInt32(std::u16string_view str, sal_Int16 radix = 10)
478 sal_Int64
n = rtl_ustr_toInt64_WithLength(str.data(), radix, str.size());
483inline sal_uInt32
toUInt32(std::string_view str, sal_Int16 radix = 10)
485 sal_Int64
n = rtl_str_toInt64_WithLength(str.data(), radix, str.size());
492inline sal_Int64
toInt64(std::u16string_view str, sal_Int16 radix = 10)
494 return rtl_ustr_toInt64_WithLength(str.data(), radix, str.size());
496inline sal_Int64
toInt64(std::string_view str, sal_Int16 radix = 10)
498 return rtl_str_toInt64_WithLength(str.data(), radix, str.size());
504 return rtl_math_uStringToDouble(str.data(), str.data() + str.size(),
'.', 0,
nullptr,
nullptr);
508 return rtl_math_stringToDouble(str.data(), str.data() + str.size(),
'.', 0,
nullptr,
nullptr);
513 sal_Int32 incrementCodePoints = 1)
518 assert(indexUtf16 !=
nullptr);
521 while (incrementCodePoints < 0)
525 if (rtl::isLowSurrogate(cu) &&
n != 0 && rtl::isHighSurrogate(
string[
n - 1]))
529 ++incrementCodePoints;
533 if (rtl::isHighSurrogate(cu) &&
string.
length() -
n >= 2 && rtl::isLowSurrogate(
string[
n + 1]))
535 cp = rtl::combineSurrogates(cu,
string[
n + 1]);
541 while (incrementCodePoints > 0)
545 if (rtl::isHighSurrogate(cu) &&
n !=
string.
length() && rtl::isLowSurrogate(
string[
n]))
549 --incrementCodePoints;
bool implIsWhitespace(sal_Unicode c)
std::basic_string_view< charT, traits > trim(std::basic_string_view< charT, traits > str)
bool equalsIgnoreAsciiCase(std::u16string_view s1, std::u16string_view s2)
bool endsWithIgnoreAsciiCase(std::u16string_view s1, std::u16string_view s2, std::u16string_view *rest=nullptr)
constexpr bool ends_with(std::basic_string_view< charT, traits > sv, std::basic_string_view< charT, traits > x) noexcept
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
sal_Int64 toInt64(std::u16string_view str, sal_Int16 radix=10)
constexpr bool starts_with(std::basic_string_view< charT, traits > sv, std::basic_string_view< charT, traits > x) noexcept
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
double toDouble(std::u16string_view str)
sal_uInt32 toUInt32(std::u16string_view str, sal_Int16 radix=10)
bool matchIgnoreAsciiCase(std::u16string_view s1, std::u16string_view s2, sal_Int32 fromIndex=0)
int compareToIgnoreAsciiCase(std::u16string_view s1, std::u16string_view s2)
sal_uInt32 iterateCodePoints(std::u16string_view string, sal_Int32 *indexUtf16, sal_Int32 incrementCodePoints=1)
int compareToAscii(std::u16string_view s1, std::string_view s2)
bool equalsAscii(std::u16string_view s1, std::string_view s2)