summaryrefslogtreecommitdiff
path: root/sal/qa
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2012-02-15 15:26:43 +0100
committerStephan Bergmann <sbergman@redhat.com>2012-02-15 15:41:09 +0100
commit9ab0b38e95133dab720408cc2c80093b8a201c10 (patch)
tree416dde227ed5c4ded99292feb94f36a64c327999 /sal/qa
parent42422f2599220b678aa41c4aadeec28df113c3ec (diff)
Various string function clean up
Added: * rtl::OString::matchL * rtl::OString::endsWith * rtl::OString::endsWithL * rtl::OString::indexOfL * rtl::OString::replaceFirst * rtl::OString::replaceAll * rtl::OString::getToken * rtl::OUString::endsWith * rtl::OUString::replaceFirst * rtl::OUString::replaceFirstAsciiL * rtl::OUString::replaceFirstAsciiLAsciiL * rtl::OUString::replaceAll * rtl::OUString::replaceAllAsciiL * rtl::OUString::replaceAllAsciiLAsciiL * rtl::OUString::getToken plus underlying C functions where necessary Deprecated: * comphelper::string::remove * comphelper::string::getToken Removed: * comphelper::string::searchAndReplaceAsciiL * comphelper::string::searchAndReplaceAllAsciiWithAscii * comphelper::string::searchAndReplaceAsciiI * comphelper::string::replace * comphelper::string::matchL * comphelper::string::matchIgnoreAsciiCaseL * comphelper::string::indexOfL Also fixed some apparent misuses of RTL_CONSTASCII_USTRINGPARAM -> RTL_CONSTASCII_STRINGPARAM.
Diffstat (limited to 'sal/qa')
-rw-r--r--sal/qa/rtl/strings/test_strings_replace.cxx349
1 files changed, 349 insertions, 0 deletions
diff --git a/sal/qa/rtl/strings/test_strings_replace.cxx b/sal/qa/rtl/strings/test_strings_replace.cxx
new file mode 100644
index 000000000000..33638899a4ab
--- /dev/null
+++ b/sal/qa/rtl/strings/test_strings_replace.cxx
@@ -0,0 +1,349 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * Version: MPL 1.1 / GPLv3+ / LGPLv3+
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License or as specified alternatively below. You may obtain a copy of
+ * the License at http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * Major Contributor(s):
+ * [ Copyright (C) 2012 Red Hat, Inc., Stephan Bergmann <sbergman@redhat.com>
+ * (initial developer) ]
+ *
+ * All Rights Reserved.
+ *
+ * For minor contributions see the git repository.
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
+ * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
+ * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
+ * instead of those above.
+ */
+
+#include "sal/config.h"
+#include "sal/precppunit.hxx"
+
+#include <ostream>
+
+#include "cppunit/TestAssert.h"
+#include "cppunit/TestFixture.h"
+#include "cppunit/extensions/HelperMacros.h"
+#include "rtl/oustringostreaminserter.hxx"
+#include "rtl/string.h"
+#include "rtl/string.hxx"
+#include "rtl/ustring.h"
+#include "rtl/ustring.hxx"
+
+namespace rtl {
+
+template< typename charT, typename traits > std::basic_ostream<charT, traits> &
+operator <<(
+ std::basic_ostream<charT, traits> & stream, rtl::OString const & string)
+{
+ return stream << string.getStr();
+ // best effort; potentially loses data due to embedded null characters
+}
+
+}
+
+namespace {
+
+class Test: public CppUnit::TestFixture {
+private:
+ void stringReplaceFirst();
+
+ void stringReplaceAll();
+
+ void ustringReplaceFirst();
+
+ void ustringReplaceFirstAsciiL();
+
+ void ustringReplaceFirstAsciiLAsciiL();
+
+ void ustringReplaceAll();
+
+ void ustringReplaceAllAsciiL();
+
+ void ustringReplaceAllAsciiLAsciiL();
+
+ CPPUNIT_TEST_SUITE(Test);
+ CPPUNIT_TEST(stringReplaceFirst);
+ CPPUNIT_TEST(stringReplaceAll);
+ CPPUNIT_TEST(ustringReplaceFirst);
+ CPPUNIT_TEST(ustringReplaceFirstAsciiL);
+ CPPUNIT_TEST(ustringReplaceFirstAsciiLAsciiL);
+ CPPUNIT_TEST(ustringReplaceAll);
+ CPPUNIT_TEST(ustringReplaceAllAsciiL);
+ CPPUNIT_TEST(ustringReplaceAllAsciiLAsciiL);
+ CPPUNIT_TEST_SUITE_END();
+};
+
+void Test::stringReplaceFirst() {
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OString("otherbarfoo"),
+ rtl::OString("foobarfoo").replaceFirst("foo", "other"));
+
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OString("foobarfoo"),
+ rtl::OString("foobarfoo").replaceFirst("bars", "other"));
+
+ {
+ sal_Int32 n = 0;
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OString("otherbarfoo"),
+ rtl::OString("foobarfoo").replaceFirst("foo", "other", &n));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
+ }
+
+ {
+ sal_Int32 n = 1;
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OString("foobarother"),
+ rtl::OString("foobarfoo").replaceFirst("foo", "other", &n));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
+ }
+
+ {
+ sal_Int32 n = 4;
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OString("foobarfoo"),
+ rtl::OString("foobarfoo").replaceFirst("bar", "other", &n));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
+ }
+}
+
+void Test::stringReplaceAll() {
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OString("otherbarother"),
+ rtl::OString("foobarfoo").replaceAll("foo", "other"));
+
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OString("foobarfoo"),
+ rtl::OString("foobarfoo").replaceAll("bars", "other"));
+
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OString("xxa"), rtl::OString("xaa").replaceAll("xa", "xx"));
+}
+
+void Test::ustringReplaceFirst() {
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("otherbarfoo")),
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")).replaceFirst(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foo")),
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("other"))));
+
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")),
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")).replaceFirst(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("bars")),
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("other"))));
+
+ {
+ sal_Int32 n = 0;
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("otherbarfoo")),
+ (rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")).
+ replaceFirst(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foo")),
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("other")), &n)));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
+ }
+
+ {
+ sal_Int32 n = 1;
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarother")),
+ (rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")).
+ replaceFirst(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foo")),
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("other")), &n)));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
+ }
+
+ {
+ sal_Int32 n = 4;
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")),
+ (rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")).
+ replaceFirst(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("bar")),
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("other")), &n)));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
+ }
+}
+
+void Test::ustringReplaceFirstAsciiL() {
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("otherbarfoo")),
+ (rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")).
+ replaceFirstAsciiL(
+ RTL_CONSTASCII_STRINGPARAM("foo"),
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("other")))));
+
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")),
+ (rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")).
+ replaceFirstAsciiL(
+ RTL_CONSTASCII_STRINGPARAM("bars"),
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("other")))));
+
+ {
+ sal_Int32 n = 0;
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("otherbarfoo")),
+ (rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")).
+ replaceFirstAsciiL(
+ RTL_CONSTASCII_STRINGPARAM("foo"),
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("other")), &n)));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
+ }
+
+ {
+ sal_Int32 n = 1;
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarother")),
+ (rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")).
+ replaceFirstAsciiL(
+ RTL_CONSTASCII_STRINGPARAM("foo"),
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("other")), &n)));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
+ }
+
+ {
+ sal_Int32 n = 4;
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")),
+ (rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")).
+ replaceFirstAsciiL(
+ RTL_CONSTASCII_STRINGPARAM("bar"),
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("other")), &n)));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
+ }
+}
+
+void Test::ustringReplaceFirstAsciiLAsciiL() {
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("otherbarfoo")),
+ (rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")).
+ replaceFirstAsciiLAsciiL(
+ RTL_CONSTASCII_STRINGPARAM("foo"),
+ RTL_CONSTASCII_STRINGPARAM("other"))));
+
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")),
+ (rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")).
+ replaceFirstAsciiLAsciiL(
+ RTL_CONSTASCII_STRINGPARAM("bars"),
+ RTL_CONSTASCII_STRINGPARAM("other"))));
+
+ {
+ sal_Int32 n = 0;
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("otherbarfoo")),
+ (rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")).
+ replaceFirstAsciiLAsciiL(
+ RTL_CONSTASCII_STRINGPARAM("foo"),
+ RTL_CONSTASCII_STRINGPARAM("other"), &n)));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
+ }
+
+ {
+ sal_Int32 n = 1;
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarother")),
+ (rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")).
+ replaceFirstAsciiLAsciiL(
+ RTL_CONSTASCII_STRINGPARAM("foo"),
+ RTL_CONSTASCII_STRINGPARAM("other"), &n)));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
+ }
+
+ {
+ sal_Int32 n = 4;
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")),
+ (rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")).
+ replaceFirstAsciiLAsciiL(
+ RTL_CONSTASCII_STRINGPARAM("bar"),
+ RTL_CONSTASCII_STRINGPARAM("other"), &n)));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
+ }
+}
+
+void Test::ustringReplaceAll() {
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("otherbarother")),
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")).replaceAll(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foo")),
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("other"))));
+
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")),
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")).replaceAll(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("bars")),
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("other"))));
+
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("xxa")),
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("xaa")).replaceAll(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("xa")),
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("xx"))));
+}
+
+void Test::ustringReplaceAllAsciiL() {
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("otherbarother")),
+ (rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")).
+ replaceAllAsciiL(
+ RTL_CONSTASCII_STRINGPARAM("foo"),
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("other")))));
+
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")),
+ (rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")).
+ replaceAllAsciiL(
+ RTL_CONSTASCII_STRINGPARAM("bars"),
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("other")))));
+
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("xxa")),
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("xaa")).replaceAllAsciiL(
+ RTL_CONSTASCII_STRINGPARAM("xa"),
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("xx"))));
+}
+
+void Test::ustringReplaceAllAsciiLAsciiL() {
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("otherbarother")),
+ (rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")).
+ replaceAllAsciiLAsciiL(
+ RTL_CONSTASCII_STRINGPARAM("foo"),
+ RTL_CONSTASCII_STRINGPARAM("other"))));
+
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")),
+ (rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobarfoo")).
+ replaceAllAsciiLAsciiL(
+ RTL_CONSTASCII_STRINGPARAM("bars"),
+ RTL_CONSTASCII_STRINGPARAM("other"))));
+
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("xxa")),
+ (rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("xaa")).
+ replaceAllAsciiLAsciiL(
+ RTL_CONSTASCII_STRINGPARAM("xa"),
+ RTL_CONSTASCII_STRINGPARAM("xx"))));
+}
+
+}
+
+CPPUNIT_TEST_SUITE_REGISTRATION(Test);
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */