include/rtl/string.hxx                              |   51 +++++++++++++++-----
 include/rtl/ustring.hxx                             |   37 +++++++++-----
 sal/qa/rtl/strings/test_ostring_stringliterals.cxx  |    3 +
 sal/qa/rtl/strings/test_oustring_stringliterals.cxx |    3 +
 4 files changed, 70 insertions(+), 24 deletions(-)

New commits:
commit 6fce450b8a66d7e98a4d39528f8591184277e9fa
Author:     Stephan Bergmann <sberg...@redhat.com>
AuthorDate: Fri Jul 21 14:20:57 2023 +0200
Commit:     Stephan Bergmann <sberg...@redhat.com>
CommitDate: Mon Jul 31 07:58:08 2023 +0200

    Introduce "..."_tstr as a companion to "..."_ostr/u"..."_ustr in template 
code
    
    ...like
    
    >         *path = "/";
    
    in
    
    > template<typename T> oslFileError getSystemPathFromFileUrl(
    >     OUString const & url, T * path, bool resolveHome)
    
    in sal/osl/unx/file_url.cxx, where T is either OString or OUString.  That 
can
    now be replaced with
    
    >         *path = "/"_tstr;
    
    and for OString it will be a no-cost constexpr operation, while for 
OUString it
    will still construct the OUString instance at runtime (as did the original 
code
    for both OString and OUString).
    
    (This change required moving the definition of rtl::detail::OStringHolder
    around in include/rtl/string.hxx.  For consistency, I similarly moved 
around the
    definition of rtl::detail::OUStringHolder in include/rtl/ustring.hxx, too.)
    
    Change-Id: I7fb3a81b438f98c609684f7e70b7a60068d32fdb
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/154748
    Tested-by: Jenkins
    Reviewed-by: Stephan Bergmann <sberg...@redhat.com>

diff --git a/include/rtl/string.hxx b/include/rtl/string.hxx
index aae291f46053..eae18bc30d0d 100644
--- a/include/rtl/string.hxx
+++ b/include/rtl/string.hxx
@@ -186,6 +186,18 @@ private:
     rtl_String* pData;
 };
 
+#if __cplusplus >= 202002L
+
+namespace detail {
+
+template<OStringLiteral L> struct OStringHolder {
+    static constexpr auto & literal = L;
+};
+
+}
+
+#endif
+
 #endif
 
 /* ======================================================================= */
@@ -384,6 +396,12 @@ public:
     /// @endcond
 #endif
 
