From 2c10714426cc813c36aa82e4870b7b51c5c03050 Mon Sep 17 00:00:00 2001 From: Stephan Bergmann Date: Sun, 28 Aug 2016 09:47:27 +0200 Subject: Make OUStringLiteral1 a wrapper around UTF-16 instead of just ASCII ...not merely an ASCII character Change-Id: Id2b381b35fe3a15574728ed973d60263dfef7249 Reviewed-on: https://gerrit.libreoffice.org/28446 Tested-by: Jenkins Reviewed-by: Stephan Bergmann --- chart2/inc/SpecialUnicodes.hxx | 2 +- chart2/qa/extras/chart2_trendcalculators.cxx | 8 ++--- .../tools/ExponentialRegressionCurveCalculator.cxx | 4 +-- .../tools/LogarithmicRegressionCurveCalculator.cxx | 4 +-- .../tools/PolynomialRegressionCurveCalculator.cxx | 2 +- .../tools/PotentialRegressionCurveCalculator.cxx | 2 +- compilerplugins/clang/stringstatic.cxx | 7 ---- include/rtl/stringconcat.hxx | 6 ++-- include/rtl/stringutils.hxx | 41 ++++++++++------------ .../rtl/strings/test_oustring_stringliterals.cxx | 10 +++--- 10 files changed, 38 insertions(+), 48 deletions(-) diff --git a/chart2/inc/SpecialUnicodes.hxx b/chart2/inc/SpecialUnicodes.hxx index db6e3b65fdb5..22ddc35b5e39 100644 --- a/chart2/inc/SpecialUnicodes.hxx +++ b/chart2/inc/SpecialUnicodes.hxx @@ -10,7 +10,7 @@ #ifndef INCLUDED_CHART2_INC_SPECIALUNICODES_HXX #define INCLUDED_CHART2_INC_SPECIALUNICODES_HXX -const OUString aMinusSign ( sal_Unicode (0x2212) ); +const sal_Unicode aMinusSign = 0x2212; const OUString aNewLine ("\n"); const OUString aHashString ("###"); const sal_Unicode aSuperscriptFigures[10]={ 0x2070, 0x00B9, 0x00B2, 0x00B3, 0x2074, 0x2075, 0x2076, 0x2077, 0x2078, 0x2079 }; diff --git a/chart2/qa/extras/chart2_trendcalculators.cxx b/chart2/qa/extras/chart2_trendcalculators.cxx index 738a6d6bfca4..dd79dadab345 100644 --- a/chart2/qa/extras/chart2_trendcalculators.cxx +++ b/chart2/qa/extras/chart2_trendcalculators.cxx @@ -136,7 +136,7 @@ void Chart2TrendCalculators::testPotentialRegression2() xValues[i] = d; yValues[i] = -2.0 * pow ( d, 3 ); } - checkCalculator( xValues, yValues, "f(x) = "+ OUString(aMinusSign) +" 2 x^3"); + checkCalculator( xValues, yValues, "f(x) = "+ OUStringLiteral1() +" 2 x^3"); } // test y = - 2 X - 5 @@ -152,7 +152,7 @@ void Chart2TrendCalculators::testLinearRegression1() xValues[i] = d; yValues[i] = - 2.0 * d - 5.0 ; } - checkCalculator( xValues, yValues, "f(x) = "+ OUString(aMinusSign) +" 2x "+ OUString(aMinusSign) +" 5"); + checkCalculator( xValues, yValues, "f(x) = "+ OUStringLiteral1() +" 2x "+ OUStringLiteral1() +" 5"); } // test y = A x ^ B @@ -168,7 +168,7 @@ void Chart2TrendCalculators::testPolynomialRegression1() xValues[i] = d; yValues[i] = - 2.0 * d * d + 4 * d - 5; } - OUString sExpectedFormula( "f(x) = "+ OUString(aMinusSign) +" 2x" + OUString( aSuperscriptFigures[2] ) + " + 4x "+ OUString(aMinusSign) +" 5" ); + OUString sExpectedFormula( "f(x) = "+ OUStringLiteral1() +" 2x" + OUString( aSuperscriptFigures[2] ) + " + 4x "+ OUStringLiteral1() +" 5" ); checkCalculator( xValues, yValues, sExpectedFormula ); } @@ -199,7 +199,7 @@ void Chart2TrendCalculators::testExponentialRegression2() xValues[i] = d; yValues[i] = -2.0 * exp ( 0.3 * d ); } - checkCalculator( xValues, yValues, "f(x) = "+ OUString(aMinusSign) + " 2 exp( 0.3 x )"); + checkCalculator( xValues, yValues, "f(x) = "+ OUStringLiteral1() + " 2 exp( 0.3 x )"); } diff --git a/chart2/source/tools/ExponentialRegressionCurveCalculator.cxx b/chart2/source/tools/ExponentialRegressionCurveCalculator.cxx index 9d3f105d9b54..35c85e373ac6 100644 --- a/chart2/source/tools/ExponentialRegressionCurveCalculator.cxx +++ b/chart2/source/tools/ExponentialRegressionCurveCalculator.cxx @@ -185,7 +185,7 @@ OUString ExponentialRegressionCurveCalculator::ImplGetRepresentation( // if nValueLength not calculated then nullptr sal_Int32* pValueLength = nValueLength ? &nValueLength : nullptr; if ( m_fSign < 0.0 ) - aTmpBuf.append( aMinusSign + " " ); + aTmpBuf.append( OUStringLiteral1() + " " ); if ( bHasIntercept ) { OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, fIntercept, pValueLength ); @@ -210,7 +210,7 @@ OUString ExponentialRegressionCurveCalculator::ImplGetRepresentation( } } if ( m_fLogSlope < 0.0 ) - aTmpBuf.append( aMinusSign + " " ); + aTmpBuf.append( OUStringLiteral1() + " " ); if ( bHasLogSlope ) { OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, fabs(m_fLogSlope), pValueLength ); diff --git a/chart2/source/tools/LogarithmicRegressionCurveCalculator.cxx b/chart2/source/tools/LogarithmicRegressionCurveCalculator.cxx index b3d5e8f018af..93cbf3a70263 100644 --- a/chart2/source/tools/LogarithmicRegressionCurveCalculator.cxx +++ b/chart2/source/tools/LogarithmicRegressionCurveCalculator.cxx @@ -162,7 +162,7 @@ OUString LogarithmicRegressionCurveCalculator::ImplGetRepresentation( { if( m_fSlope < 0.0 ) { - aTmpBuf.append( aMinusSign + " " ); + aTmpBuf.append( OUStringLiteral1() + " " ); } if( bHasSlope ) { @@ -181,7 +181,7 @@ OUString LogarithmicRegressionCurveCalculator::ImplGetRepresentation( } // add intercept value if( m_fIntercept < 0.0 ) - aTmpBuf.append( aMinusSign+" " ); + aTmpBuf.append( OUStringLiteral1()+" " ); OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, fabs(m_fIntercept), pValueLength ); if ( aValueString != "0" ) // aValueString may be rounded to 0 if nValueLength is small { diff --git a/chart2/source/tools/PolynomialRegressionCurveCalculator.cxx b/chart2/source/tools/PolynomialRegressionCurveCalculator.cxx index c4976a54efa7..120c33e86386 100644 --- a/chart2/source/tools/PolynomialRegressionCurveCalculator.cxx +++ b/chart2/source/tools/PolynomialRegressionCurveCalculator.cxx @@ -278,7 +278,7 @@ OUString PolynomialRegressionCurveCalculator::ImplGetRepresentation( { if ( bFindValue ) // if it is not the first aValue aTmpBuf.append( " " ); - aTmpBuf.append( aMinusSign + " "); + aTmpBuf.append( OUStringLiteral1() + " "); aValue = - aValue; } else diff --git a/chart2/source/tools/PotentialRegressionCurveCalculator.cxx b/chart2/source/tools/PotentialRegressionCurveCalculator.cxx index aea4e68ad0ac..b767412bc6b5 100644 --- a/chart2/source/tools/PotentialRegressionCurveCalculator.cxx +++ b/chart2/source/tools/PotentialRegressionCurveCalculator.cxx @@ -176,7 +176,7 @@ OUString PotentialRegressionCurveCalculator::ImplGetRepresentation( // if nValueLength not calculated then nullptr sal_Int32* pValueLength = nValueLength ? &nValueLength : nullptr; if ( m_fIntercept < 0.0 ) // add intercept value - aTmpBuf.append( aMinusSign+" " ); + aTmpBuf.append( OUStringLiteral1()+" " ); if( bHasIntercept ) { OUString aValueString = getFormattedString( xNumFormatter, nNumberFormatKey, fabs(m_fIntercept), pValueLength ); diff --git a/compilerplugins/clang/stringstatic.cxx b/compilerplugins/clang/stringstatic.cxx index 4ddac65d7e07..823fc7bab4b3 100644 --- a/compilerplugins/clang/stringstatic.cxx +++ b/compilerplugins/clang/stringstatic.cxx @@ -66,13 +66,6 @@ bool StringStatic::VisitVarDecl(VarDecl const* varDecl) if (ignoreLocation(varDecl)) { return true; } - // We could use OUStringLiteral1 here, but we'd need to update OUStringLiteral1 to allow BMP chars first: - if (compiler.getSourceManager().getFilename(varDecl->getLocation()) - == SRCDIR "/chart2/inc/SpecialUnicodes.hxx") - { - return true; - } - QualType qt = varDecl->getType(); if (!varDecl->hasGlobalStorage() || !varDecl->isThisDeclarationADefinition() diff --git a/include/rtl/stringconcat.hxx b/include/rtl/stringconcat.hxx index 5a32bf930019..ff8ad95f93c2 100644 --- a/include/rtl/stringconcat.hxx +++ b/include/rtl/stringconcat.hxx @@ -150,13 +150,11 @@ template struct ToStringHelper { static bool const allowOUStringConcat = true; }; -template struct ToStringHelper> { +template struct ToStringHelper> { static int length(OUStringLiteral1_) { return 1; } - static char * addData(char * buffer, OUStringLiteral1_ literal) - { return addDataHelper(buffer, &literal.c, 1); } static sal_Unicode * addData( sal_Unicode * buffer, OUStringLiteral1_ literal) - { return addDataLiteral(buffer, &literal.c, 1); } + { return addDataHelper(buffer, &literal.c, 1); } static bool const allowOStringConcat = false; static bool const allowOUStringConcat = true; }; diff --git a/include/rtl/stringutils.hxx b/include/rtl/stringutils.hxx index 1e5761f0fab4..6965347788a0 100644 --- a/include/rtl/stringutils.hxx +++ b/include/rtl/stringutils.hxx @@ -36,13 +36,13 @@ namespace rtl #if defined LIBO_INTERNAL_ONLY /// @cond INTERNAL -/** A simple wrapper around an ASCII character literal. +/** A simple wrapper around a sal_Unicode character literal. - Can be useful to pass a char constant with ASCII value into a - OUString-related function that is optimized for ASCII string literal - arguments. That is, instead of + Can be useful to pass a sal_Unicode constant into an OUString-related + function that is optimized for UTF-16 string literal arguments. That is, + instead of - char const WILDCARD = '%'; + sal_Unicode const WILDCARD = '%'; ... if (s[i] == WILDCARD) ... ... @@ -50,7 +50,7 @@ namespace rtl use - char const WILDCARD = '%'; + sal_Unicode const WILDCARD = '%'; ... if (s[i] == WILDCARD) ... ... @@ -58,7 +58,7 @@ namespace rtl to avoid creating a temporary OUString instance, and instead pick the endsWith overload actually designed to take an argument of type - char const[N]. + sal_Unicode const[N]. Instances of OUStringLiteral1 need to be const, as those literal-optimized functions take the literal argument by non-const lvalue reference, for @@ -71,18 +71,15 @@ namespace rtl @since LibreOffice 5.0 */ -template struct SAL_WARN_UNUSED OUStringLiteral1_ { - static_assert( - static_cast(C) < 0x80, - "non-ASCII character in OUStringLiteral1"); - char const c = C; +template struct SAL_WARN_UNUSED OUStringLiteral1_ { + sal_Unicode const c = C; }; #if defined _MSC_VER && _MSC_VER <= 1900 && !defined __clang__ // Visual Studio 2015 -template using OUStringLiteral1 = OUStringLiteral1_; +template using OUStringLiteral1 = OUStringLiteral1_; #pragma warning(disable: 4239) #else -template using OUStringLiteral1 = OUStringLiteral1_ const; +template using OUStringLiteral1 = OUStringLiteral1_ const; #endif /// @endcond @@ -181,7 +178,7 @@ struct ConstCharArrayDetector { sal_Unicode const (& literal)[N]) { return literal; } }; -template struct ConstCharArrayDetector< +template struct ConstCharArrayDetector< #if defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ <= 8 \ && !defined __clang__ OUStringLiteral1_ const, @@ -190,11 +187,11 @@ template struct ConstCharArrayDetector< #endif T> { - typedef T Type; - static const std::size_t length = 1; - static const bool ok = true; - static bool isValid(OUStringLiteral1_) { return true; } - static char const * toPointer(OUStringLiteral1_ const & literal) + using TypeUtf16 = T; + static SAL_CONSTEXPR bool const ok = true; + static SAL_CONSTEXPR std::size_t const length = 1; + static SAL_CONSTEXPR sal_Unicode const * toPointer( + OUStringLiteral1_ const & literal) { return &literal.c; } }; #endif @@ -212,7 +209,7 @@ struct ExceptConstCharArrayDetector< const char[ N ] > #if defined LIBO_INTERNAL_ONLY template struct ExceptConstCharArrayDetector {}; -template struct ExceptConstCharArrayDetector< +template struct ExceptConstCharArrayDetector< #if defined __GNUC__ && __GNUC__ == 4 && __GNUC_MINOR__ <= 8 \ && !defined __clang__ OUStringLiteral1_ const @@ -243,7 +240,7 @@ struct ExceptCharArrayDetector< const char[ N ] > #if defined LIBO_INTERNAL_ONLY template struct ExceptCharArrayDetector {}; template struct ExceptCharArrayDetector {}; -template struct ExceptCharArrayDetector> {}; +template struct ExceptCharArrayDetector> {}; #endif template< typename T1, typename T2 = void > diff --git a/sal/qa/rtl/strings/test_oustring_stringliterals.cxx b/sal/qa/rtl/strings/test_oustring_stringliterals.cxx index aa496a4779f1..ec4bbe244862 100644 --- a/sal/qa/rtl/strings/test_oustring_stringliterals.cxx +++ b/sal/qa/rtl/strings/test_oustring_stringliterals.cxx @@ -218,18 +218,20 @@ void test::oustring::StringLiterals::checkOUStringLiteral() void test::oustring::StringLiterals::checkOUStringLiteral1() { auto l1 = rtlunittest::OUStringLiteral1<'A'>(); - CPPUNIT_ASSERT_EQUAL('A', l1.c); + CPPUNIT_ASSERT_EQUAL(sal_Unicode('A'), l1.c); char const c2 = 'A'; auto l2 = rtlunittest::OUStringLiteral1(); - CPPUNIT_ASSERT_EQUAL('A', l2.c); + CPPUNIT_ASSERT_EQUAL(sal_Unicode('A'), l2.c); // char c3 = 'A'; auto l3 = rtlunittest::OUStringLiteral1(); auto l4 = rtlunittest::OUStringLiteral1(); - CPPUNIT_ASSERT_EQUAL('A', l4.c); + CPPUNIT_ASSERT_EQUAL(sal_Unicode('A'), l4.c); - // auto l5 = rtlunittest::OUStringLiteral1(); + sal_Unicode const c5 = 0x100; + auto l5 = rtlunittest::OUStringLiteral1(); + CPPUNIT_ASSERT_EQUAL(c5, l5.c); rtl::OUString s1{rtlunittest::OUStringLiteral1<'A'>()}; CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s1.getLength()); -- cgit