summaryrefslogtreecommitdiff
path: root/include/rtl
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2020-09-05 21:45:38 +0200
committerStephan Bergmann <sbergman@redhat.com>2020-09-07 19:19:14 +0200
commitc927aab29ebfff1ce3ac0b2f27ae343025a9890c (patch)
tree4a3c67c6d4f50528dd2363d6b81070e83e02018c /include/rtl
parentfbcdbfef8664430e15b9429187b58dede992accf (diff)
Make the OUString ctors taking raw sal_Unicode pointer/non-const array explicit
...and in turn add OUString::operator = and OUString::operator += overloads that take a std::u16string_view. Without making the ctors explicit, the operator overloads would have caused ambiguities when called with raw sal_Unicode pointers/non-const arrays, as those can convert to both OUString and to std::u16string_view. But the std::u16string_view operator overloads will generally be useful when changing OUStringLiteral similarly to 4b9e440c51be3e40326bc90c33ae69885bfb51e4 "Turn OStringLiteral into a consteval'ed, static-refcound rtl_String", at which point many existing uses of OUStringLiteral will be replaced with uses of std::u16string_view. Implementing this change turned up a need for an operator = overload for OUStringNumber, which has thus been added. No such need turned up for a corresponding operator += overload, but which can easily be added when the need arises. It also revealed that the operator == overloads between an OUString and a raw sal_Unicode pointer/non-const array were implemented rather inefficiently, creating a temporary OUString from the raw argument. Those have been improved. Preceding commits have already taken care of many dubious or simply unnecessary implicit uses of the now-explicit OUString ctors. This commit makes explicit the few remaining reasonable uses. (And in some cases needed to change variable initialization syntax from using parentheses to using curly braces, to avoid the most vexing parse issue. And needed to explicitly add OUString ctors from char16 const[2] string literal lvalues in a conditional expression in writerfilter/source/ooxml/OOXMLFastContextHandler.cxx that are only necessary because MSVC apparently still insists on doing array-to-pointer decay there.) All of this only affects LIBO_INTERNAL_ONLY. Change-Id: I7ce31162e9be1c3ff3c0bd184a34b535ec56be9e Reviewed-on: https://gerrit.libreoffice.org/c/core/+/102098 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'include/rtl')
-rw-r--r--include/rtl/ustring.hxx49
1 files changed, 43 insertions, 6 deletions
diff --git a/include/rtl/ustring.hxx b/include/rtl/ustring.hxx
index cba3c47f5155..035f407c7672 100644
--- a/include/rtl/ustring.hxx
+++ b/include/rtl/ustring.hxx
@@ -213,14 +213,14 @@ public:
#if defined LIBO_INTERNAL_ONLY
- template<typename T> OUString(
+ template<typename T> explicit OUString(
T const & value,
typename libreoffice_internal::CharPtrDetector<T, libreoffice_internal::Dummy>::TypeUtf16
= libreoffice_internal::Dummy()):
pData(nullptr)
{ rtl_uString_newFromStr(&pData, value); }
- template<typename T> OUString(
+ template<typename T> explicit OUString(
T & value,
typename
libreoffice_internal::NonConstCharArrayDetector<T, libreoffice_internal::Dummy>::TypeUtf16
@@ -560,6 +560,22 @@ public:
}
return *this;
}
+
+ template<typename T>
+ OUString & operator =(OUStringNumber<T> && n) {
+ // n.length should never be zero, so no need to add an optimization for that case
+ rtl_uString_newFromStr_WithLength(&pData, n.buf, n.length);
+ return *this;
+ }
+
+ OUString & operator =(std::u16string_view sv) {
+ if (sv.empty()) {
+ rtl_uString_new(&pData);
+ } else {
+ rtl_uString_newFromStr_WithLength(&pData, sv.data(), sv.size());
+ }
+ return *this;
+ }
#endif
#if defined LIBO_INTERNAL_ONLY
@@ -642,6 +658,15 @@ public:
return *this;
}
void operator +=(OUStringLiteral const &) && = delete;
+
+ OUString & operator +=(std::u16string_view sv) & {
+ if (sv.size() > sal_uInt32(std::numeric_limits<sal_Int32>::max())) {
+ throw std::bad_alloc();
+ }
+ rtl_uString_newConcatUtf16L(&pData, pData, sv.data(), sv.size());
+ return *this;
+ }
+ void operator +=(std::u16string_view) && = delete;
#endif
#ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
@@ -1614,18 +1639,30 @@ public:
#if defined LIBO_INTERNAL_ONLY
template<typename T> friend typename libreoffice_internal::CharPtrDetector<T, bool>::TypeUtf16
- operator ==(OUString const & s1, T const & s2) { return s1.compareTo(s2) == 0; }
+ operator ==(OUString const & s1, T const & s2) {
+ return rtl_ustr_compare_WithLength(s1.getStr(), s1.getLength(), s2, rtl_ustr_getLength(s2))
+ == 0;
+ }
template<typename T>
friend typename libreoffice_internal::NonConstCharArrayDetector<T, bool>::TypeUtf16
- operator ==(OUString const & s1, T & s2) { return s1.compareTo(s2) == 0; }
+ operator ==(OUString const & s1, T & s2) {
+ return rtl_ustr_compare_WithLength(s1.getStr(), s1.getLength(), s2, rtl_ustr_getLength(s2))
+ == 0;
+ }
template<typename T> friend typename libreoffice_internal::CharPtrDetector<T, bool>::TypeUtf16
- operator ==(T const & s1, OUString const & s2) { return s2.compareTo(s1) == 0; }
+ operator ==(T const & s1, OUString const & s2) {
+ return rtl_ustr_compare_WithLength(s1, rtl_ustr_getLength(s1), s2.getStr(), s2.getLength())
+ == 0;
+ }
template<typename T>
friend typename libreoffice_internal::NonConstCharArrayDetector<T, bool>::TypeUtf16
- operator ==(T & s1, OUString const & s2) { return s2.compareTo(s1) == 0; }
+ operator ==(T & s1, OUString const & s2) {
+ return rtl_ustr_compare_WithLength(s1, rtl_ustr_getLength(s1), s2.getStr(), s2.getLength())
+ == 0;
+ }
template<typename T> friend typename libreoffice_internal::CharPtrDetector<T, bool>::TypeUtf16
operator !=(OUString const & s1, T const & s2) { return !(s1 == s2); }