+#if defined LIBO_INTERNAL_ONLY && __cplusplus >= 202002L
+    // For operator ""_tstr:
+    template<OStringLiteral L> constexpr OString(detail::OStringHolder<L> 
const & holder):
+        pData(const_cast<rtl_String *>(&holder.literal.str)) {}
+#endif
+
 #if defined LIBO_INTERNAL_ONLY
     explicit OString(std::string_view sv) {
         if (sv.size() > sal_uInt32(std::numeric_limits<sal_Int32>::max())) {
@@ -2275,18 +2293,6 @@ inline bool operator !=(StringConcatenation<char> const 
& lhs, OString const & r
 
 #ifdef LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
 
-#if __cplusplus >= 202002L
-
-namespace detail {
-
-template<OStringLiteral L> struct OStringHolder {
-    static constexpr auto & literal = L;
-};
-
-}
-
-#endif
-
 /**
  @internal
 */
@@ -2409,6 +2415,27 @@ operator ""_ostr() {
         ::detail::OStringHolder<L>::literal;
 }
 
+template<
+#if defined RTL_STRING_UNITTEST
+    rtlunittest::
+#endif
+    OStringLiteral L>
+constexpr
+#if defined RTL_STRING_UNITTEST
+rtlunittest
+#else
+rtl
+#endif
+::detail::OStringHolder<L> operator ""_tstr() {
+    return
+#if defined RTL_STRING_UNITTEST
+        rtlunittest
+#else
+        rtl
+#endif
+        ::detail::OStringHolder<L>();
+}
+
 #endif
 
 /// @cond INTERNAL
diff --git a/include/rtl/ustring.hxx b/include/rtl/ustring.hxx
index 90b195754a17..c273c76ace6a 100644
--- a/include/rtl/ustring.hxx
+++ b/include/rtl/ustring.hxx
@@ -175,6 +175,18 @@ private:
     rtl_uString* pData;
 };
 
+#if __cplusplus >= 202002L
+
+namespace detail {
+
+template<OUStringLiteral L> struct OUStringHolder {
+    static constexpr auto & literal = L;
+};
+
+}
+
+#endif
+
 /// @endcond
 #endif
 
@@ -440,6 +452,19 @@ public:
     /// @endcond
 #endif
 
+#if defined LIBO_INTERNAL_ONLY && __cplusplus >= 202002L
+    // For operator ""_tstr:
+    template<OStringLiteral L> OUString(detail::OStringHolder<L> const & 
holder) {
+        pData = nullptr;
+        if (holder.literal.getLength() == 0) {
+            rtl_uString_new(&pData);
+        } else {
+            rtl_uString_newFromLiteral(
+                &pData, holder.literal.getStr(), holder.literal.getLength(), 
0);
+        }
+    }
+#endif
+
     /**
       New string from an 8-Bit character buffer array.
 
@@ -3414,18 +3439,6 @@ inline bool operator !=(StringConcatenation<char16_t> 
const & lhs, OUString cons
 #if defined LIBO_INTERNAL_ONLY // "RTL_FAST_STRING"
 /// @cond INTERNAL
 
-#if __cplusplus >= 202002L
-
-namespace detail {
-
-template<OUStringLiteral L> struct OUStringHolder {
-    static constexpr auto & literal = L;
-};
-
-}
-
-#endif
-
 /**
  @internal
 */
diff --git a/sal/qa/rtl/strings/test_ostring_stringliterals.cxx 
b/sal/qa/rtl/strings/test_ostring_stringliterals.cxx
index 8cfc3a65bdde..1017106e2800 100644
--- a/sal/qa/rtl/strings/test_ostring_stringliterals.cxx
+++ b/sal/qa/rtl/strings/test_ostring_stringliterals.cxx
@@ -280,6 +280,9 @@ void test::ostring::StringLiterals::checkOstr() {
     CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ""_ostr.getLength());
     CPPUNIT_ASSERT_EQUAL(sal_Int32(6), "foobar"_ostr.getLength());
     CPPUNIT_ASSERT_EQUAL(sal_Int32(7), "foo\0bar"_ostr.getLength());
+    CPPUNIT_ASSERT_EQUAL(""_ostr, rtl::OString(""_tstr));
+    CPPUNIT_ASSERT_EQUAL("foobar"_ostr, rtl::OString("foobar"_tstr));
+    CPPUNIT_ASSERT_EQUAL("foo\0bar"_ostr, rtl::OString("foo\0bar"_tstr));
 #endif
 }
 
diff --git a/sal/qa/rtl/strings/test_oustring_stringliterals.cxx 
b/sal/qa/rtl/strings/test_oustring_stringliterals.cxx
index 709ddb669cc5..197f72a50fea 100644
--- a/sal/qa/rtl/strings/test_oustring_stringliterals.cxx
+++ b/sal/qa/rtl/strings/test_oustring_stringliterals.cxx
@@ -432,6 +432,9 @@ void test::oustring::StringLiterals::checkOstr() {
     CPPUNIT_ASSERT_EQUAL(sal_Int32(0), u""_ustr.getLength());
     CPPUNIT_ASSERT_EQUAL(sal_Int32(6), u"foobar"_ustr.getLength());
     CPPUNIT_ASSERT_EQUAL(sal_Int32(7), u"foo\0bar"_ustr.getLength());
+    CPPUNIT_ASSERT_EQUAL(u""_ustr, rtl::OUString(""_tstr));
+    CPPUNIT_ASSERT_EQUAL(u"foobar"_ustr, rtl::OUString("foobar"_tstr));
+    CPPUNIT_ASSERT_EQUAL(u"foo\0bar"_ustr, rtl::OUString("foo\0bar"_tstr));
 #endif
 }
 

Reply via email to