diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2020-08-27 11:40:59 +0200 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2020-08-28 08:07:09 +0200 |
commit | 33ecd0d5c4fff9511a8436513936a3f7044a775a (patch) | |
tree | c25809adda140ff89d9f2a2b6dfadba17e188fb0 /include/rtl | |
parent | 554834484a3323f73b5aeace246bcd9635368967 (diff) |
Change OUStringLiteral from char[] to char16_t[]
This is a prerequisite for making conversion from OUStringLiteral to OUString
more efficient at least for C++20 (by replacing its internals with a constexpr-
generated sal_uString-compatible layout with a SAL_STRING_STATIC_FLAG refCount,
conditionally for C++20 for now).
For a configure-wise bare-bones build on Linux, size reported by `du -bs
instdir` grew by 118792 bytes from 1155636636 to 1155755428.
In most places just a u"..." string literal prefix had to be added. In some
places
char const a[] = "...";
variables have been changed to char16_t, and a few places required even further
changes to code (which prompted the addition of include/o3tl/string_view.hxx
helper function o3tl::equalsIgnoreAsciiCase and the additional
OUString::createFromAscii overload).
For all uses of macros expanding to string literals, the relevant uses have been
rewritten as
u"" MACRO
instead of changing the macro definitions. It should be possible to change at
least some of those macro definitions (and drop the u"" from their call sites)
in follow-up commits.
Change-Id: Iec4ef1a057d412d22443312d40c6a8a290dc6144
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/101483
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'include/rtl')
-rw-r--r-- | include/rtl/stringutils.hxx | 2 | ||||
-rw-r--r-- | include/rtl/ustrbuf.hxx | 20 | ||||
-rw-r--r-- | include/rtl/ustring.hxx | 158 |
3 files changed, 101 insertions, 79 deletions
diff --git a/include/rtl/stringutils.hxx b/include/rtl/stringutils.hxx index 40b03c84f4bb..29a7904e6019 100644 --- a/include/rtl/stringutils.hxx +++ b/include/rtl/stringutils.hxx @@ -255,6 +255,8 @@ struct ConstCharArrayDetector<sal_Unicode const [N], T> { using TypeUtf16 = T; static constexpr bool const ok = true; static constexpr std::size_t const length = N - 1; + static constexpr bool isValid(sal_Unicode const (& literal)[N]) + { return literal[N - 1] == '\0'; } static constexpr sal_Unicode const * toPointer( sal_Unicode const (& literal)[N]) { return literal; } diff --git a/include/rtl/ustrbuf.hxx b/include/rtl/ustrbuf.hxx index 2d957d81064e..9e8a586279b0 100644 --- a/include/rtl/ustrbuf.hxx +++ b/include/rtl/ustrbuf.hxx @@ -177,7 +177,7 @@ public: OUStringBuffer(OUStringLiteral const & literal): pData(nullptr), nCapacity(literal.size + 16) //TODO: check for overflow { - rtl_uString_newFromLiteral(&pData, literal.data, literal.size, 16); + rtl_uStringbuffer_newFromStr_WithLength(&pData, literal.data, literal.size); } #endif @@ -320,11 +320,9 @@ public: if (n >= nCapacity) { ensureCapacity(n + 16); //TODO: check for overflow } - char const * from = literal.data; - sal_Unicode * to = pData->buffer; - for (sal_Int32 i = 0; i <= n; ++i) { - to[i] = from[i]; - } + std::memcpy( + pData->buffer, literal.data, + (n + 1) * sizeof (sal_Unicode)); //TODO: check for overflow pData->length = n; return *this; } @@ -662,7 +660,7 @@ public: /** @overload @since LibreOffice 5.4 */ OUStringBuffer & append(OUStringLiteral const & literal) { - return appendAscii(literal.data, literal.size); + return append(literal.data, literal.size); } #endif @@ -1040,9 +1038,7 @@ public: /** @overload @since LibreOffice 5.4 */ OUStringBuffer & insert(sal_Int32 offset, OUStringLiteral const & literal) { - rtl_uStringbuffer_insert_ascii( - &pData, &nCapacity, offset, literal.data, literal.size); - return *this; + return insert(offset, literal.data, literal.size); } #endif @@ -1453,7 +1449,7 @@ public: sal_Int32 indexOf(OUStringLiteral const & literal, sal_Int32 fromIndex = 0) const { - sal_Int32 n = rtl_ustr_indexOfAscii_WithLength( + sal_Int32 n = rtl_ustr_indexOfStr_WithLength( pData->buffer + fromIndex, pData->length - fromIndex, literal.data, literal.size); return n < 0 ? n : n + fromIndex; @@ -1539,7 +1535,7 @@ public: /** @overload @since LibreOffice 5.4 */ sal_Int32 lastIndexOf(OUStringLiteral const & literal) const { - return rtl_ustr_lastIndexOfAscii_WithLength( + return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, pData->length, literal.data, literal.size); } #endif diff --git a/include/rtl/ustring.hxx b/include/rtl/ustring.hxx index d73977192cd0..2f9518029be1 100644 --- a/include/rtl/ustring.hxx +++ b/include/rtl/ustring.hxx @@ -78,7 +78,7 @@ struct SAL_WARN_UNUSED OUStringLiteral template<typename T> constexpr OUStringLiteral( T & literal, typename libreoffice_internal::ConstCharArrayDetector< - T, libreoffice_internal::Dummy>::Type + T, libreoffice_internal::Dummy>::TypeUtf16 = libreoffice_internal::Dummy()): size(libreoffice_internal::ConstCharArrayDetector<T>::length), data( @@ -88,8 +88,10 @@ struct SAL_WARN_UNUSED OUStringLiteral libreoffice_internal::ConstCharArrayDetector<T>::isValid(literal)); } + constexpr operator std::u16string_view() const { return {data, unsigned(size)}; } + int size; - const char* data; + const sal_Unicode* data; // So we can use this struct in some places interchangeably with OUString constexpr sal_Int32 getLength() const { return size; } @@ -330,12 +332,12 @@ public: written as - OUString(flag ? OUStringLiteral("a") : OUStringLiteral("bb")) + OUString(flag ? OUStringLiteral(u"a") : OUStringLiteral(u"bb")) @since LibreOffice 5.0 */ OUString(OUStringLiteral literal): pData(NULL) { - rtl_uString_newFromLiteral(&pData, literal.data, literal.size, 0); + rtl_uString_newFromStr_WithLength(&pData, literal.data, literal.size); } /// @endcond #endif @@ -532,7 +534,7 @@ public: if (literal.size == 0) { rtl_uString_new(&pData); } else { - rtl_uString_newFromLiteral(&pData, literal.data, literal.size, 0); + rtl_uString_newFromStr_WithLength(&pData, literal.data, literal.size); } return *this; } @@ -614,7 +616,7 @@ public: /** @overload @since LibreOffice 5.4 */ OUString & operator +=(OUStringLiteral const & literal) & { - rtl_uString_newConcatAsciiL(&pData, pData, literal.data, literal.size); + rtl_uString_newConcatUtf16L(&pData, pData, literal.data, literal.size); return *this; } void operator +=(OUStringLiteral const &) && = delete; @@ -803,7 +805,7 @@ public: /** @overload @since LibreOffice 5.4 */ sal_Int32 reverseCompareTo(OUStringLiteral const & literal) const { - return rtl_ustr_asciil_reverseCompare_WithLength( + return rtl_ustr_reverseCompare_WithLength( pData->buffer, pData->length, literal.data, literal.size); } #endif @@ -911,10 +913,10 @@ public: /** @overload @since LibreOffice 5.4 */ bool equalsIgnoreAsciiCase(OUStringLiteral const & literal) const { - return pData->length == literal.size - && (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength( - pData->buffer, pData->length, literal.data) - == 0); + return + rtl_ustr_compareIgnoreAsciiCase_WithLength( + pData->buffer, pData->length, literal.data, literal.size) + == 0; } #endif @@ -977,9 +979,9 @@ public: /** @overload @since LibreOffice 5.4 */ bool match(OUStringLiteral const & literal, sal_Int32 fromIndex = 0) const { return - rtl_ustr_ascii_shortenedCompare_WithLength( + rtl_ustr_shortenedCompare_WithLength( pData->buffer + fromIndex, pData->length - fromIndex, - literal.data, literal.size) + literal.data, literal.size, literal.size) == 0; } #endif @@ -1049,9 +1051,9 @@ public: OUStringLiteral const & literal, sal_Int32 fromIndex = 0) const { return - rtl_ustr_ascii_shortenedCompareIgnoreAsciiCase_WithLength( + rtl_ustr_shortenedCompareIgnoreAsciiCase_WithLength( pData->buffer+fromIndex, pData->length-fromIndex, literal.data, - literal.size) + literal.size, literal.size) == 0; } #endif @@ -1402,8 +1404,9 @@ public: const { bool b = literal.size <= pData->length - && rtl_ustr_asciil_reverseEquals_WithLength( - pData->buffer, literal.data, literal.size); + && (rtl_ustr_reverseCompare_WithLength( + pData->buffer, literal.size, literal.data, literal.size) + == 0); if (b && rest != nullptr) { *rest = copy(literal.size); } @@ -1494,7 +1497,7 @@ public: OUStringLiteral const & literal, OUString * rest = nullptr) const { bool b - = (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths( + = (rtl_ustr_compareIgnoreAsciiCase_WithLength( pData->buffer, literal.size, literal.data, literal.size) == 0); if (b && rest != nullptr) { @@ -1586,9 +1589,10 @@ public: const { bool b = literal.size <= pData->length - && rtl_ustr_asciil_reverseEquals_WithLength( - pData->buffer + pData->length - literal.size, - literal.data, literal.size); + && (rtl_ustr_reverseCompare_WithLength( + pData->buffer + pData->length - literal.size, literal.size, + literal.data, literal.size) + == 0); if (b && rest != nullptr) { *rest = copy(0, (getLength() - literal.size)); } @@ -1707,7 +1711,7 @@ public: OUStringLiteral const & literal, OUString * rest = nullptr) const { bool b = literal.size <= pData->length - && (rtl_ustr_ascii_compareIgnoreAsciiCase_WithLengths( + && (rtl_ustr_compareIgnoreAsciiCase_WithLength( pData->buffer + pData->length - literal.size, literal.size, literal.data, literal.size) == 0); @@ -1882,75 +1886,87 @@ public: */ friend bool operator ==(OUString const & lhs, OUStringLiteral const & rhs) { - return lhs.equalsAsciiL(rhs.data, rhs.size); + return + rtl_ustr_reverseCompare_WithLength( + lhs.pData->buffer, lhs.pData->length, rhs.data, rhs.size) + == 0; } friend bool operator !=(OUString const & lhs, OUStringLiteral const & rhs) { - return !lhs.equalsAsciiL(rhs.data, rhs.size); + return + rtl_ustr_reverseCompare_WithLength( + lhs.pData->buffer, lhs.pData->length, rhs.data, rhs.size) + != 0; } friend bool operator <(OUString const & lhs, OUStringLiteral const & rhs) { return - (rtl_ustr_ascii_compare_WithLength( - lhs.pData->buffer, lhs.pData->length, rhs.data)) + (rtl_ustr_compare_WithLength( + lhs.pData->buffer, lhs.pData->length, rhs.data, rhs.size)) < 0; } friend bool operator <=(OUString const & lhs, OUStringLiteral const & rhs) { return - (rtl_ustr_ascii_compare_WithLength( - lhs.pData->buffer, lhs.pData->length, rhs.data)) + (rtl_ustr_compare_WithLength( + lhs.pData->buffer, lhs.pData->length, rhs.data, rhs.size)) <= 0; } friend bool operator >(OUString const & lhs, OUStringLiteral const & rhs) { return - (rtl_ustr_ascii_compare_WithLength( - lhs.pData->buffer, lhs.pData->length, rhs.data)) + (rtl_ustr_compare_WithLength( + lhs.pData->buffer, lhs.pData->length, rhs.data, rhs.size)) > 0; } friend bool operator >=(OUString const & lhs, OUStringLiteral const & rhs) { return - (rtl_ustr_ascii_compare_WithLength( - lhs.pData->buffer, lhs.pData->length, rhs.data)) + (rtl_ustr_compare_WithLength( + lhs.pData->buffer, lhs.pData->length, rhs.data, rhs.size)) >= 0; } friend bool operator ==(OUStringLiteral const & lhs, OUString const & rhs) { - return rhs.equalsAsciiL(lhs.data, lhs.size); + return + rtl_ustr_reverseCompare_WithLength( + lhs.data, lhs.size, rhs.pData->buffer, rhs.pData->length) + == 0; } friend bool operator !=(OUStringLiteral const & lhs, OUString const & rhs) { - return !rhs.equalsAsciiL(lhs.data, lhs.size); + return + rtl_ustr_reverseCompare_WithLength( + lhs.data, lhs.size, rhs.pData->buffer, rhs.pData->length) + != 0; } friend bool operator <(OUStringLiteral const & lhs, OUString const & rhs) { return - (rtl_ustr_ascii_compare_WithLength( - rhs.pData->buffer, rhs.pData->length, lhs.data)) - >= 0; + (rtl_ustr_compare_WithLength( + lhs.data, lhs.size, rhs.pData->buffer, rhs.pData->length)) + < 0; } friend bool operator <=(OUStringLiteral const & lhs, OUString const & rhs) { return - (rtl_ustr_ascii_compare_WithLength( - rhs.pData->buffer, rhs.pData->length, lhs.data)) - > 0; + (rtl_ustr_compare_WithLength( + lhs.data, lhs.size, rhs.pData->buffer, rhs.pData->length)) + <= 0; } friend bool operator >(OUStringLiteral const & lhs, OUString const & rhs) { return - (rtl_ustr_ascii_compare_WithLength( - rhs.pData->buffer, rhs.pData->length, lhs.data)) - <= 0; + (rtl_ustr_compare_WithLength( + lhs.data, lhs.size, rhs.pData->buffer, rhs.pData->length)) + > 0; } friend bool operator >=(OUStringLiteral const & lhs, OUString const & rhs) { return - (rtl_ustr_ascii_compare_WithLength( - rhs.pData->buffer, rhs.pData->length, lhs.data)) - < 0; + (rtl_ustr_compare_WithLength( + lhs.data, lhs.size, rhs.pData->buffer, rhs.pData->length)) + >= 0; } /// @endcond @@ -2075,7 +2091,7 @@ public: sal_Int32 indexOf(OUStringLiteral const & literal, sal_Int32 fromIndex = 0) const { - sal_Int32 n = rtl_ustr_indexOfAscii_WithLength( + sal_Int32 n = rtl_ustr_indexOfStr_WithLength( pData->buffer + fromIndex, pData->length - fromIndex, literal.data, literal.size); return n < 0 ? n : n + fromIndex; @@ -2195,7 +2211,7 @@ public: /** @overload @since LibreOffice 5.4 */ sal_Int32 lastIndexOf(OUStringLiteral const & literal) const { - return rtl_ustr_lastIndexOfAscii_WithLength( + return rtl_ustr_lastIndexOfStr_WithLength( pData->buffer, pData->length, literal.data, literal.size); } #endif @@ -2572,8 +2588,8 @@ public: { rtl_uString * s = nullptr; sal_Int32 i = 0; - rtl_uString_newReplaceFirstAsciiL( - &s, pData, from.data, from.size, to.pData, + rtl_uString_newReplaceFirstUtf16LUtf16L( + &s, pData, from.data, from.size, to.pData->buffer, to.pData->length, index == nullptr ? &i : index); return OUString(s, SAL_NO_ACQUIRE); } @@ -2584,8 +2600,8 @@ public: { rtl_uString * s = nullptr; sal_Int32 i = 0; - rtl_uString_newReplaceFirstToAsciiL( - &s, pData, from.pData, to.data, to.size, + rtl_uString_newReplaceFirstUtf16LUtf16L( + &s, pData, from.pData->buffer, from.pData->length, to.data, to.size, index == nullptr ? &i : index); return OUString(s, SAL_NO_ACQUIRE); } @@ -2596,7 +2612,7 @@ public: { rtl_uString * s = nullptr; sal_Int32 i = 0; - rtl_uString_newReplaceFirstAsciiLAsciiL( + rtl_uString_newReplaceFirstUtf16LUtf16L( &s, pData, from.data, from.size, to.data, to.size, index == nullptr ? &i : index); return OUString(s, SAL_NO_ACQUIRE); @@ -2610,7 +2626,7 @@ public: assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(to)); rtl_uString * s = nullptr; sal_Int32 i = 0; - rtl_uString_newReplaceFirstAsciiLAsciiL( + rtl_uString_newReplaceFirstUtf16LAsciiL( &s, pData, from.data, from.size, libreoffice_internal::ConstCharArrayDetector<T>::toPointer(to), libreoffice_internal::ConstCharArrayDetector<T>::length, @@ -2626,7 +2642,7 @@ public: assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(from)); rtl_uString * s = nullptr; sal_Int32 i = 0; - rtl_uString_newReplaceFirstAsciiLAsciiL( + rtl_uString_newReplaceFirstAsciiLUtf16L( &s, pData, libreoffice_internal::ConstCharArrayDetector<T>::toPointer(from), libreoffice_internal::ConstCharArrayDetector<T>::length, to.data, @@ -2643,7 +2659,7 @@ public: assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(to)); rtl_uString * s = nullptr; sal_Int32 i = 0; - rtl_uString_newReplaceFirstAsciiLUtf16L( + rtl_uString_newReplaceFirstUtf16LUtf16L( &s, pData, from.data, from.size, libreoffice_internal::ConstCharArrayDetector<T>::toPointer(to), libreoffice_internal::ConstCharArrayDetector<T>::length, @@ -2660,7 +2676,7 @@ public: assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(from)); rtl_uString * s = nullptr; sal_Int32 i = 0; - rtl_uString_newReplaceFirstUtf16LAsciiL( + rtl_uString_newReplaceFirstUtf16LUtf16L( &s, pData, libreoffice_internal::ConstCharArrayDetector<T>::toPointer(from), libreoffice_internal::ConstCharArrayDetector<T>::length, to.data, @@ -2877,8 +2893,8 @@ public: OUStringLiteral const & from, OUString const & to) const { rtl_uString * s = nullptr; - rtl_uString_newReplaceAllAsciiL( - &s, pData, from.data, from.size, to.pData); + rtl_uString_newReplaceAllUtf16LUtf16L( + &s, pData, from.data, from.size, to.pData->buffer, to.pData->length); return OUString(s, SAL_NO_ACQUIRE); } /** @overload @since LibreOffice 5.4 */ @@ -2886,8 +2902,8 @@ public: OUString const & from, OUStringLiteral const & to) const { rtl_uString * s = nullptr; - rtl_uString_newReplaceAllToAsciiL( - &s, pData, from.pData, to.data, to.size); + rtl_uString_newReplaceAllUtf16LUtf16L( + &s, pData, from.pData->buffer, from.pData->length, to.data, to.size); return OUString(s, SAL_NO_ACQUIRE); } /** @overload @since LibreOffice 5.4 */ @@ -2895,7 +2911,7 @@ public: OUStringLiteral const & from, OUStringLiteral const & to) const { rtl_uString * s = nullptr; - rtl_uString_newReplaceAllAsciiLAsciiL( + rtl_uString_newReplaceAllUtf16LUtf16L( &s, pData, from.data, from.size, to.data, to.size); return OUString(s, SAL_NO_ACQUIRE); } @@ -2905,7 +2921,7 @@ public: replaceAll(OUStringLiteral const & from, T & to) const { assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(to)); rtl_uString * s = nullptr; - rtl_uString_newReplaceAllAsciiLAsciiL( + rtl_uString_newReplaceAllUtf16LAsciiL( &s, pData, from.data, from.size, libreoffice_internal::ConstCharArrayDetector<T>::toPointer(to), libreoffice_internal::ConstCharArrayDetector<T>::length); @@ -2917,7 +2933,7 @@ public: replaceAll(T & from, OUStringLiteral const & to) const { assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(from)); rtl_uString * s = nullptr; - rtl_uString_newReplaceAllAsciiLAsciiL( + rtl_uString_newReplaceAllAsciiLUtf16L( &s, pData, libreoffice_internal::ConstCharArrayDetector<T>::toPointer(from), libreoffice_internal::ConstCharArrayDetector<T>::length, to.data, @@ -2931,7 +2947,7 @@ public: replaceAll(OUStringLiteral const & from, T & to) const { assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(to)); rtl_uString * s = nullptr; - rtl_uString_newReplaceAllAsciiLUtf16L( + rtl_uString_newReplaceAllUtf16LUtf16L( &s, pData, from.data, from.size, libreoffice_internal::ConstCharArrayDetector<T>::toPointer(to), libreoffice_internal::ConstCharArrayDetector<T>::length); @@ -2944,7 +2960,7 @@ public: replaceAll(T & from, OUStringLiteral const & to) const { assert(libreoffice_internal::ConstCharArrayDetector<T>::isValid(from)); rtl_uString * s = nullptr; - rtl_uString_newReplaceAllUtf16LAsciiL( + rtl_uString_newReplaceAllUtf16LUtf16L( &s, pData, libreoffice_internal::ConstCharArrayDetector<T>::toPointer(from), libreoffice_internal::ConstCharArrayDetector<T>::length, to.data, @@ -3609,6 +3625,14 @@ public: } #if defined LIBO_INTERNAL_ONLY + static OUString createFromAscii(std::string_view value) { + rtl_uString * p = nullptr; + rtl_uString_newFromLiteral(&p, value.data(), value.size(), 0); //TODO: check for overflow + return OUString(p, SAL_NO_ACQUIRE); + } + #endif + +#if defined LIBO_INTERNAL_ONLY operator std::u16string_view() const { return {getStr(), sal_uInt32(getLength())}; } #endif @@ -3661,7 +3685,7 @@ template<> struct ToStringHelper< OUStringLiteral > { static std::size_t length( const OUStringLiteral& str ) { return str.size; } - static sal_Unicode* addData( sal_Unicode* buffer, const OUStringLiteral& str ) { return addDataLiteral( buffer, str.data, str.size ); } + static sal_Unicode* addData( sal_Unicode* buffer, const OUStringLiteral& str ) { return addDataHelper( buffer, str.data, str.size ); } static const bool allowOStringConcat = false; static const bool allowOUStringConcat = true; }; |