summaryrefslogtreecommitdiff
path: root/include/rtl
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2020-09-08 09:48:17 +0200
committerStephan Bergmann <sbergman@redhat.com>2020-09-16 23:02:09 +0200
commite6dfaf9f44f9939abc338c83b3024108431d0f69 (patch)
tree2e220510dc589fa4ce9b205696b9d0b58d38b60a /include/rtl
parenteb55bc5eae82873492cfd7577fb553b1c5abff6b (diff)
Turn OUStringLiteral into a consteval'ed, static-refcound rtl_uString
...from which an OUString can cheaply be instantiated. This is the OUString equivalent of 4b9e440c51be3e40326bc90c33ae69885bfb51e4 "Turn OStringLiteral into a consteval'ed, static-refcound rtl_String". Most remarks about that commit apply here too (this commit is just substantially bigger and a bit more complicated because there were so much more uses of OUStringLiteral than of OStringLiteral): The one downside is that OUStringLiteral now needs to be a template abstracting over the string length. But any uses for which that is a problem (e.g., as the element type of a container that would no longer be homogeneous, or in the signature of a function that shall not be turned into a template for one reason or another) can be replaced with std::u16string_view, without loss of efficiency compared to the original OUStringLiteral, and without loss of expressivity. The new OUStringLiteral ctor code would probably not be very efficient if it were ever executed at runtime, but it is intended to be only executed at compile time. Where available, C++20 "consteval" is used to statically ensure that. The intended use of the new OUStringLiteral is in all cases where an object that shall itself not be an OUString (e.g., because it shall be a global static variable for which the OUString ctor/dtor would be detrimental at library load/unload) must be converted to an OUString instance in at least one place. Other string literal abstractions could use std::u16string_view (or just plain char16_t const[N]), but interestingly OUStringLiteral might be more efficient than constexpr std::u16string_view even for such cases, as it should not need any relocations at library load time. For now, no existing uses of OUStringLiteral have been changed to some other abstraction (unless technically necessary as discussed above), and no additional places that would benefit from OUStringLiteral have been changed to use it. Global constexpr OUStringLiteral variables defined in an included file would be somewhat suboptimal, as each translation unit that uses them would create its own, unshared instance. The envisioned solution is to turn them into static data members of some class (and there may be a loplugin coming to find and fix affected places). Another approach that has been taken here in a few cases where such variables were only used in one .cxx anyway is to move their definitions from the .hxx into that one .cxx (in turn causing some files to become empty and get removed completely)---which also silenced some GCC -Werror=unused-variable if a variable from a .hxx was not used in some .cxx including it. To keep individual commits reasonably manageable, some consumers of OUStringLiteral in rtl/ustrbuf.hxx and rtl/ustring.hxx are left in a somewhat odd state for now, where they don't take advantage of OUStringLiteral's equivalence to rtl_uString, but just keep extracting its contents and copy it elsewhere. In follow-up commits, those consumers should be changed appropriately, making them treat OUStringLiteral like an rtl_uString or dropping the OUStringLiteral overload in favor of an existing (and cheap to use now) OUString overload, etc. In a similar vein, comparison operators between OUString and std::u16string_view have been added to the existing plethora of comparison operator overloads. It would be nice to eventually consolidate them, esp. with the overloads taking OUStringLiteral and/or char16_t const[N] string literals, but that appears tricky to get right without introducing new ambiguities. Also, a handful of places across the code base use comparisons between OUString and OUStringNumber, which are now ambiguous (converting the OUStringNumber to either OUString or std::u16string_view). For simplicity, those few places have manually been fixed for now by adding explicit conversion to std::u16string_view. Also some compilerplugins code needed to be adapted, and some of the compilerplugins/test cases have become irrelevant (and have been removed), as the tested code would no longer compile in the first place. sal/qa/rtl/strings/test_oustring_concat.cxx documents a workaround for GCC bug <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96878> "Failed class template argument deduction in unevaluated, parenthesized context". That place, as well as uses of OUStringLiteral in extensions/source/abpilot/fieldmappingimpl.cxx and i18npool/source/localedata/localedata.cxx, which have been replaced with OUString::Concat (and which is arguably a better choice, anyway), also caused failures with at least Clang 5.0.2 (but would not have caused failures with at least recent Clang 12 trunk, so appear to be bugs in Clang that have meanwhile been fixed). Change-Id: I34174462a28f2000cfeb2d219ffd533a767920b8 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/102222 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'include/rtl')
-rw-r--r--include/rtl/ustrbuf.hxx32
-rw-r--r--include/rtl/ustring.hxx234
2 files changed, 186 insertions, 80 deletions
diff --git a/include/rtl/ustrbuf.hxx b/include/rtl/ustrbuf.hxx
index 9800ba8d3020..56968def8d71 100644
--- a/include/rtl/ustrbuf.hxx
+++ b/include/rtl/ustrbuf.hxx
@@ -174,10 +174,10 @@ public:
}
/** @overload @since LibreOffice 5.4 */
- OUStringBuffer(OUStringLiteral const & literal):
- pData(nullptr), nCapacity(literal.size + 16) //TODO: check for overflow
+ template<std::size_t N> OUStringBuffer(OUStringLiteral<N> const & literal):
+ pData(nullptr), nCapacity(literal.getLength() + 16) //TODO: check for overflow
{
- rtl_uStringbuffer_newFromStr_WithLength(&pData, literal.data, literal.size);
+ rtl_uStringbuffer_newFromStr_WithLength(&pData, literal.getStr(), literal.getLength());
}
#endif
@@ -315,13 +315,13 @@ public:
}
/** @overload @since LibreOffice 5.4 */
- OUStringBuffer & operator =(OUStringLiteral const & literal) {
- sal_Int32 const n = literal.size;
+ template<std::size_t N> OUStringBuffer & operator =(OUStringLiteral<N> const & literal) {
+ sal_Int32 const n = literal.getLength();
if (n >= nCapacity) {
ensureCapacity(n + 16); //TODO: check for overflow
}
std::memcpy(
- pData->buffer, literal.data,
+ pData->buffer, literal.getStr(),
(n + 1) * sizeof (sal_Unicode)); //TODO: check for overflow
pData->length = n;
return *this;
@@ -659,8 +659,8 @@ public:
}
/** @overload @since LibreOffice 5.4 */
- OUStringBuffer & append(OUStringLiteral const & literal) {
- return append(literal.data, literal.size);
+ template<std::size_t N> OUStringBuffer & append(OUStringLiteral<N> const & literal) {
+ return append(literal.getStr(), literal.getLength());
}
#endif
@@ -1037,8 +1037,9 @@ public:
}
/** @overload @since LibreOffice 5.4 */
- OUStringBuffer & insert(sal_Int32 offset, OUStringLiteral const & literal) {
- return insert(offset, literal.data, literal.size);
+ template<std::size_t N>
+ OUStringBuffer & insert(sal_Int32 offset, OUStringLiteral<N> const & literal) {
+ return insert(offset, literal.getStr(), literal.getLength());
}
#endif
@@ -1446,12 +1447,13 @@ public:
}
/** @overload @since LibreOffice 5.4 */
- sal_Int32 indexOf(OUStringLiteral const & literal, sal_Int32 fromIndex = 0)
+ template<std::size_t N>
+ sal_Int32 indexOf(OUStringLiteral<N> const & literal, sal_Int32 fromIndex = 0)
const
{
sal_Int32 n = rtl_ustr_indexOfStr_WithLength(
- pData->buffer + fromIndex, pData->length - fromIndex, literal.data,
- literal.size);
+ pData->buffer + fromIndex, pData->length - fromIndex, literal.getStr(),
+ literal.getLength());
return n < 0 ? n : n + fromIndex;
}
#endif
@@ -1534,9 +1536,9 @@ public:
}
/** @overload @since LibreOffice 5.4 */
- sal_Int32 lastIndexOf(OUStringLiteral const & literal) const {
+ template<std::size_t N> sal_Int32 lastIndexOf(OUStringLiteral<N> const & literal) const {
return rtl_ustr_lastIndexOfStr_WithLength(
- pData->buffer, pData->length, literal.data, literal.size);
+ pData->buffer, pData->length, literal.getStr(), literal.getLength());
}
#endif
diff --git a/include/rtl/ustring.hxx b/include/rtl/ustring.hxx
index eebdc261e4f1..45272b325cc0 100644
--- a/include/rtl/ustring.hxx
+++ b/include/rtl/ustring.hxx
@@ -39,6 +39,7 @@
#include "rtl/textenc.h"
#ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
+#include "config_global.h"
#include "rtl/stringconcat.hxx"
#endif
@@ -68,35 +69,49 @@ class OUStringBuffer;
/// @cond INTERNAL
/**
-A simple wrapper around string literal.
+A wrapper dressing a string literal as a static-refcount rtl_uString.
This class is not part of public API and is meant to be used only in LibreOffice code.
@since LibreOffice 4.0
*/
-struct SAL_WARN_UNUSED OUStringLiteral
-{
- template<typename T> constexpr OUStringLiteral(
- T & literal,
- typename libreoffice_internal::ConstCharArrayDetector<
- T, libreoffice_internal::Dummy>::TypeUtf16
- = libreoffice_internal::Dummy()):
- size(libreoffice_internal::ConstCharArrayDetector<T>::length),
- data(
- libreoffice_internal::ConstCharArrayDetector<T>::toPointer(literal))
- {
- assert(
- libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal));
+template<std::size_t N> class SAL_WARN_UNUSED OUStringLiteral {
+ static_assert(N != 0);
+ static_assert(N - 1 <= std::numeric_limits<sal_Int32>::max(), "literal too long");
+
+public:
+#if HAVE_CPP_CONSTEVAL
+ consteval
+#else
+ constexpr
+#endif
+ OUStringLiteral(char16_t const (&literal)[N]) {
+ assert(literal[N - 1] == '\0');
+ //TODO: Use C++20 constexpr std::copy_n (P0202R3):
+ for (std::size_t i = 0; i != N; ++i) {
+ buffer[i] = literal[i];
+ }
}
- constexpr operator std::u16string_view() const { return {data, unsigned(size)}; }
+ constexpr sal_Int32 getLength() const { return length; }
+
+ constexpr sal_Unicode const * getStr() const SAL_RETURNS_NONNULL { return buffer; }
- int size;
- const sal_Unicode* data;
+ constexpr operator std::u16string_view() const { return {buffer, sal_uInt32(length)}; }
- // So we can use this struct in some places interchangeably with OUString
- constexpr sal_Int32 getLength() const { return size; }
+private:
+ // Same layout as rtl_uString (include/rtl/ustring.h):
+ oslInterlockedCount refCount = 0x40000000; // SAL_STRING_STATIC_FLAG (sal/rtl/strimp.hxx)
+ sal_Int32 length = N - 1;
+ sal_Unicode buffer[N] = {}; //TODO: drop initialization for C++20 (P1331R2)
};
+#if defined RTL_STRING_UNITTEST
+namespace libreoffice_internal {
+template<std::size_t N> struct ExceptConstCharArrayDetector<OUStringLiteral<N>> {};
+template<std::size_t N> struct ExceptCharArrayDetector<OUStringLiteral<N>> {};
+}
+#endif
+
/// @endcond
#endif
@@ -347,20 +362,11 @@ public:
/**
New string from a string literal.
- This constructor is similar to the "direct template" one, but can be
- useful in cases where the latter does not work, like in
-
- OUString(flag ? "a" : "bb")
-
- written as
-
- OUString(flag ? OUStringLiteral(u"a") : OUStringLiteral(u"bb"))
-
@since LibreOffice 5.0
*/
- OUString(OUStringLiteral literal): pData(NULL) {
- rtl_uString_newFromStr_WithLength(&pData, literal.data, literal.size);
- }
+ template<std::size_t N> OUString(OUStringLiteral<N> const & literal):
+ pData(const_cast<rtl_uString *>(reinterpret_cast<rtl_uString const *>(&literal))) {}
+ template<std::size_t N> OUString(OUStringLiteral<N> &&) = delete;
/// @endcond
#endif
@@ -552,11 +558,11 @@ public:
}
/** @overload @since LibreOffice 5.4 */
- OUString & operator =(OUStringLiteral const & literal) {
- if (literal.size == 0) {
+ template<std::size_t N> OUString & operator =(OUStringLiteral<N> const & literal) {
+ if (literal.getLength() == 0) {
rtl_uString_new(&pData);
} else {
- rtl_uString_newFromStr_WithLength(&pData, literal.data, literal.size);
+ rtl_uString_newFromStr_WithLength(&pData, literal.getStr(), literal.getLength());
}
return *this;
}
@@ -653,11 +659,11 @@ public:
operator +=(T &) && = delete;
/** @overload @since LibreOffice 5.4 */
- OUString & operator +=(OUStringLiteral const & literal) & {
- rtl_uString_newConcatUtf16L(&pData, pData, literal.data, literal.size);
+ template<std::size_t N> OUString & operator +=(OUStringLiteral<N> const & literal) & {
+ rtl_uString_newConcatUtf16L(&pData, pData, literal.getStr(), literal.getLength());
return *this;
}
- void operator +=(OUStringLiteral const &) && = delete;
+ template<std::size_t N> void operator +=(OUStringLiteral<N> const &) && = delete;
OUString & operator +=(std::u16string_view sv) & {
if (sv.size() > sal_uInt32(std::numeric_limits<sal_Int32>::max())) {
@@ -1812,93 +1818,191 @@ public:
@since LibreOffice 5.0
*/
- friend bool operator ==(OUString const & lhs, OUStringLiteral const & rhs) {
+ template<std::size_t N>
+ friend bool operator ==(OUString const & lhs, OUStringLiteral<N> const & rhs) {
return
rtl_ustr_reverseCompare_WithLength(
- lhs.pData->buffer, lhs.pData->length, rhs.data, rhs.size)
+ lhs.pData->buffer, lhs.pData->length, rhs.getStr(), rhs.getLength())
== 0;
}
- friend bool operator !=(OUString const & lhs, OUStringLiteral const & rhs) {
+ template<std::size_t N>
+ friend bool operator !=(OUString const & lhs, OUStringLiteral<N> const & rhs) {
return
rtl_ustr_reverseCompare_WithLength(
- lhs.pData->buffer, lhs.pData->length, rhs.data, rhs.size)
+ lhs.pData->buffer, lhs.pData->length, rhs.getStr(), rhs.getLength())
!= 0;
}
- friend bool operator <(OUString const & lhs, OUStringLiteral const & rhs) {
+ template<std::size_t N>
+ friend bool operator <(OUString const & lhs, OUStringLiteral<N> const & rhs) {
return
(rtl_ustr_compare_WithLength(
- lhs.pData->buffer, lhs.pData->length, rhs.data, rhs.size))
+ lhs.pData->buffer, lhs.pData->length, rhs.getStr(), rhs.getLength()))
< 0;
}
- friend bool operator <=(OUString const & lhs, OUStringLiteral const & rhs) {
+ template<std::size_t N>
+ friend bool operator <=(OUString const & lhs, OUStringLiteral<N> const & rhs) {
return
(rtl_ustr_compare_WithLength(
- lhs.pData->buffer, lhs.pData->length, rhs.data, rhs.size))
+ lhs.pData->buffer, lhs.pData->length, rhs.getStr(), rhs.getLength()))
<= 0;
}
- friend bool operator >(OUString const & lhs, OUStringLiteral const & rhs) {
+ template<std::size_t N>
+ friend bool operator >(OUString const & lhs, OUStringLiteral<N> const & rhs) {
return
(rtl_ustr_compare_WithLength(
- lhs.pData->buffer, lhs.pData->length, rhs.data, rhs.size))
+ lhs.pData->buffer, lhs.pData->length, rhs.getStr(), rhs.getLength()))
> 0;
}
- friend bool operator >=(OUString const & lhs, OUStringLiteral const & rhs) {
+ template<std::size_t N>
+ friend bool operator >=(OUString const & lhs, OUStringLiteral<N> const & rhs) {
return
(rtl_ustr_compare_WithLength(
- lhs.pData->buffer, lhs.pData->length, rhs.data, rhs.size))
+ lhs.pData->buffer, lhs.pData->length, rhs.getStr(), rhs.getLength()))
>= 0;
}
- friend bool operator ==(OUStringLiteral const & lhs, OUString const & rhs) {
+ template<std::size_t N>
+ friend bool operator ==(OUStringLiteral<N> const & lhs, OUString const & rhs) {
return
rtl_ustr_reverseCompare_WithLength(
- lhs.data, lhs.size, rhs.pData->buffer, rhs.pData->length)
+ lhs.getStr(), lhs.getLength(), rhs.pData->buffer, rhs.pData->length)
== 0;
}
- friend bool operator !=(OUStringLiteral const & lhs, OUString const & rhs) {
+ template<std::size_t N>
+ friend bool operator !=(OUStringLiteral<N> const & lhs, OUString const & rhs) {
return
rtl_ustr_reverseCompare_WithLength(
- lhs.data, lhs.size, rhs.pData->buffer, rhs.pData->length)
+ lhs.getStr(), lhs.getLength(), rhs.pData->buffer, rhs.pData->length)
!= 0;
}
- friend bool operator <(OUStringLiteral const & lhs, OUString const & rhs) {
+ template<std::size_t N>
+ friend bool operator <(OUStringLiteral<N> const & lhs, OUString const & rhs) {
return
(rtl_ustr_compare_WithLength(
- lhs.data, lhs.size, rhs.pData->buffer, rhs.pData->length))
+ lhs.getStr(), lhs.getLength(), rhs.pData->buffer, rhs.pData->length))
< 0;
}
- friend bool operator <=(OUStringLiteral const & lhs, OUString const & rhs) {
+ template<std::size_t N>
+ friend bool operator <=(OUStringLiteral<N> const & lhs, OUString const & rhs) {
return
(rtl_ustr_compare_WithLength(
- lhs.data, lhs.size, rhs.pData->buffer, rhs.pData->length))
+ lhs.getStr(), lhs.getLength(), rhs.pData->buffer, rhs.pData->length))
<= 0;
}
- friend bool operator >(OUStringLiteral const & lhs, OUString const & rhs) {
+ template<std::size_t N>
+ friend bool operator >(OUStringLiteral<N> const & lhs, OUString const & rhs) {
return
(rtl_ustr_compare_WithLength(
- lhs.data, lhs.size, rhs.pData->buffer, rhs.pData->length))
+ lhs.getStr(), lhs.getLength(), rhs.pData->buffer, rhs.pData->length))
> 0;
}
- friend bool operator >=(OUStringLiteral const & lhs, OUString const & rhs) {
+ template<std::size_t N>
+ friend bool operator >=(OUStringLiteral<N> const & lhs, OUString const & rhs) {
return
(rtl_ustr_compare_WithLength(
- lhs.data, lhs.size, rhs.pData->buffer, rhs.pData->length))
+ lhs.getStr(), lhs.getLength(), rhs.pData->buffer, rhs.pData->length))
>= 0;
}
/// @endcond
#endif
+#if defined LIBO_INTERNAL_ONLY
+ friend bool operator ==(OUString const & lhs, std::u16string_view rhs) {
+ return
+ rtl_ustr_reverseCompare_WithLength(
+ lhs.pData->buffer, lhs.pData->length, rhs.data(), rhs.size())
+ == 0;
+ }
+
+ friend bool operator !=(OUString const & lhs, std::u16string_view rhs) {
+ return
+ rtl_ustr_reverseCompare_WithLength(
+ lhs.pData->buffer, lhs.pData->length, rhs.data(), rhs.size())
+ != 0;
+ }
+
+ friend bool operator <(OUString const & lhs, std::u16string_view rhs) {
+ return
+ (rtl_ustr_compare_WithLength(
+ lhs.pData->buffer, lhs.pData->length, rhs.data(), rhs.size()))
+ < 0;
+ }
+
+ friend bool operator <=(OUString const & lhs, std::u16string_view rhs) {
+ return
+ (rtl_ustr_compare_WithLength(
+ lhs.pData->buffer, lhs.pData->length, rhs.data(), rhs.size()))
+ <= 0;
+ }
+
+ friend bool operator >(OUString const & lhs, std::u16string_view rhs) {
+ return
+ (rtl_ustr_compare_WithLength(
+ lhs.pData->buffer, lhs.pData->length, rhs.data(), rhs.size()))
+ > 0;
+ }
+
+ friend bool operator >=(OUString const & lhs, std::u16string_view rhs) {
+ return
+ (rtl_ustr_compare_WithLength(
+ lhs.pData->buffer, lhs.pData->length, rhs.data(), rhs.size()))
+ >= 0;
+ }
+
+ friend bool operator ==(std::u16string_view lhs, OUString const & rhs) {
+ return
+ rtl_ustr_reverseCompare_WithLength(
+ lhs.data(), lhs.size(), rhs.pData->buffer, rhs.pData->length)
+ == 0;
+ }
+
+ friend bool operator !=(std::u16string_view lhs, OUString const & rhs) {
+ return
+ rtl_ustr_reverseCompare_WithLength(
+ lhs.data(), lhs.size(), rhs.pData->buffer, rhs.pData->length)
+ != 0;
+ }
+
+ friend bool operator <(std::u16string_view lhs, OUString const & rhs) {
+ return
+ (rtl_ustr_compare_WithLength(
+ lhs.data(), lhs.size(), rhs.pData->buffer, rhs.pData->length))
+ < 0;
+ }
+
+ friend bool operator <=(std::u16string_view lhs, OUString const & rhs) {
+ return
+ (rtl_ustr_compare_WithLength(
+ lhs.data(), lhs.size(), rhs.pData->buffer, rhs.pData->length))
+ <= 0;
+ }
+
+ friend bool operator >(std::u16string_view lhs, OUString const & rhs) {
+ return
+ (rtl_ustr_compare_WithLength(
+ lhs.data(), lhs.size(), rhs.pData->buffer, rhs.pData->length))
+ > 0;
+ }
+
+ friend bool operator >=(std::u16string_view lhs, OUString const & rhs) {
+ return
+ (rtl_ustr_compare_WithLength(
+ lhs.data(), lhs.size(), rhs.pData->buffer, rhs.pData->length))
+ >= 0;
+ }
+#endif
+
/**
Returns a hashcode for this string.
@@ -3295,11 +3399,11 @@ struct ToStringHelper< OUString >
/**
@internal
*/
-template<>
-struct ToStringHelper< OUStringLiteral >
+template<std::size_t N>
+struct ToStringHelper< OUStringLiteral<N> >
{
- static std::size_t length( const OUStringLiteral& str ) { return str.size; }
- static sal_Unicode* addData( sal_Unicode* buffer, const OUStringLiteral& str ) { return addDataHelper( buffer, str.data, str.size ); }
+ static std::size_t length( const OUStringLiteral<N>& str ) { return str.getLength(); }
+ static sal_Unicode* addData( sal_Unicode* buffer, const OUStringLiteral<N>& str ) { return addDataHelper( buffer, str.getStr(), str.getLength() ); }
static const bool allowOStringConcat = false;
static const bool allowOUStringConcat = true;
};