summaryrefslogtreecommitdiff
path: root/include/rtl
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2020-08-28 17:14:50 +0200
committerStephan Bergmann <sbergman@redhat.com>2020-08-30 20:42:56 +0200
commit2e21240f23ac2191a3535d697a7308b29303c67c (patch)
tree89f6e634912c2d1d476cddb2550e7368b10427b9 /include/rtl
parent6379f799704935a571a4b3af44cabd0671c48dbe (diff)
Goodbye O[U]StringView, welcome O[U]String::Concat
O[U]StringView had an odd mixture of uses. For one, it was used like std::[u16]string_view, for which directly using the latter std types is clearly the better alternative. For another, it was used in concatenation sequences, when neither of the two leading terms were of our rtl string-related types. For that second use case introduce O[U]String::Concat (as std::[u16]string_view can obviously not be used, those not being one of our rtl string-related types). Also, O[U]StringLiteral is occasionally used for this, but the planned changes outlined in the 33ecd0d5c4fff9511a8436513936a3f7044a775a "Change OUStringLiteral from char[] to char16_t[]" commit message will make that no longer work, so O[U]String::Concat will be the preferred solution in such use cases going forward, too. O[U]StringView was also occasionally used to include O[U]StringBuffer values in concatenation sequences, for which a more obvious alternative is to make O[U]StringBuffer participate directly in the ToStringHelper/O[U]StringConcat machinery. Change-Id: I1f0e8d836796c9ae01c45f32c518be5f52976622 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/101586 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'include/rtl')
-rw-r--r--include/rtl/strbuf.hxx19
-rw-r--r--include/rtl/string.hxx25
-rw-r--r--include/rtl/stringconcat.hxx87
-rw-r--r--include/rtl/ustrbuf.hxx19
-rw-r--r--include/rtl/ustring.hxx25
5 files changed, 112 insertions, 63 deletions
diff --git a/include/rtl/strbuf.hxx b/include/rtl/strbuf.hxx
index 2a303ce998e6..e7e6cc01a2d0 100644
--- a/include/rtl/strbuf.hxx
+++ b/include/rtl/strbuf.hxx
@@ -1035,13 +1035,6 @@ public:
*pInternalCapacity = &nCapacity;
}
-#if defined LIBO_INTERNAL_ONLY
- explicit operator OStringView() const
- {
- return OStringView(getStr(), getLength());
- }
-#endif
-
private:
/**
A pointer to the data structure which contains the data.
@@ -1054,6 +1047,18 @@ private:
sal_Int32 nCapacity;
};
+#if defined LIBO_INTERNAL_ONLY
+template<> struct ToStringHelper<OStringBuffer> {
+ static std::size_t length(OStringBuffer const & s) { return s.getLength(); }
+
+ static char * addData(char * buffer, OStringBuffer const & s) SAL_RETURNS_NONNULL
+ { return addDataHelper(buffer, s.getStr(), s.getLength()); }
+
+ static constexpr bool allowOStringConcat = true;
+ static constexpr bool allowOUStringConcat = false;
+};
+#endif
+
}
#ifdef RTL_STRING_UNITTEST
diff --git a/include/rtl/string.hxx b/include/rtl/string.hxx
index 35eccfc073c3..60485fbdfb96 100644
--- a/include/rtl/string.hxx
+++ b/include/rtl/string.hxx
@@ -1900,6 +1900,31 @@ public:
#if defined LIBO_INTERNAL_ONLY
operator std::string_view() const { return {getStr(), sal_uInt32(getLength())}; }
#endif
+
+#if defined LIBO_INTERNAL_ONLY
+ // A wrapper for the first expression in an
+ //
+ // OString::Concat(e1) + e2 + ...
+ //
+ // concatenation chain, when neither of the first two e1, e2 is one of our rtl string-related
+ // classes (so something like
+ //
+ // OString s = "a" + (b ? std::string_view("c" : "dd");
+ //
+ // would not compile):
+ template<typename T> [[nodiscard]] static
+ typename std::enable_if_t<
+ ToStringHelper<T>::allowOStringConcat, OStringConcat<OStringConcatMarker, T>>
+ Concat(T const & value) { return OStringConcat<OStringConcatMarker, T>({}, value); }
+
+ // This overload is needed so that an argument of type 'char const[N]' ends up as
+ // 'OStringConcat<rtl::OStringConcatMarker, char const[N]>' rather than as
+ // 'OStringConcat<rtl::OStringConcatMarker, char[N]>':
+ template<typename T, std::size_t N> [[nodiscard]] static
+ typename std::enable_if_t<
+ ToStringHelper<T[N]>::allowOStringConcat, OStringConcat<OStringConcatMarker, T[N]>>
+ Concat(T (& value)[N]) { return OStringConcat<OStringConcatMarker, T[N]>({}, value); }
+#endif
};
/* ======================================================================= */
diff --git a/include/rtl/stringconcat.hxx b/include/rtl/stringconcat.hxx
index f5c9a4d33a52..e2cba5d86f0d 100644
--- a/include/rtl/stringconcat.hxx
+++ b/include/rtl/stringconcat.hxx
@@ -490,63 +490,52 @@ struct ToStringHelper< OUStringNumber< T > >
static const bool allowOUStringConcat = true;
};
-// Abstractions over null-terminated char and sal_Unicode strings that sometimes are needed in
-// concatenations of multiple such raw strings, as in
-//
-// char const * s1, s2;
-// OString s = OStringView(s1) + s2;
-//
-// (Providing specializations of ToStringHelper<std::string_view> and
-// ToStringHelper<std::u16string_view> would look dubious, as it would give meaning to expressions
-// like
-//
-// std::string_view(s1) + s2
-//
-// that do not involve any user-defined types.)
-
-class OStringView {
-public:
- explicit OStringView(char const * s): view_(s) {}
- explicit OStringView(char const * s, size_t len): view_(s, len) {}
-
- std::size_t length() const { return view_.length(); }
-
- char const * data() const { return view_.data(); }
-
-private:
- std::string_view view_;
+template<> struct ToStringHelper<std::string_view> {
+ static constexpr std::size_t length(std::string_view s) { return s.size(); }
+
+ static char * addData(char * buffer, std::string_view s) SAL_RETURNS_NONNULL
+ { return addDataHelper(buffer, s.data(), s.size()); }
+
+ static constexpr bool allowOStringConcat = true;
+ static constexpr bool allowOUStringConcat = false;
};
-template<>
-struct ToStringHelper< OStringView >
- {
- static std::size_t length( const OStringView& v ) { return v.length(); }
- static char* addData( char* buffer, const OStringView& v ) SAL_RETURNS_NONNULL { return addDataHelper( buffer, v.data(), v.length() ); }
- static const bool allowOStringConcat = true;
- static const bool allowOUStringConcat = false;
- };
+template<> struct ToStringHelper<std::u16string_view> {
+ static constexpr std::size_t length(std::u16string_view s) { return s.size(); }
+
+ static sal_Unicode * addData(sal_Unicode * buffer, std::u16string_view s) SAL_RETURNS_NONNULL
+ { return addDataHelper(buffer, s.data(), s.size()); }
+
+ static constexpr bool allowOStringConcat = false;
+ static constexpr bool allowOUStringConcat = true;
+};
-class OUStringView {
-public:
- explicit OUStringView(sal_Unicode const * s): view_(s) {}
- explicit OUStringView(sal_Unicode const * s, size_t len): view_(s, len) {}
+// An internal marker class used by OString::Concat:
+struct OStringConcatMarker {};
- std::size_t length() const { return view_.length(); }
+template<> struct ToStringHelper<OStringConcatMarker> {
+ static constexpr std::size_t length(OStringConcatMarker) { return 0; }
- sal_Unicode const * data() const { return view_.data(); }
+ static constexpr char * addData(char * buffer, OStringConcatMarker) SAL_RETURNS_NONNULL
+ { return buffer; }
-private:
- std::u16string_view view_;
+ static constexpr bool allowOStringConcat = true;
+ static constexpr bool allowOUStringConcat = false;
};
-template<>
-struct ToStringHelper< OUStringView >
- {
- static std::size_t length( const OUStringView& v ) { return v.length(); }
- static sal_Unicode* addData( sal_Unicode* buffer, const OUStringView& v ) SAL_RETURNS_NONNULL { return addDataHelper( buffer, v.data(), v.length() ); }
- static const bool allowOStringConcat = false;
- static const bool allowOUStringConcat = true;
- };
+// An internal marker class used by OUString::Concat:
+struct OUStringConcatMarker {};
+
+template<> struct ToStringHelper<OUStringConcatMarker> {
+ static constexpr std::size_t length(OUStringConcatMarker) { return 0; }
+
+ static constexpr sal_Unicode * addData(sal_Unicode * buffer, OUStringConcatMarker)
+ SAL_RETURNS_NONNULL
+ { return buffer; }
+
+ static constexpr bool allowOStringConcat = false;
+ static constexpr bool allowOUStringConcat = true;
+};
} // namespace
diff --git a/include/rtl/ustrbuf.hxx b/include/rtl/ustrbuf.hxx
index 9e8a586279b0..a4936b8fd186 100644
--- a/include/rtl/ustrbuf.hxx
+++ b/include/rtl/ustrbuf.hxx
@@ -1643,13 +1643,6 @@ public:
return OUStringBuffer( pNew, count + 16 );
}
-#if defined LIBO_INTERNAL_ONLY
- explicit operator OUStringView() const
- {
- return OUStringView(getStr(), getLength());
- }
-#endif
-
private:
OUStringBuffer( rtl_uString * value, const sal_Int32 capacity )
{
@@ -1669,6 +1662,18 @@ private:
};
#if defined LIBO_INTERNAL_ONLY
+template<> struct ToStringHelper<OUStringBuffer> {
+ static std::size_t length(OUStringBuffer const & s) { return s.getLength(); }
+
+ static sal_Unicode * addData(sal_Unicode * buffer, OUStringBuffer const & s) SAL_RETURNS_NONNULL
+ { return addDataHelper(buffer, s.getStr(), s.getLength()); }
+
+ static constexpr bool allowOStringConcat = false;
+ static constexpr bool allowOUStringConcat = true;
+};
+#endif
+
+#if defined LIBO_INTERNAL_ONLY
// Define this here to avoid circular includes
inline OUString & OUString::operator+=( const OUStringBuffer & str ) &
{
diff --git a/include/rtl/ustring.hxx b/include/rtl/ustring.hxx
index 229ed22aeb45..b99cf758ed8b 100644
--- a/include/rtl/ustring.hxx
+++ b/include/rtl/ustring.hxx
@@ -3693,6 +3693,31 @@ public:
operator std::u16string_view() const { return {getStr(), sal_uInt32(getLength())}; }
#endif
+#if defined LIBO_INTERNAL_ONLY
+ // A wrapper for the first expression in an
+ //
+ // OUString::Concat(e1) + e2 + ...
+ //
+ // concatenation chain, when neither of the first two e1, e2 is one of our rtl string-related
+ // classes (so something like
+ //
+ // OUString s = "a" + (b ? std::u16string_view(u"c" : u"dd");
+ //
+ // would not compile):
+ template<typename T> [[nodiscard]] static
+ typename std::enable_if_t<
+ ToStringHelper<T>::allowOUStringConcat, OUStringConcat<OUStringConcatMarker, T>>
+ Concat(T const & value) { return OUStringConcat<OUStringConcatMarker, T>({}, value); }
+
+ // This overload is needed so that an argument of type 'char const[N]' ends up as
+ // 'OUStringConcat<rtl::OUStringConcatMarker, char const[N]>' rather than as
+ // 'OUStringConcat<rtl::OUStringConcatMarker, char[N]>':
+ template<typename T, std::size_t N> [[nodiscard]] static
+ typename std::enable_if_t<
+ ToStringHelper<T[N]>::allowOUStringConcat, OUStringConcat<OUStringConcatMarker, T[N]>>
+ Concat(T (& value)[N]) { return OUStringConcat<OUStringConcatMarker, T[N]>({}, value); }
+#endif
+
private:
OUString & internalAppend( rtl_uString* pOtherData )
{