summaryrefslogtreecommitdiff
path: root/sal
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2015-06-23 14:53:56 +0200
committerStephan Bergmann <sbergman@redhat.com>2015-06-23 14:53:56 +0200
commitfd7889a9cfb601df65670dcdce8c4c2c2450f47c (patch)
tree771d97562d4023e975effbe3cae30a1ca204ef73 /sal
parent0f03d20011c31e36823ca5260e1840593d6949a1 (diff)
Implement full set of OUString::replaceFirst/All literal overloads
Change-Id: I5f525d91ce24d1d2653a6855f1c4fffc039ae398
Diffstat (limited to 'sal')
-rw-r--r--sal/qa/rtl/strings/test_strings_replace.cxx54
-rw-r--r--sal/rtl/ustring.cxx58
-rw-r--r--sal/util/sal.map6
3 files changed, 118 insertions, 0 deletions
diff --git a/sal/qa/rtl/strings/test_strings_replace.cxx b/sal/qa/rtl/strings/test_strings_replace.cxx
index 999df825a53a..0d47c3e6f4c5 100644
--- a/sal/qa/rtl/strings/test_strings_replace.cxx
+++ b/sal/qa/rtl/strings/test_strings_replace.cxx
@@ -37,12 +37,16 @@ private:
void ustringReplaceFirstAsciiL();
+ void ustringReplaceFirstToAsciiL();
+
void ustringReplaceFirstAsciiLAsciiL();
void ustringReplaceAll();
void ustringReplaceAllAsciiL();
+ void ustringReplaceAllToAsciiL();
+
void ustringReplaceAllAsciiLAsciiL();
CPPUNIT_TEST_SUITE(Test);
@@ -50,9 +54,11 @@ private:
CPPUNIT_TEST(stringReplaceAll);
CPPUNIT_TEST(ustringReplaceFirst);
CPPUNIT_TEST(ustringReplaceFirstAsciiL);
+ CPPUNIT_TEST(ustringReplaceFirstToAsciiL);
CPPUNIT_TEST(ustringReplaceFirstAsciiLAsciiL);
CPPUNIT_TEST(ustringReplaceAll);
CPPUNIT_TEST(ustringReplaceAllAsciiL);
+ CPPUNIT_TEST(ustringReplaceAllToAsciiL);
CPPUNIT_TEST(ustringReplaceAllAsciiLAsciiL);
CPPUNIT_TEST_SUITE_END();
};
@@ -172,6 +178,40 @@ void Test::ustringReplaceFirstAsciiL() {
}
}
+void Test::ustringReplaceFirstToAsciiL() {
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString("otherbarfoo"),
+ rtl::OUString("foobarfoo").replaceFirst(s_foo, "other"));
+
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString("foobarfoo"),
+ rtl::OUString("foobarfoo").replaceFirst(s_bars, "other"));
+
+ {
+ sal_Int32 n = 0;
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString("otherbarfoo"),
+ rtl::OUString("foobarfoo").replaceFirst(s_foo, "other", &n));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
+ }
+
+ {
+ sal_Int32 n = 1;
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString("foobarother"),
+ rtl::OUString("foobarfoo").replaceFirst(s_foo, "other", &n));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
+ }
+
+ {
+ sal_Int32 n = 4;
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString("foobarfoo"),
+ rtl::OUString("foobarfoo").replaceFirst(s_bar, "other", &n));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
+ }
+}
+
void Test::ustringReplaceFirstAsciiLAsciiL() {
CPPUNIT_ASSERT_EQUAL(
rtl::OUString("otherbarfoo"),
@@ -239,6 +279,20 @@ void Test::ustringReplaceAllAsciiL() {
rtl::OUString("xaa").replaceAll("xa", s_xx));
}
+void Test::ustringReplaceAllToAsciiL() {
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString("otherbarother"),
+ rtl::OUString("foobarfoo").replaceAll(s_foo, "other"));
+
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString("foobarfoo"),
+ rtl::OUString("foobarfoo").replaceAll(s_bars, "other"));
+
+ CPPUNIT_ASSERT_EQUAL(
+ rtl::OUString("xxa"),
+ rtl::OUString("xaa").replaceAll(s_xa, "xx"));
+}
+
void Test::ustringReplaceAllAsciiLAsciiL() {
CPPUNIT_ASSERT_EQUAL(
rtl::OUString("otherbarother"),
diff --git a/sal/rtl/ustring.cxx b/sal/rtl/ustring.cxx
index 3c9c8b750a1f..827077468fd5 100644
--- a/sal/rtl/ustring.cxx
+++ b/sal/rtl/ustring.cxx
@@ -1173,6 +1173,49 @@ void rtl_uString_newReplaceFirstAsciiL(
*index = i;
}
+void rtl_uString_newReplaceFirstToAsciiL(
+ rtl_uString ** newStr, rtl_uString * str, rtl_uString const * from,
+ char const * to, sal_Int32 toLength, sal_Int32 * index)
+ SAL_THROW_EXTERN_C()
+{
+ assert(str != 0);
+ assert(index != 0);
+ assert(*index >= 0 && *index <= str->length);
+ assert(from != 0);
+ assert(toLength >= 0);
+ sal_Int32 i = rtl_ustr_indexOfStr_WithLength(
+ str->buffer + *index, str->length - *index, from->buffer, from->length);
+ if (i == -1) {
+ rtl_uString_assign(newStr, str);
+ } else {
+ assert(i <= str->length - *index);
+ i += *index;
+ assert(from->length <= str->length);
+ if (str->length - from->length > SAL_MAX_INT32 - toLength) {
+ std::abort();
+ }
+ sal_Int32 n = str->length - from->length + toLength;
+ rtl_uString_acquire(str); // in case *newStr == str
+ if (n != 0) {
+ rtl_uString_new_WithLength(newStr, n);
+ (*newStr)->length = n;
+ assert(i >= 0 && i < str->length);
+ memcpy(
+ (*newStr)->buffer, str->buffer, i * sizeof (sal_Unicode));
+ for (sal_Int32 j = 0; j != toLength; ++j) {
+ assert(static_cast< unsigned char >(to[j]) <= 0x7F);
+ (*newStr)->buffer[i + j] = to[j];
+ }
+ memcpy(
+ (*newStr)->buffer + i + toLength,
+ str->buffer + i + from->length,
+ (str->length - i - from->length) * sizeof (sal_Unicode));
+ }
+ rtl_uString_release(str);
+ }
+ *index = i;
+}
+
void rtl_uString_newReplaceFirstAsciiLAsciiL(
rtl_uString ** newStr, rtl_uString * str, char const * from,
sal_Int32 fromLength, char const * to, sal_Int32 toLength,
@@ -1254,6 +1297,21 @@ void rtl_uString_newReplaceAllAsciiL(
}
}
+void rtl_uString_newReplaceAllToAsciiL(
+ rtl_uString ** newStr, rtl_uString * str, rtl_uString const * from,
+ char const * to, sal_Int32 toLength) SAL_THROW_EXTERN_C()
+{
+ assert(from != 0);
+ rtl_uString_assign(newStr, str);
+ for (sal_Int32 i = 0;; i += toLength) {
+ rtl_uString_newReplaceFirstToAsciiL(
+ newStr, *newStr, from, to, toLength, &i);
+ if (i == -1) {
+ break;
+ }
+ }
+}
+
void rtl_uString_newReplaceAllAsciiLAsciiL(
rtl_uString ** newStr, rtl_uString * str, char const * from,
sal_Int32 fromLength, char const * to, sal_Int32 toLength)
diff --git a/sal/util/sal.map b/sal/util/sal.map
index baafa66a255f..c41ee41ebc7e 100644
--- a/sal/util/sal.map
+++ b/sal/util/sal.map
@@ -683,6 +683,12 @@ LIBO_UDK_5.0 { # symbols available in >= LibO 5.0
rtl_secureZeroMemory;
} LIBO_UDK_4.3;
+LIBO_UDK_5.1 { # symbols available in >= LibO 5.1
+ global:
+ rtl_uString_newReplaceAllToAsciiL;
+ rtl_uString_newReplaceFirstToAsciiL;
+} LIBO_UDK_5.0;
+
PRIVATE_1.0 {
global:
osl_detail_ObjectRegistry_storeAddresses;