/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * 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/.
*/
/* Implementation of efficient string concatenation.
The whole system is built around two basic template classes: - ToStringHelper< T > - for each T it can give the length of the resulting string representation and can write this string representation to a buffer - O(U)StringConcat< T1, T2 > - operator+ now, instead of creating O(U)String object, returns only this helper object, that keeps a reference to both operator+ operands; only when converted to O(U)String it will actually create the resulting string object using ToStringHelper, creating directly the resulting object without any string intermediate objects As all the code is inline methods, it allows for extensive optimization and will usually result in very effective code (even surpassing strlen/strcat and equalling handwritten), while allowing for very easy and intuitive syntax.
*/
/** @internal
Helper class for converting a given type to a string representation.
*/ template< typename T > struct ToStringHelper
{ /// Return length of the string representation of the given object. // static std::size_t length( const T& ); /// Add 8-bit representation of the given object to the given buffer and return position right after the added data. // char* operator()( char* buffer, const T& ) const SAL_RETURNS_NONNULL; /// Add Unicode representation of the given object to the given buffer and return position right after the added data. // sal_Unicode* operator()( sal_Unicode* buffer, const T& ) const SAL_RETURNS_NONNULL;
};
/// If true, T can be used in concatenation resulting in O(U)String. template<typename C, typename T, class Enable = void> constexpr bool allowStringConcat = false; template<typename C, typename T> constexpr bool allowStringConcat<C, T, std::enable_if_t<std::is_invocable_v<ToStringHelper<T>, C*, T>>> = true;
// Output a character specified number of times. If the number is less than 1, do nothing. template <typename C> struct RepeatedChar_t
{
C c;
sal_Int32 n;
constexpr RepeatedChar_t(C ch, sal_Int32 num) : c(ch), n(std::max(num, sal_Int32(0))) {}
};
Objects returned by operator+, instead of O(U)String. These objects (possibly recursively) keep a representation of the whole concatenation operation.
If you get a build error related to this class, you most probably need to explicitly convert the result of a string concatenation to O(U)String.
*/ template <typename C, typename T1, typename T2, std::enable_if_t<allowStringConcat<C, T1> && allowStringConcat<C, T2>, int> = 0 > struct StringConcat
{ public:
StringConcat( const T1& left_, const T2& right_ ) : left( left_ ), right( right_ ) {}
std::size_t length() const { return ToStringHelper< T1 >::length( left ) + ToStringHelper< T2 >::length( right ); }
C* addData( C* buffer ) const SAL_RETURNS_NONNULL { return ToStringHelper< T2 >()( ToStringHelper< T1 >()( buffer, left ), right ); } // NOTE here could be functions that would forward to the "real" temporary O(U)String. Note however that e.g. getStr() // is not so simple, as the O(U)String temporary must live long enough (i.e. can't be created here in a function, a wrapper // temporary object containing it must be returned instead). private: const T1& left; const T2& right;
};
// char[N] and const char[N] need to be done explicitly, otherwise the compiler likes to treat them the same way for some reason template< typename T, std::size_t N >
[[nodiscard]] inline
OStringConcat< T, constchar[ N ] > operator+( const T& left, constchar (&right)[ N ] )
{ return OStringConcat< T, constchar[ N ] >( left, right );
}
template< typename T, std::size_t N >
[[nodiscard]] inline
OStringConcat< constchar[ N ], T > operator+( constchar (&left)[ N ], const T& right )
{ return OStringConcat< constchar[ N ], T >( left, right );
}
template< typename T, std::size_t N >
[[nodiscard]] inline
OStringConcat< T, char[ N ] > operator+( const T& left, char (&right)[ N ] )
{ return OStringConcat< T, char[ N ] >( left, right );
}
template< typename T, std::size_t N >
[[nodiscard]] inline
OStringConcat< char[ N ], T > operator+( char (&left)[ N ], const T& right )
{ return OStringConcat< char[ N ], T >( left, right );
}
#ifdef RTL_STRING_UNITTEST_CONCAT // Special overload to catch the remaining invalid combinations. The helper struct must // be used to make this operator+ overload a worse choice than all the existing overloads above. struct StringConcatInvalid
{ template< typename T >
StringConcatInvalid( const T& ) {}
}; template< typename T > inline intoperator+( const StringConcatInvalid&, const T& )
{
rtl_string_unittest_invalid_concat = true; return 0; // doesn't matter
} #endif
// Lightweight alternative to O(U)String when a (temporary) object is needed to hold // an O(U)StringConcat result that can then be used as a std::(u16)string_view: template <typename C> class StringConcatenation { public: template <class Concat> explicit StringConcatenation(Concat const& c):
length_(c.length()),
buffer_(new C[length_])
{ autoconst end = c.addData(buffer_.get());
assert(end == buffer_.get() + length_); (void)end;
}
Objects returned by O(U)String::number(), instead of O(U)String. These objects keep a representation of the number() operation.
If you get a build error related to this class, you most probably need to explicitly convert the result of calling O(U)String::number() to O(U)String.
*/
template <typename C, std::size_t nBufSize> struct StringNumber
{ template <typename Func, typename... Args,
std::enable_if_t<std::is_invocable_r_v<sal_Int32, Func, C*, Args...>, int> = 0>
StringNumber(Func func, Args... args) { length = func(buf, args...); } // O(U)String::number(value).getStr() is very common (writing xml code, ...), // so implement that one also here, to avoid having to explicitly convert // to O(U)String in all such places const C* getStr() const SAL_RETURNS_NONNULL { return buf; }
StringNumber&& toAsciiUpperCase() &&
{ if constexpr (sizeof(C) == sizeof(char))
rtl_str_toAsciiUpperCase_WithLength(buf, length); else
rtl_ustr_toAsciiUpperCase_WithLength(buf, length); return std::move(*this);
} operator std::basic_string_view<C>() const { return std::basic_string_view<C>(buf, length); }
C buf[nBufSize];
sal_Int32 length;
};
template<std::size_t nBufSize> using OStringNumber = StringNumber<char, nBufSize>; template<std::size_t nBufSize> using OUStringNumber = StringNumber<sal_Unicode, nBufSize>;
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.