summaryrefslogtreecommitdiff
path: root/include/rtl/string.hxx
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2020-08-31 21:19:22 +0200
committerStephan Bergmann <sbergman@redhat.com>2020-09-02 08:12:04 +0200
commit4b9e440c51be3e40326bc90c33ae69885bfb51e4 (patch)
treed248768cea9dff7307d00036eb697778ed58ce85 /include/rtl/string.hxx
parent5aba7d0bc54a03247fbd5e6514f79fff846f527c (diff)
Turn OStringLiteral into a consteval'ed, static-refcound rtl_String
...from which an OString can cheaply be instantiated. The one downside is that OStringLiteral 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 containers 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::string_view, without loss of efficiency compared to the original OStringLiteral, and without loss of expressivity (esp. with the newly introduced OString(std::string_view) ctor). The new OStringLiteral 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 OStringLiteral is in all cases where an object that shall itself not be an OString (e.g., because it shall be a global static variable for which the OString ctor/dtor would be detrimental at library load/unload) must be converted to an OString instance in at least one place. Other string literal abstractions could use std::string_view (or just plain char const[N]), but interestingly OStringLiteral might be more efficient than constexpr std::string_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. sal/qa/rtl/strings/test_ostring_concat.cxx documents some workarounds for GCC bug <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96878> "Failed class template argument deduction in unevaluated, parenthesized context". Those places, as well as uses of OStringLiteral in incodemaker/source/javamaker/javaoptions.cxx and i18npool/source/breakiterator/breakiterator_unicode.cxx, which have been replaced with OString::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). This change also revealed a bug in at least recent Clang 12 trunk CastExpr::getSubExprAsWritten (still to be reported to LLVM), triggered at least in some calls from loplugin code (for which it can be fixed for now in the existing compat::getSubStringAsWritten). A similar commit for OUStringLiteral is planned, too. Change-Id: Ib192f4ed4c44769512a16364cb55c25627bae6f4 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/101814 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'include/rtl/string.hxx')
-rw-r--r--include/rtl/string.hxx90
1 files changed, 55 insertions, 35 deletions
diff --git a/include/rtl/string.hxx b/include/rtl/string.hxx
index da75e5eee57c..ca1fd99b7fba 100644
--- a/include/rtl/string.hxx
+++ b/include/rtl/string.hxx
@@ -24,6 +24,7 @@
#include <cassert>
#include <cstddef>
+#include <limits>
#include <new>
#include <ostream>
#include <utility>
@@ -38,6 +39,7 @@
#include "rtl/stringutils.hxx"
#ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
+#include "config_global.h"
#include "rtl/stringconcat.hxx"
#endif
@@ -70,34 +72,53 @@ namespace rtl
#ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
/**
-A simple wrapper around string literal. It is usually not necessary to use, can
-be mostly used to force OString operator+ working with operands that otherwise would
-not trigger it.
+A wrapper dressing a string literal as a static-refcount rtl_String.
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 OStringLiteral
-{
- template< int N >
- explicit OStringLiteral( const char (&str)[ N ] ) : size( N - 1 ), data( str ) { assert( strlen( str ) == N - 1 ); }
+template<std::size_t N> class SAL_WARN_UNUSED OStringLiteral {
+ 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
+ explicit OStringLiteral(char 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];
+ }
+ }
+
#if defined __cpp_char8_t
- template< int N >
- explicit OStringLiteral( const char8_t (&str)[ N ] ) : size( N - 1 ), data( reinterpret_cast<char const *>(str) ) { assert( strlen( data ) == N - 1 ); }
+#if HAVE_CPP_CONSTEVAL
+ consteval
+#else
+ constexpr
+#endif
+ explicit OStringLiteral(char8_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];
+ }
+ }
#endif
- int size;
- const char* data;
+ constexpr sal_Int32 getLength() const { return length; }
- /** So we can use this in some places interchangeably with OUString.
- * @since LibreOffice 7.1
- */
- constexpr sal_Int32 getLength() const { return size; }
+ constexpr char const * getStr() const SAL_RETURNS_NONNULL { return buffer; }
- /** So we can use this in some places interchangeably with OString.
- * @since LibreOffice 7.1
- */
- constexpr const char* getStr() const { return data; }
+private:
+ // Same layout as rtl_String (include/rtl/string.h):
+ oslInterlockedCount refCount = 0x40000000; // SAL_STRING_STATIC_FLAG (sal/rtl/strimp.hxx)
+ sal_Int32 length = N - 1;
+ char buffer[N] = {}; //TODO: drop initialization for C++20 (P1331R2)
};
#endif
@@ -274,23 +295,22 @@ public:
/**
New string from an 8-Bit 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
-
- OString(flag ? "a" : "bb")
-
- written as
-
- OString(flag ? OStringLiteral("a") : OStringLiteral("bb"))
-
@since LibreOffice 7.1
*/
- OString(OStringLiteral literal): pData(NULL) {
- rtl_string_newFromLiteral(&pData, literal.data, literal.size, 0);
- }
+ template<std::size_t N> OString(OStringLiteral<N> const & literal):
+ pData(const_cast<rtl_String *>(reinterpret_cast<rtl_String const *>(&literal))) {}
/// @endcond
#endif
+#if defined LIBO_INTERNAL_ONLY
+ explicit OString(std::string_view sv) {
+ if (sv.size() > sal_uInt32(std::numeric_limits<sal_Int32>::max())) {
+ throw std::bad_alloc();
+ }
+ pData = nullptr;
+ rtl_string_newFromStr_WithLength(&pData, sv.data(), sv.size());
+ }
+#endif
/**
New string from a Unicode character buffer array.
@@ -1946,11 +1966,11 @@ struct ToStringHelper< OString >
/**
@internal
*/
-template<>
-struct ToStringHelper< OStringLiteral >
+template<std::size_t N>
+struct ToStringHelper< OStringLiteral<N> >
{
- static std::size_t length( const OStringLiteral& str ) { return str.size; }
- static char* addData( char* buffer, const OStringLiteral& str ) { return addDataHelper( buffer, str.data, str.size ); }
+ static constexpr std::size_t length( const OStringLiteral<N>& str ) { return str.getLength(); }
+ static char* addData( char* buffer, const OStringLiteral<N>& str ) { return addDataHelper( buffer, str.getStr(), str.getLength() ); }
static const bool allowOStringConcat = true;
static const bool allowOUStringConcat = false;
};