diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2021-05-12 07:31:20 +0200 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2021-05-12 09:23:36 +0200 |
commit | ad48b2b02f83eed41fb1eb8d16de7e804156fcf1 (patch) | |
tree | 8238213135d3efe1f8b2cb6fdb9c5a79939f106c /include/rtl | |
parent | de16265f55ff2e4e1beb574fcb5b7b894df234f9 (diff) |
Optimized OString operator += overloads
...similar to how OUString got an operator += overload for std::u16string_view
with c927aab29ebfff1ce3ac0b2f27ae343025a9890c "Make the OUString ctors taking
raw sal_Unicode pointer/non-const array explicit". In this case, though, we can
easily add enough overloads (which forward to the one taking std::string_view)
to avoid any ambiguities at call sites.
We had left this out in the past as there is no suitable rtl_string_newConcat*
function to use in the implementation, but we already had inline implementations
of OStringConcat/OStringNumber overloads, so stick to that pattern here with the
new std::string_view overload.
Change-Id: I329036ea2b62cb921b4dbc1eef7c45a9cdb56e13
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/115452
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'include/rtl')
-rw-r--r-- | include/rtl/string.hxx | 54 |
1 files changed, 45 insertions, 9 deletions
diff --git a/include/rtl/string.hxx b/include/rtl/string.hxx index a6812cc7c7df..f80dd7410da7 100644 --- a/include/rtl/string.hxx +++ b/include/rtl/string.hxx @@ -480,6 +480,50 @@ public: void operator+=(OString const &) && = delete; #endif +#if defined LIBO_INTERNAL_ONLY + template<typename T> typename libreoffice_internal::CharPtrDetector<T, OString &>::Type + operator +=(T const & value) & { return operator +=(std::string_view(value)); } + template<typename T> typename libreoffice_internal::CharPtrDetector<T, OString &>::Type + operator +=(T const &) && = delete; + + template<typename T> + typename libreoffice_internal::NonConstCharArrayDetector<T, OString &>::Type + operator +=(T & value) & { return operator +=(std::string_view(value)); } + template<typename T> + typename libreoffice_internal::NonConstCharArrayDetector<T, OString &>::Type operator +=(T &) && + = delete; + + template<typename T> typename libreoffice_internal::ConstCharArrayDetector<T, OString &>::Type + operator +=(T & literal) & { + assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); + return operator +=( + std::string_view( + libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal), + libreoffice_internal::ConstCharArrayDetector<T>::length)); + } + template<typename T> typename libreoffice_internal::ConstCharArrayDetector<T, OString &>::Type + operator +=(T &) && = delete; + + template<std::size_t N> OString & operator +=(OStringLiteral<N> const & literal) & + { return operator +=(std::string_view(literal.getStr(), literal.getLength())); } + template<std::size_t N> void operator +=(OStringLiteral<N> const &) && = delete; + + OString & operator +=(std::string_view sv) & { + if (sv.empty()) { + return *this; + } + if (sv.size() > sal_uInt32(std::numeric_limits<sal_Int32>::max() - pData->length)) { + throw std::bad_alloc(); + } + auto const l = pData->length + sv.size(); + rtl_string_ensureCapacity(&pData, l); + *addDataHelper(pData->buffer + pData->length, sv.data(), sv.size()) = '\0'; + pData->length = l; + return *this; + } + void operator +=(std::string_view) && = delete; +#endif + #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING" /** @overload @@ -506,15 +550,7 @@ public: */ template< typename T > OString& operator+=( OStringNumber< T >&& n ) & { - sal_Int32 l = n.length; - if( l == 0 ) - return *this; - l += pData->length; - rtl_string_ensureCapacity( &pData, l ); - char* end = addDataHelper( pData->buffer + pData->length, n.buf, n.length ); - *end = '\0'; - pData->length = l; - return *this; + return operator +=(std::string_view(n.buf, n.length)); } template<typename T> void operator +=( OStringNumber<T> &&) && = delete; |