14 #ifndef INCLUDED_RTL_STRINGUTILS_HXX 15 #define INCLUDED_RTL_STRINGUTILS_HXX 22 #if defined LIBO_INTERNAL_ONLY 25 #include <type_traits> 35 #ifdef RTL_STRING_UNITTEST 36 #define rtl rtlunittest 42 #ifdef RTL_STRING_UNITTEST 46 #if defined LIBO_INTERNAL_ONLY 56 constexpr OStringChar(
char theC): c(theC) {}
57 template<
typename T> OStringChar(
58 T, std::enable_if_t<std::is_arithmetic_v<T> || std::is_enum_v<T>,
int> = 0) =
delete;
59 constexpr
operator std::string_view()
const {
return {&c, 1}; }
106 constexpr OUStringChar_(
sal_Unicode theC): c(theC) {}
107 constexpr OUStringChar_(
char theC): c(theC) { assert(c <= 0x7F); }
108 template<
typename T> OUStringChar_(
109 T, std::enable_if_t<std::is_arithmetic_v<T> || std::is_enum_v<T>,
int> = 0) =
delete;
110 constexpr
operator std::u16string_view()
const {
return {&c, 1}; }
113 using OUStringChar = OUStringChar_
const;
118 namespace libreoffice_internal
120 #if defined LIBO_INTERNAL_ONLY 121 template <
typename I, std::enable_if_t<
122 std::is_integral_v<I> && std::is_signed_v<I>,
124 constexpr
bool IsValidStrLen(I i, sal_Int32 margin = 0)
127 constexpr sal_uInt32 maxLen = std::numeric_limits<sal_Int32>::max();
128 return i >= 0 &&
static_cast<std::make_unsigned_t<I>
>(i) <= maxLen - margin;
131 template <
typename I, std::enable_if_t<
132 std::is_integral_v<I> && std::is_unsigned_v<I>,
134 constexpr
bool IsValidStrLen(I i, sal_Int32 margin = 0)
137 constexpr sal_uInt32 maxLen = std::numeric_limits<sal_Int32>::max();
138 return i <= maxLen - margin;
141 template <
typename I, std::enable_if_t<std::is_
integral_v<I>,
int> = 0>
142 sal_Int32 ThrowIfInvalidStrLen(I i, sal_Int32 margin = 0)
144 if (!IsValidStrLen(i, margin))
145 throw std::bad_alloc();
179 template<
typename T1,
typename T2 =
void >
182 static const bool ok =
false;
184 template<
typename T >
188 static const bool ok =
true;
190 template<
typename T >
194 static const bool ok =
true;
196 #if defined LIBO_INTERNAL_ONLY 198 template<
typename T>
struct CharPtrDetector<
sal_Unicode const *, T> {
using TypeUtf16 = T; };
199 template<
typename T>
struct CharPtrDetector<
sal_Unicode[], T> {
using TypeUtf16 = T; };
200 template<
typename T>
struct CharPtrDetector<
sal_Unicode const[], T> {
using TypeUtf16 = T; };
203 template<
typename T1,
typename T2 >
207 template<
typename T,
int N >
212 #ifdef RTL_STRING_UNITTEST 214 template<
typename T >
219 template<
typename T >
220 struct NonConstCharArrayDetector< const char[], T >
225 #if defined LIBO_INTERNAL_ONLY 226 template<
typename T, std::
size_t N>
struct NonConstCharArrayDetector<
sal_Unicode[N], T> {
231 template<
typename T1,
typename T2 =
void >
234 static const bool ok =
false;
236 template< std::
size_t N,
typename T >
240 static const std::size_t length = N - 1;
241 static const bool ok =
true;
242 #if defined LIBO_INTERNAL_ONLY 245 static bool isValid(
char const (& literal)[N]) {
246 for (std::size_t i = 0; i != N - 1; ++i) {
247 if (literal[i] ==
'\0') {
251 return literal[N - 1] ==
'\0';
253 #if defined LIBO_INTERNAL_ONLY 256 static char const *
toPointer(
char const (& literal)[N]) {
return literal; }
259 #if defined(__COVERITY__) && __COVERITY_MAJOR__ <= 2023 262 template<
typename T >
263 struct ConstCharArrayDetector< const char[ 1 ], T >
266 static const std::size_t length = 0;
267 static const bool ok =
true;
268 #if defined LIBO_INTERNAL_ONLY 271 static bool isValid(
char const (& literal)[1]) {
272 return literal[0] ==
'\0';
274 #if defined LIBO_INTERNAL_ONLY 277 static char const * toPointer(
char const (& literal)[1]) {
return literal; }
281 #if defined LIBO_INTERNAL_ONLY \ 282 && !(defined _MSC_VER && _MSC_VER >= 1930 && _MSC_VER <= 1939 && defined _MANAGED) 283 template<std::
size_t N,
typename T>
284 struct ConstCharArrayDetector<char8_t const [N], T> {
286 static constexpr
bool const ok =
true;
287 static constexpr std::size_t
const length = N - 1;
288 static constexpr
bool isValid(char8_t
const (& literal)[N]) {
289 for (std::size_t i = 0; i != N - 1; ++i) {
290 if (literal[i] == u8
'\0') {
294 return literal[N - 1] == u8
'\0';
296 static constexpr
char const * toPointer(char8_t
const (& literal)[N])
297 {
return reinterpret_cast<char const *
>(literal); }
301 #if defined LIBO_INTERNAL_ONLY 302 template<std::
size_t N,
typename T>
303 struct ConstCharArrayDetector<
sal_Unicode const [N], T> {
305 static constexpr
bool const ok =
true;
306 static constexpr std::size_t
const length = N - 1;
307 static constexpr
bool isValid(
sal_Unicode const (& literal)[N]) {
308 for (std::size_t i = 0; i != N - 1; ++i) {
309 if (literal[i] ==
'\0') {
313 return literal[N - 1] ==
'\0';
320 #if defined(__COVERITY__) && __COVERITY_MAJOR__ <= 2023 324 struct ConstCharArrayDetector<
sal_Unicode const [1], T> {
326 static constexpr
bool const ok =
true;
327 static constexpr std::size_t
const length = 0;
328 static constexpr
bool isValid(
sal_Unicode const (& literal)[1]) {
329 return literal[0] ==
'\0';
337 template<
typename T>
struct ConstCharArrayDetector<
342 static constexpr
bool const ok =
true;
343 static constexpr std::size_t
const length = 1;
344 static constexpr
bool isValid(OUStringChar) {
return true; }
346 OUStringChar_
const & literal)
347 {
return &literal.c; }
351 #if defined LIBO_INTERNAL_ONLY && defined RTL_STRING_UNITTEST 354 template<
typename T >
355 struct ExceptConstCharArrayDetector
360 struct ExceptConstCharArrayDetector< const char[ N ] >
363 template<std::
size_t N>
364 struct ExceptConstCharArrayDetector<
sal_Unicode const[N]> {};
365 template<>
struct ExceptConstCharArrayDetector<
374 template<
typename T >
375 struct ExceptCharArrayDetector
380 struct ExceptCharArrayDetector< char[ N ] >
384 struct ExceptCharArrayDetector< const char[ N ] >
387 template<std::
size_t N>
struct ExceptCharArrayDetector<
sal_Unicode[N]> {};
388 template<std::
size_t N>
struct ExceptCharArrayDetector<
sal_Unicode const[N]> {};
389 template<>
struct ExceptCharArrayDetector<OUStringChar_> {};
393 template<
typename T1,
typename T2 =
void >
396 static const bool ok =
false;
398 template<
typename T >
402 static const bool ok =
true;
404 template<
typename T >
408 static const bool ok =
true;
412 template<
typename T,
bool >
417 template<
typename T >
428 #endif // INCLUDED_RTL_STRINGUTILS_HXX T Type
Definition: stringutils.hxx:239
T Type
Definition: stringutils.hxx:401
Definition: stringutils.hxx:178
#define SAL_WARN_UNUSED
Annotate classes where a compiler should warn if an instance is unused.
Definition: types.h:611
Definition: stringutils.hxx:232
T Type
Definition: stringutils.hxx:193
static const bool ok
Definition: stringutils.hxx:396
T Type
Definition: stringutils.hxx:210
Definition: stringutils.hxx:413
sal_uInt16 sal_Unicode
Definition: types.h:123
Definition: stringutils.hxx:394
static const bool ok
Definition: stringutils.hxx:234
Definition: bootstrap.hxx:33
T Type
Definition: stringutils.hxx:420
T Type
Definition: stringutils.hxx:407
static bool isValid(char const (&literal)[N])
Definition: stringutils.hxx:245
Definition: stringutils.hxx:204
Definition: stringutils.hxx:180
static const bool ok
Definition: stringutils.hxx:182
T Type
Definition: stringutils.hxx:187
static char const * toPointer(char const (&literal)[N])
Definition: stringutils.hxx:256