1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
12 #include <sal/config.h>
17 #include <string_view>
19 #include <o3tl/intcmp.hxx>
20 #include <rtl/character.hxx>
21 #include <rtl/ustring.h>
23 #include <sal/types.h>
27 // Like OUString::equalsAscii/OUString::equalsAsciiL, but for std::u16string_view:
28 inline 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())
35 // Like OUString::compareToAscii, but for std::u16string_view and std::string_view:
36 inline int compareToAscii(std::u16string_view s1
, std::string_view s2
)
38 return rtl_ustr_asciil_reverseCompare_WithLength(s1
.data(), s1
.size(), s2
.data(), s2
.size());
41 // Like OUString::equalsIgnoreAsciiCase, but for two std::u16string_view:
42 inline bool equalsIgnoreAsciiCase(std::u16string_view s1
, std::u16string_view s2
)
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())
52 inline bool equalsIgnoreAsciiCase(std::u16string_view s1
, std::string_view s2
)
54 return s1
.size() == s2
.size()
55 && (rtl_ustr_ascii_shortenedCompareIgnoreAsciiCase_WithLength(s1
.data(), s1
.size(),
60 inline bool equalsIgnoreAsciiCase(std::string_view s1
, std::string_view s2
)
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())
70 // Like OUString::compareToIgnoreAsciiCase, but for two std::u16string_view:
71 inline int compareToIgnoreAsciiCase(std::u16string_view s1
, std::u16string_view s2
)
73 return rtl_ustr_compareIgnoreAsciiCase_WithLength(s1
.data(), s1
.size(), s2
.data(), s2
.size());
76 // Like OUString::matchIgnoreAsciiCase, but for two std::u16string_view:
77 inline bool matchIgnoreAsciiCase(std::u16string_view s1
, std::u16string_view s2
,
78 sal_Int32 fromIndex
= 0)
80 return rtl_ustr_shortenedCompareIgnoreAsciiCase_WithLength(
81 s1
.data() + fromIndex
, s1
.size() - fromIndex
, s2
.data(), s2
.size(), s2
.size())
85 // Like OUString::matchIgnoreAsciiCase, but for std::u16string_view and std::string_view:
86 inline bool matchIgnoreAsciiCase(std::u16string_view s1
, std::string_view s2
,
87 sal_Int32 fromIndex
= 0)
89 return rtl_ustr_ascii_shortenedCompareIgnoreAsciiCase_WithLength(
90 s1
.data() + fromIndex
, s1
.size() - fromIndex
, s2
.data(), s2
.size())
94 // Like OUString::endsWithIgnoreAsciiCase, but for std::u16string_view
95 inline bool endsWithIgnoreAsciiCase(std::u16string_view s1
, std::u16string_view s2
,
96 std::u16string_view
* rest
= nullptr)
98 auto const b
= s2
.size() <= s1
.size() && matchIgnoreAsciiCase(s1
, s2
, s1
.size() - s2
.size());
99 if (b
&& rest
!= nullptr)
101 *rest
= s1
.substr(0, s1
.size() - s2
.size());
106 inline bool endsWithIgnoreAsciiCase(std::u16string_view s1
, std::string_view s2
,
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());
120 // Similar to O[U]String::getToken, returning the first token of a std::[u16]string_view starting
121 // at a given position.
123 // Attention: There are two sets of o3tl::getToken overloads here. This first set has an interface
124 // based on std::size_t length parameters, and its semantics don't match those of
125 // O[U]String::getToken exactly (buf if needed, it can be extended to return the n'th token instead
126 // of just the first, and/or support an initial position of npos, to make the semantics match).
127 template <typename charT
, typename traits
= std::char_traits
<charT
>>
128 inline std::basic_string_view
<charT
, traits
> getToken(std::basic_string_view
<charT
, traits
> sv
,
129 charT delimiter
, std::size_t& position
)
131 assert(position
<= sv
.size());
132 auto const n
= sv
.find(delimiter
, position
);
133 std::basic_string_view
<charT
, traits
> t
;
134 if (n
== std::string_view::npos
)
136 t
= sv
.substr(position
);
137 position
= std::string_view::npos
;
141 t
= sv
.substr(position
, n
- position
);
146 // The following two overloads prevent overload resolution mistakes that would occur with their
147 // template counterpart, when sv is of a type that is implicitly convertible to basic_string_view
148 // (like OString or OUString), in which case overload resolution would erroneously choose the
149 // three-argument overloads (taking sv, nToken, cTok) from the second set of
150 // o3tl::getToken overloads below:
151 inline std::string_view
getToken(std::string_view sv
, char delimiter
, std::size_t& position
)
153 return getToken
<char>(sv
, delimiter
, position
);
155 inline std::u16string_view
getToken(std::u16string_view sv
, char16_t delimiter
,
156 std::size_t& position
)
158 return getToken
<char16_t
>(sv
, delimiter
, position
);
161 // Similar to O[U]String::getToken.
163 // Attention: There are two sets of o3tl::getToken overloads here. This second set has an
164 // interface based on sal_Int32 length parameters, and is meant to be a drop-in replacement for
165 // O[U]String::getToken.
166 template <typename charT
, typename traits
= std::char_traits
<charT
>>
167 inline std::basic_string_view
<charT
, traits
> getToken(std::basic_string_view
<charT
, traits
> pStr
,
168 sal_Int32 nToken
, charT cTok
,
171 assert(o3tl::IntCmp(rnIndex
) <= o3tl::IntCmp(pStr
.size()));
173 // Return an empty string and set rnIndex to -1 if either nToken or rnIndex is
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
)
188 if (nTokCount
> nToken
)
190 if (nTokCount
== nToken
)
191 pCharStrStart
= pCharStr
+ 1;
197 if (nTokCount
>= nToken
)
200 rnIndex
= pCharStr
- pOrgCharStr
+ 1;
203 return std::basic_string_view
<charT
, traits
>(pCharStrStart
, pCharStr
- pCharStrStart
);
208 return std::basic_string_view
<charT
, traits
>();
210 // The following two overloads prevent deduction failures that would occur with their template
211 // counterpart, when sv is of a type that is implicitly convertible to basic_string_view (like
212 // OString or OUString):
213 inline std::string_view
getToken(std::string_view sv
, sal_Int32 nToken
, char cTok
,
216 return getToken
<char>(sv
, nToken
, cTok
, rnIndex
);
218 inline std::u16string_view
getToken(std::u16string_view sv
, sal_Int32 nToken
, char16_t cTok
,
221 return getToken
<char16_t
>(sv
, nToken
, cTok
, rnIndex
);
223 inline std::string_view
getToken(std::string_view sv
, sal_Int32 nToken
, char cTok
)
225 sal_Int32 nIndex
= 0;
226 return getToken
<char>(sv
, nToken
, cTok
, nIndex
);
228 inline std::u16string_view
getToken(std::u16string_view sv
, sal_Int32 nToken
, char16_t cTok
)
230 sal_Int32 nIndex
= 0;
231 return getToken
<char16_t
>(sv
, nToken
, cTok
, nIndex
);
234 // Implementations of C++20 std::basic_string_view::starts_with and
235 // std::basic_string_view::ends_with, until we can use those directly on all platforms:
236 template <typename charT
, typename traits
= std::char_traits
<charT
>>
237 constexpr 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
;
246 template <typename charT
, typename traits
= std::char_traits
<charT
>>
247 constexpr 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
);
255 template <typename charT
, typename traits
= std::char_traits
<charT
>>
256 constexpr 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
));
264 template <typename charT
, typename traits
= std::char_traits
<charT
>>
265 constexpr bool ends_with(std::basic_string_view
<charT
, traits
> sv
,
266 std::basic_string_view
<charT
, traits
> x
) noexcept
268 #if defined __cpp_lib_starts_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;
275 template <typename charT
, typename traits
= std::char_traits
<charT
>>
276 constexpr bool ends_with(std::basic_string_view
<charT
, traits
> sv
, charT x
) noexcept
278 #if defined __cpp_lib_starts_ends_with
279 return sv
.ends_with(x
);
281 return !sv
.empty() && traits::eq(sv
.back(), x
);
284 template <typename charT
, typename traits
= std::char_traits
<charT
>>
285 constexpr bool ends_with(std::basic_string_view
<charT
, traits
> sv
, charT
const* x
)
287 #if defined __cpp_lib_starts_ends_with
288 return sv
.ends_with(x
);
290 return ends_with(sv
, std::basic_string_view
<charT
, traits
>(x
));
293 // The following overloads prevent deduction failures that would occur with their template
294 // counterparts, when x is of a type that is implicitly convertible to basic_string_view (like
295 // OString or OUString, and we only bother to provide overloads for the char and char16_t cases, not
296 // also for char32_t and wchar_t, nor for C++20 char8_t):
297 constexpr bool starts_with(std::string_view sv
, std::string_view x
) noexcept
299 return starts_with
<char>(sv
, x
);
301 constexpr bool starts_with(std::u16string_view sv
, std::u16string_view x
) noexcept
303 return starts_with
<char16_t
>(sv
, x
);
305 constexpr bool ends_with(std::string_view sv
, std::string_view x
) noexcept
307 return ends_with
<char>(sv
, x
);
309 constexpr bool ends_with(std::u16string_view sv
, std::u16string_view x
) noexcept
311 return ends_with
<char16_t
>(sv
, x
);
314 // Variants of C++20 std::basic_string_view::starts_with and
315 // std::basic_string_view::ends_with that have a rest out parameter, similar to our OString and
316 // OUString startsWith and endsWith member functions:
317 template <typename charT
, typename traits
= std::char_traits
<charT
>>
318 constexpr 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);
323 auto const found
= starts_with(sv
, x
);
326 *rest
= sv
.substr(x
.length());
330 template <typename charT
, typename traits
= std::char_traits
<charT
>>
331 constexpr 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);
335 auto const found
= starts_with(sv
, x
);
338 *rest
= sv
.substr(1);
342 template <typename charT
, typename traits
= std::char_traits
<charT
>>
343 constexpr 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);
347 auto const found
= starts_with(sv
, x
);
350 *rest
= sv
.substr(traits::length(x
));
354 template <typename charT
, typename traits
= std::char_traits
<charT
>>
355 constexpr 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);
360 auto const found
= ends_with(sv
, x
);
363 *rest
= sv
.substr(0, sv
.length() - x
.length());
367 template <typename charT
, typename traits
= std::char_traits
<charT
>>
368 constexpr 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);
372 auto const found
= ends_with(sv
, x
);
375 *rest
= sv
.substr(0, sv
.length() - 1);
379 template <typename charT
, typename traits
= std::char_traits
<charT
>>
380 constexpr 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);
384 auto const found
= ends_with(sv
, x
);
387 *rest
= sv
.substr(0, sv
.length() - traits::length(x
));
391 // The following overloads prevent deduction failures that would occur with their template
392 // counterparts, when x is of a type that is implicitly convertible to basic_string_view (like
393 // OString or OUString, and we only bother to provide overloads for the char and char16_t cases, not
394 // also for char32_t and wchar_t, nor for C++20 char8_t):
395 constexpr bool starts_with(std::string_view sv
, std::string_view x
, std::string_view
* rest
) noexcept
397 return starts_with
<char>(sv
, x
, rest
);
399 constexpr 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
);
404 constexpr bool ends_with(std::string_view sv
, std::string_view x
, std::string_view
* rest
) noexcept
406 return ends_with
<char>(sv
, x
, rest
);
408 constexpr 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
);
416 inline bool implIsWhitespace(sal_Unicode c
)
418 /* Space or Control character? */
422 /* Only in the General Punctuation area Space or Control characters are included? */
423 if ((c
< 0x2000) || (c
> 0x2029))
426 if ((c
<= 0x200B) || /* U+2000 - U+200B All Spaces */
427 (c
>= 0x2028)) /* U+2028 LINE SEPARATOR, U+2029 PARAGRAPH SEPARATOR */
432 } // namespace internal
434 // Like OUString::trim, but for std::[u16]string_view:
435 template <typename charT
, typename traits
= std::char_traits
<charT
>>
436 std::basic_string_view
<charT
, traits
> trim(std::basic_string_view
<charT
, traits
> str
)
438 auto pFirst
= str
.data();
439 auto pLast
= pFirst
+ str
.size();
441 while ((pFirst
< pLast
) && internal::implIsWhitespace(*pFirst
))
449 while (internal::implIsWhitespace(*pLast
));
451 return std::basic_string_view
<charT
, traits
>(pFirst
, pLast
- pFirst
+ 1);
454 // "deduction guides"
456 inline auto trim(std::string_view str
) { return trim
<>(str
); }
457 inline auto trim(std::u16string_view str
) { return trim
<>(str
); }
459 // Like OString::toInt32, but for std::string_view:
460 inline sal_Int32
toInt32(std::u16string_view str
, sal_Int16 radix
= 10)
462 sal_Int64 n
= rtl_ustr_toInt64_WithLength(str
.data(), radix
, str
.size());
463 if (n
< SAL_MIN_INT32
|| n
> SAL_MAX_INT32
)
467 inline sal_Int32
toInt32(std::string_view str
, sal_Int16 radix
= 10)
469 sal_Int64 n
= rtl_str_toInt64_WithLength(str
.data(), radix
, str
.size());
470 if (n
< SAL_MIN_INT32
|| n
> SAL_MAX_INT32
)
475 // Like OString::toUInt32, but for std::string_view:
476 inline sal_uInt32
toUInt32(std::u16string_view str
, sal_Int16 radix
= 10)
478 sal_Int64 n
= rtl_ustr_toInt64_WithLength(str
.data(), radix
, str
.size());
479 if (n
< 0 || n
> SAL_MAX_UINT32
)
483 inline sal_uInt32
toUInt32(std::string_view str
, sal_Int16 radix
= 10)
485 sal_Int64 n
= rtl_str_toInt64_WithLength(str
.data(), radix
, str
.size());
486 if (n
< 0 || n
> SAL_MAX_UINT32
)
491 // Like OString::toInt64, but for std::string_view:
492 inline sal_Int64
toInt64(std::u16string_view str
, sal_Int16 radix
= 10)
494 return rtl_ustr_toInt64_WithLength(str
.data(), radix
, str
.size());
496 inline sal_Int64
toInt64(std::string_view str
, sal_Int16 radix
= 10)
498 return rtl_str_toInt64_WithLength(str
.data(), radix
, str
.size());
501 // Like OString::toDouble, but for std::string_view:
502 inline double toDouble(std::u16string_view str
)
504 return rtl_math_uStringToDouble(str
.data(), str
.data() + str
.size(), '.', 0, nullptr, nullptr);
506 inline double toDouble(std::string_view str
)
508 return rtl_math_stringToDouble(str
.data(), str
.data() + str
.size(), '.', 0, nullptr, nullptr);
511 // Like OUString::iterateCodePoints, but for std::string_view:
512 inline sal_uInt32
iterateCodePoints(std::u16string_view string
, sal_Int32
* indexUtf16
,
513 sal_Int32 incrementCodePoints
= 1)
518 assert(indexUtf16
!= nullptr);
520 assert(n
<= string
.length());
521 while (incrementCodePoints
< 0)
525 if (rtl::isLowSurrogate(cu
) && n
!= 0 && rtl::isHighSurrogate(string
[n
- 1]))
529 ++incrementCodePoints
;
531 assert(n
< string
.length());
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)
543 assert(n
< string
.length());
545 if (rtl::isHighSurrogate(cu
) && n
!= string
.length() && rtl::isLowSurrogate(string
[n
]))
549 --incrementCodePoints
;
551 assert(n
<= string
.length());
558 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */