[PATCH] Create OUString::valueInt, valueBool and valueChar function ...

Noel Grandin (via Code Review) gerrit at gerrit.libreoffice.org
Wed Jan 9 23:46:07 PST 2013


Hi,

I have submitted a patch for review:

    https://gerrit.libreoffice.org/1625

To pull it, you can do:

    git pull ssh://gerrit.libreoffice.org:29418/core refs/changes/25/1625/1

Create OUString::valueInt, valueBool and valueChar function templates.

Cleans up the call sites by hiding the necessary casts.

Change-Id: Id3d150a6525eb0334e41e2ec6640bb06cd790b43
---
M sal/CppunitTest_sal_rtl_strings.mk
M sal/inc/rtl/ustring.hxx
A sal/qa/rtl/strings/test_oustring_valuex.cxx
3 files changed, 98 insertions(+), 0 deletions(-)



diff --git a/sal/CppunitTest_sal_rtl_strings.mk b/sal/CppunitTest_sal_rtl_strings.mk
index a30bf70..9ca6e7b 100644
--- a/sal/CppunitTest_sal_rtl_strings.mk
+++ b/sal/CppunitTest_sal_rtl_strings.mk
@@ -38,6 +38,7 @@
     sal/qa/rtl/strings/test_oustring_noadditional \
     sal/qa/rtl/strings/test_oustring_startswith \
     sal/qa/rtl/strings/test_oustring_stringliterals \
+    sal/qa/rtl/strings/test_oustring_valuex \
 ))
 
 $(eval $(call gb_CppunitTest_use_libraries,sal_rtl_strings,\
diff --git a/sal/inc/rtl/ustring.hxx b/sal/inc/rtl/ustring.hxx
index c9fe25e..2a598d3 100644
--- a/sal/inc/rtl/ustring.hxx
+++ b/sal/inc/rtl/ustring.hxx
@@ -2088,6 +2088,36 @@
     }
 
     /**
+      Returns the string representation of the long argument.
+      This is here because when choosing
+      which conversion for overloaded functions is better, the standard treats all
+      integer conversions the same.
+
+      This function can't be used for language specific conversion.
+
+      @param    ll          a int64.
+      @param    radix       the radix (between 2 and 36)
+      @return   a string with the string representation of the argument.
+    */
+    template< typename T >
+    static OUString valueInt(T i, sal_Int16 radix = 10) SAL_THROW(())
+    {
+        return valueOf( static_cast<sal_Int32>(i), radix );
+    }
+
+    template< typename T >
+    static OUString valueBool(T i) SAL_THROW(())
+    {
+        return valueOf( static_cast<sal_Bool>(i) );
+    }
+
+    template< typename T >
+    static OUString valueChar(T i) SAL_THROW(())
+    {
+        return valueOf( static_cast<sal_Unicode>(i) );
+    }
+
+    /**
       Returns the string representation of the float argument.
 
       This function can't be used for language specific conversion.
diff --git a/sal/qa/rtl/strings/test_oustring_valuex.cxx b/sal/qa/rtl/strings/test_oustring_valuex.cxx
new file mode 100644
index 0000000..e86f5e7
--- /dev/null
+++ b/sal/qa/rtl/strings/test_oustring_valuex.cxx
@@ -0,0 +1,67 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <sal/types.h>
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+#include "rtl/ustring.hxx"
+
+namespace test { namespace oustring {
+
+class valueX : public CppUnit::TestFixture {
+public:
+    void testBool();
+    void testChar();
+    void testInt();
+
+    CPPUNIT_TEST_SUITE(valueX);
+    CPPUNIT_TEST(testBool);
+    CPPUNIT_TEST(testChar);
+    CPPUNIT_TEST(testInt);
+    CPPUNIT_TEST_SUITE_END();
+};
+
+} }
+
+CPPUNIT_TEST_SUITE_REGISTRATION(test::oustring::valueX);
+
+void test::oustring::valueX::testBool() {
+    rtl::OUString val1 = rtl::OUString::valueOf( sal_True );
+    rtl::OUString val2 = rtl::OUString::valueBool( sal_True );
+    CPPUNIT_ASSERT( val1 == val2 );
+    val1 = rtl::OUString::valueOf( sal_False );
+    val2 = rtl::OUString::valueBool( sal_False );
+    CPPUNIT_ASSERT( val1 == val2 );
+    val1 = rtl::OUString::valueOf( static_cast<sal_Bool>(1) );
+    val2 = rtl::OUString::valueBool( 1 );
+    CPPUNIT_ASSERT( val1 == val2 );
+    val1 = rtl::OUString::valueOf( static_cast<sal_Bool>(0) );
+    val2 = rtl::OUString::valueBool( 0 );
+    CPPUNIT_ASSERT( val1 == val2 );
+    val1 = rtl::OUString::valueOf( static_cast<sal_Bool>('X') );
+    val2 = rtl::OUString::valueBool( 'X' );
+    CPPUNIT_ASSERT( val1 == val2 );
+    val1 = rtl::OUString::valueOf( static_cast<sal_Bool>(NULL) );
+    val2 = rtl::OUString::valueBool( NULL );
+    CPPUNIT_ASSERT( val1 == val2 );
+}
+
+void test::oustring::valueX::testChar() {
+    rtl::OUString val1 = rtl::OUString::valueOf( static_cast<sal_Unicode>('X') );
+    rtl::OUString val2 = rtl::OUString::valueChar( 'X' );
+    CPPUNIT_ASSERT( val1 == val2 );
+}
+
+void test::oustring::valueX::testInt() {
+    rtl::OUString val1 = rtl::OUString::valueOf( 30039062 );
+    rtl::OUString val2 = rtl::OUString::valueInt( 30039062 );
+    CPPUNIT_ASSERT( val1 == val2 );
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

-- 
To view, visit https://gerrit.libreoffice.org/1625
To unsubscribe, visit https://gerrit.libreoffice.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id3d150a6525eb0334e41e2ec6640bb06cd790b43
Gerrit-PatchSet: 1
Gerrit-Project: core
Gerrit-Branch: master
Gerrit-Owner: Noel Grandin <noelgrandin at gmail.com>



More information about the LibreOffice mailing list