summaryrefslogtreecommitdiff
path: root/sal
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
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')
-rw-r--r--sal/CppunitTest_sal_rtl_strings.mk1
-rw-r--r--sal/inc/rtl/string.h60
-rw-r--r--sal/inc/rtl/string.hxx152
-rw-r--r--sal/inc/rtl/ustring.h159
-rw-r--r--sal/inc/rtl/ustring.hxx204
-rw-r--r--sal/qa/rtl/strings/test_strings_replace.cxx349
-rw-r--r--sal/rtl/source/string.cxx59
-rw-r--r--sal/rtl/source/ustring.cxx178
-rw-r--r--sal/util/sal.map8
9 files changed, 1170 insertions, 0 deletions
diff --git a/sal/CppunitTest_sal_rtl_strings.mk b/sal/CppunitTest_sal_rtl_strings.mk
index fe6da666b496..f43e7fd3a73d 100644
--- a/sal/CppunitTest_sal_rtl_strings.mk
+++ b/sal/CppunitTest_sal_rtl_strings.mk
@@ -28,6 +28,7 @@
$(eval $(call gb_CppunitTest_CppunitTest,sal_rtl_strings))
$(eval $(call gb_CppunitTest_add_exception_objects,sal_rtl_strings,\
+ sal/qa/rtl/strings/test_strings_replace \
sal/qa/rtl/strings/test_oustring_compare \
sal/qa/rtl/strings/test_oustring_convert \
sal/qa/rtl/strings/test_oustring_endswith \
diff --git a/sal/inc/rtl/string.h b/sal/inc/rtl/string.h
index 671d2fdc0053..da356932979b 100644
--- a/sal/inc/rtl/string.h
+++ b/sal/inc/rtl/string.h
@@ -1000,6 +1000,66 @@ SAL_DLLPUBLIC void SAL_CALL rtl_string_newReplaceStrAt(
SAL_DLLPUBLIC void SAL_CALL rtl_string_newReplace(
rtl_String ** newStr, rtl_String * str, sal_Char oldChar, sal_Char newChar ) SAL_THROW_EXTERN_C();
+/** Create a new string by replacing the first occurrence of a given substring
+ with another substring.
+
+ @param[in, out] newStr pointer to the new string; must not be null; must
+ point to null or a valid rtl_String
+
+ @param str pointer to the original string; must not be null
+
+ @param from pointer to the substring to be replaced; must not be null and
+ must point to memory of at least \p fromLength bytes
+
+ @param fromLength the length of the \p from substring; must be non-negative
+
+ @param to pointer to the replacing substring; must not be null and must
+ point to memory of at least \p toLength bytes
+
+ @param toLength the length of the \p to substring; must be non-negative
+
+ @param[in,out] index pointer to a start index, must not be null; upon entry
+ to the function its value is the index into the original string at which to
+ start searching for the \p from substring, the value must be non-negative
+ and not greater than the original string's length; upon exit from the
+ function its value is the index into the original string at which the
+ replacement took place or -1 if no replacement took place
+
+ @since LibreOffice 3.6
+*/
+SAL_DLLPUBLIC void SAL_CALL rtl_string_newReplaceFirst(
+ rtl_String ** newStr, rtl_String * str, char const * from,
+ sal_Int32 fromLength, char const * to, sal_Int32 toLength,
+ sal_Int32 * index) SAL_THROW_EXTERN_C();
+
+/** Create a new string by replacing all occurrences of a given substring with
+ another substring.
+
+ Replacing subsequent occurrences picks up only after a given replacement.
+ That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
+
+ @param[in, out] newStr pointer to the new string; must not be null; must
+ point to null or a valid rtl_String
+
+ @param str pointer to the original string; must not be null
+
+ @param from pointer to the substring to be replaced; must not be null and
+ must point to memory of at least \p fromLength bytes
+
+ @param fromLength the length of the \p from substring; must be non-negative
+
+ @param to pointer to the replacing substring; must not be null and must
+ point to memory of at least \p toLength bytes
+
+ @param toLength the length of the \p to substring; must be non-negative
+
+ @since LibreOffice 3.6
+*/
+SAL_DLLPUBLIC void SAL_CALL rtl_string_newReplaceAll(
+ rtl_String ** newStr, rtl_String * str, char const * from,
+ sal_Int32 fromLength, char const * to, sal_Int32 toLength)
+ SAL_THROW_EXTERN_C();
+
/** Create a new string by converting all ASCII uppercase letters to lowercase
within another string.
diff --git a/sal/inc/rtl/string.hxx b/sal/inc/rtl/string.hxx
index e39e0e091cdf..68f2aa190ce4 100644
--- a/sal/inc/rtl/string.hxx
+++ b/sal/inc/rtl/string.hxx
@@ -471,6 +471,31 @@ public:
}
/**
+ Match against a substring appearing in this string.
+
+ @param str the substring to be compared; must not be null and must point
+ to memory of at least strLength bytes
+
+ @param strLength the length of the substring; must be non-negative
+
+ @param fromIndex the index into this string to start the comparison at;
+ must be non-negative and not greater than this string's length
+
+ @return true if and only if the given str is contained as a substring of
+ this string at the given fromIndex
+
+ @since LibreOffice 3.6
+ */
+ bool matchL(
+ char const * str, sal_Int32 strLength, sal_Int32 fromIndex = 0)
+ const
+ {
+ return rtl_str_shortenedCompare_WithLength(
+ pData->buffer + fromIndex, pData->length - fromIndex,
+ str, strLength, strLength) == 0;
+ }
+
+ /**
Match against a substring appearing in this string, ignoring the case of
ASCII letters.
@@ -495,6 +520,39 @@ public:
str.pData->length ) == 0;
}
+ /**
+ Check whether this string ends with a given substring.
+
+ @param str the substring to be compared
+
+ @return true if and only if the given str appears as a substring at the
+ end of this string
+
+ @since LibreOffice 3.6
+ */
+ bool endsWith(rtl::OString const & str) const {
+ return str.getLength() <= getLength()
+ && match(str, getLength() - str.getLength());
+ }
+
+ /**
+ Check whether this string ends with a given substring.
+
+ @param str the substring to be compared; must not be null and must point
+ to memory of at least strLength bytes
+
+ @param strLength the length of the substring; must be non-negative
+
+ @return true if and only if the given str appears as a substring at the
+ end of this string
+
+ @since LibreOffice 3.6
+ */
+ bool endsWithL(char const * str, sal_Int32 strLength) const {
+ return strLength <= getLength()
+ && matchL(str, strLength, getLength() - strLength);
+ }
+
friend sal_Bool operator == ( const OString& rStr1, const OString& rStr2 ) SAL_THROW(())
{ return rStr1.getLength() == rStr2.getLength() && rStr1.compareTo( rStr2 ) == 0; }
friend sal_Bool operator == ( const OString& rStr1, const sal_Char * pStr2 ) SAL_THROW(())
@@ -603,6 +661,32 @@ public:
}
/**
+ Returns the index within this string of the first occurrence of the
+ specified substring, starting at the specified index.
+
+ If str doesn't include any character, always -1 is
+ returned. This is also the case, if both strings are empty.
+
+ @param str the substring to search for.
+ @param len the length of the substring.
+ @param fromIndex the index to start the search from.
+ @return If the string argument occurs one or more times as a substring
+ within this string at the starting index, then the index
+ of the first character of the first such substring is
+ returned. If it does not occur as a substring starting
+ at fromIndex or beyond, -1 is returned.
+
+ @since LibreOffice 3.6
+ */
+ sal_Int32 indexOfL(char const * str, sal_Int32 len, sal_Int32 fromIndex = 0)
+ const SAL_THROW(())
+ {
+ sal_Int32 n = rtl_str_indexOfStr_WithLength(
+ pData->buffer + fromIndex, pData->length - fromIndex, str, len);
+ return n < 0 ? n : n + fromIndex;
+ }
+
+ /**
Returns the index within this string of the last occurrence of
the specified substring, searching backward starting at the end.
@@ -754,6 +838,56 @@ public:
}
/**
+ Returns a new string resulting from replacing the first occurrence of a
+ given substring with another substring.
+
+ @param from the substring to be replaced
+
+ @param to the replacing substring
+
+ @param[in,out] index pointer to a start index; if the pointer is
+ non-null: upon entry to the function, its value is the index into the this
+ string at which to start searching for the \p from substring, the value
+ must be non-negative and not greater than this string's length; upon exit
+ from the function its value is the index into this string at which the
+ replacement took place or -1 if no replacement took place; if the pointer
+ is null, searching always starts at index 0
+
+ @since LibreOffice 3.6
+ */
+ OString replaceFirst(
+ OString const & from, OString const & to, sal_Int32 * index = 0) const
+ {
+ rtl_String * s = 0;
+ sal_Int32 i = 0;
+ rtl_string_newReplaceFirst(
+ &s, pData, from.pData->buffer, from.pData->length,
+ to.pData->buffer, to.pData->length, index == 0 ? &i : index);
+ return OString(s, SAL_NO_ACQUIRE);
+ }
+
+ /**
+ Returns a new string resulting from replacing all occurrences of a given
+ substring with another substring.
+
+ Replacing subsequent occurrences picks up only after a given replacement.
+ That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
+
+ @param from the substring to be replaced
+
+ @param to the replacing substring
+
+ @since LibreOffice 3.6
+ */
+ OString replaceAll(OString const & from, OString const & to) const {
+ rtl_String * s = 0;
+ rtl_string_newReplaceAll(
+ &s, pData, from.pData->buffer, from.pData->length,
+ to.pData->buffer, to.pData->length);
+ return OString(s, SAL_NO_ACQUIRE);
+ }
+
+ /**
Converts from this string all ASCII uppercase characters (65-90)
to ASCII lowercase characters (97-122).
@@ -837,6 +971,24 @@ public:
}
/**
+ Returns a token from the string.
+
+ The same as getToken(sal_Int32, sal_Char, sal_Int32 &), but always passing
+ in 0 as the start index in the third argument.
+
+ @param count the number of the token to return, starting with 0
+ @param separator the character which separates the tokens
+
+ @return the given token, or an empty string
+
+ @since LibreOffice 3.6
+ */
+ OString getToken(sal_Int32 count, char separator) const {
+ sal_Int32 n = 0;
+ return getToken(count, separator, n);
+ }
+
+ /**
Returns the Boolean value from this string.
This function can't be used for language specific conversion.
diff --git a/sal/inc/rtl/ustring.h b/sal/inc/rtl/ustring.h
index e75185bd00a0..03f145ba6ba4 100644
--- a/sal/inc/rtl/ustring.h
+++ b/sal/inc/rtl/ustring.h
@@ -1384,6 +1384,165 @@ SAL_DLLPUBLIC void SAL_CALL rtl_uString_newReplaceStrAt(
SAL_DLLPUBLIC void SAL_CALL rtl_uString_newReplace(
rtl_uString ** newStr, rtl_uString * str, sal_Unicode oldChar, sal_Unicode newChar ) SAL_THROW_EXTERN_C();
+/** Create a new string by replacing the first occurrence of a given substring
+ with another substring.
+
+ @param[in, out] newStr pointer to the new string; must not be null; must
+ point to null or a valid rtl_uString
+
+ @param str pointer to the original string; must not be null
+
+ @param from pointer to the substring to be replaced; must not be null
+
+ @param to pointer to the replacing substring; must not be null
+
+ @param[in,out] index pointer to a start index, must not be null; upon entry
+ to the function its value is the index into the original string at which to
+ start searching for the \p from substring, the value must be non-negative
+ and not greater than the original string's length; upon exit from the
+ function its value is the index into the original string at which the
+ replacement took place or -1 if no replacement took place
+
+ @since LibreOffice 3.6
+*/
+SAL_DLLPUBLIC void SAL_CALL rtl_uString_newReplaceFirst(
+ rtl_uString ** newStr, rtl_uString * str, rtl_uString const * from,
+ rtl_uString const * to, sal_Int32 * index) SAL_THROW_EXTERN_C();
+
+/** Create a new string by replacing the first occurrence of a given substring
+ with another substring.
+
+ @param[in, out] newStr pointer to the new string; must not be null; must
+ point to null or a valid rtl_uString
+
+ @param str pointer to the original string; must not be null
+
+ @param from pointer to the substring to be replaced; must not be null and
+ must point to memory of at least \p fromLength ASCII bytes
+
+ @param fromLength the length of the \p from substring; must be non-negative
+
+ @param to pointer to the replacing substring; must not be null
+
+ @param[in,out] index pointer to a start index, must not be null; upon entry
+ to the function its value is the index into the original string at which to
+ start searching for the \p from substring, the value must be non-negative
+ and not greater than the original string's length; upon exit from the
+ function its value is the index into the original string at which the
+ replacement took place or -1 if no replacement took place
+
+ @since LibreOffice 3.6
+*/
+SAL_DLLPUBLIC void SAL_CALL rtl_uString_newReplaceFirstAsciiL(
+ rtl_uString ** newStr, rtl_uString * str, char const * from,
+ sal_Int32 fromLength, rtl_uString const * to, sal_Int32 * index)
+ SAL_THROW_EXTERN_C();
+
+/** Create a new string by replacing the first occurrence of a given substring
+ with another substring.
+
+ @param[in, out] newStr pointer to the new string; must not be null; must
+ point to null or a valid rtl_uString
+
+ @param str pointer to the original string; must not be null
+
+ @param from pointer to the substring to be replaced; must not be null and
+ must point to memory of at least \p fromLength ASCII bytes
+
+ @param fromLength the length of the \p from substring; must be non-negative
+
+ @param to pointer to the substring to be replaced; must not be null and
+ must point to memory of at least \p toLength ASCII bytes
+
+ @param toLength the length of the \p to substring; must be non-negative
+
+ @param[in,out] index pointer to a start index, must not be null; upon entry
+ to the function its value is the index into the original string at which to
+ start searching for the \p from substring, the value must be non-negative
+ and not greater than the original string's length; upon exit from the
+ function its value is the index into the original string at which the
+ replacement took place or -1 if no replacement took place
+
+ @since LibreOffice 3.6
+*/
+SAL_DLLPUBLIC void SAL_CALL rtl_uString_newReplaceFirstAsciiLAsciiL(
+ rtl_uString ** newStr, rtl_uString * str, char const * from,
+ sal_Int32 fromLength, char const * to, sal_Int32 toLength,
+ sal_Int32 * index) SAL_THROW_EXTERN_C();
+
+/** Create a new string by replacing all occurrences of a given substring with
+ another substring.
+
+ Replacing subsequent occurrences picks up only after a given replacement.
+ That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
+
+ @param[in, out] newStr pointer to the new string; must not be null; must
+ point to null or a valid rtl_uString
+
+ @param str pointer to the original string; must not be null
+
+ @param from pointer to the substring to be replaced; must not be null
+
+ @param to pointer to the replacing substring; must not be null
+
+ @since LibreOffice 3.6
+*/
+SAL_DLLPUBLIC void SAL_CALL rtl_uString_newReplaceAll(
+ rtl_uString ** newStr, rtl_uString * str, rtl_uString const * from,
+ rtl_uString const * to) SAL_THROW_EXTERN_C();
+
+/** Create a new string by replacing all occurrences of a given substring with
+ another substring.
+
+ Replacing subsequent occurrences picks up only after a given replacement.
+ That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
+
+ @param[in, out] newStr pointer to the new string; must not be null; must
+ point to null or a valid rtl_uString
+
+ @param str pointer to the original string; must not be null
+
+ @param from pointer to the substring to be replaced; must not be null and
+ must point to memory of at least \p fromLength ASCII bytes
+
+ @param fromLength the length of the \p from substring; must be non-negative
+
+ @param to pointer to the replacing substring; must not be null
+
+ @since LibreOffice 3.6
+*/
+SAL_DLLPUBLIC void SAL_CALL rtl_uString_newReplaceAllAsciiL(
+ rtl_uString ** newStr, rtl_uString * str, char const * from,
+ sal_Int32 fromLength, rtl_uString const * to) SAL_THROW_EXTERN_C();
+
+/** Create a new string by replacing all occurrences of a given substring with
+ another substring.
+
+ Replacing subsequent occurrences picks up only after a given replacement.
+ That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
+
+ @param[in, out] newStr pointer to the new string; must not be null; must
+ point to null or a valid rtl_uString
+
+ @param str pointer to the original string; must not be null
+
+ @param from pointer to the substring to be replaced; must not be null and
+ must point to memory of at least \p fromLength ASCII bytes
+
+ @param fromLength the length of the \p from substring; must be non-negative
+
+ @param to pointer to the substring to be replaced; must not be null and
+ must point to memory of at least \p toLength ASCII bytes
+
+ @param toLength the length of the \p to substring; must be non-negative
+
+ @since LibreOffice 3.6
+*/
+SAL_DLLPUBLIC void SAL_CALL rtl_uString_newReplaceAllAsciiLAsciiL(
+ rtl_uString ** newStr, rtl_uString * str, char const * from,
+ sal_Int32 fromLength, char const * to, sal_Int32 toLength)
+ SAL_THROW_EXTERN_C();
+
/** Create a new string by converting all ASCII uppercase letters to lowercase
within another string.
diff --git a/sal/inc/rtl/ustring.hxx b/sal/inc/rtl/ustring.hxx
index ace4b1ae7179..cf1ec92df360 100644
--- a/sal/inc/rtl/ustring.hxx
+++ b/sal/inc/rtl/ustring.hxx
@@ -712,6 +712,21 @@ public:
}
/**
+ Check whether this string ends with a given substring.
+
+ @param str the substring to be compared
+
+ @return true if and only if the given str appears as a substring at the
+ end of this string
+
+ @since LibreOffice 3.6
+ */
+ bool endsWith(rtl::OUString const & str) const {
+ return str.getLength() <= getLength()
+ && match(str, getLength() - str.getLength());
+ }
+
+ /**
Check whether this string ends with a given ASCII string.
@param asciiStr a sequence of at least asciiStrLength ASCII characters
@@ -1067,6 +1082,177 @@ public:
}
/**
+ Returns a new string resulting from replacing the first occurrence of a
+ given substring with another substring.
+
+ @param from the substring to be replaced
+
+ @param to the replacing substring
+
+ @param[in,out] index pointer to a start index; if the pointer is
+ non-null: upon entry to the function, its value is the index into the this
+ string at which to start searching for the \p from substring, the value
+ must be non-negative and not greater than this string's length; upon exit
+ from the function its value is the index into this string at which the
+ replacement took place or -1 if no replacement took place; if the pointer
+ is null, searching always starts at index 0
+
+ @since LibreOffice 3.6
+ */
+ OUString replaceFirst(
+ OUString const & from, OUString const & to, sal_Int32 * index = 0) const
+ {
+ rtl_uString * s = 0;
+ sal_Int32 i = 0;
+ rtl_uString_newReplaceFirst(
+ &s, pData, from.pData, to.pData, index == 0 ? &i : index);
+ return OUString(s, SAL_NO_ACQUIRE);
+ }
+
+ /**
+ Returns a new string resulting from replacing the first occurrence of a
+ given substring with another substring.
+
+ @param from pointer to the substring to be replaced; must not be null and
+ must point to memory of at least \p fromLength ASCII bytes
+
+ @param fromLength the length of the \p from substring; must be
+ non-negative
+
+ @param to the replacing substring
+
+ @param[in,out] index pointer to a start index; if the pointer is
+ non-null: upon entry to the function, its value is the index into the this
+ string at which to start searching for the \p from substring, the value
+ must be non-negative and not greater than this string's length; upon exit
+ from the function its value is the index into this string at which the
+ replacement took place or -1 if no replacement took place; if the pointer
+ is null, searching always starts at index 0
+
+ @since LibreOffice 3.6
+ */
+ OUString replaceFirstAsciiL(
+ char const * from, sal_Int32 fromLength, rtl::OUString const & to,
+ sal_Int32 * index = 0) const
+ {
+ rtl_uString * s = 0;
+ sal_Int32 i = 0;
+ rtl_uString_newReplaceFirstAsciiL(
+ &s, pData, from, fromLength, to.pData, index == 0 ? &i : index);
+ return OUString(s, SAL_NO_ACQUIRE);
+ }
+
+ /**
+ Returns a new string resulting from replacing the first occurrence of a
+ given substring with another substring.
+
+ @param from pointer to the substring to be replaced; must not be null and
+ must point to memory of at least \p fromLength ASCII bytes
+
+ @param fromLength the length of the \p from substring; must be
+ non-negative
+
+ @param to pointer to the substring to be replaced; must not be null and
+ must point to memory of at least \p toLength ASCII bytes
+
+ @param toLength the length of the \p to substring; must be non-negative
+
+ @param[in,out] index pointer to a start index; if the pointer is
+ non-null: upon entry to the function, its value is the index into the this
+ string at which to start searching for the \p from substring, the value
+ must be non-negative and not greater than this string's length; upon exit
+ from the function its value is the index into this string at which the
+ replacement took place or -1 if no replacement took place; if the pointer
+ is null, searching always starts at index 0
+
+ @since LibreOffice 3.6
+ */
+ OUString replaceFirstAsciiLAsciiL(
+ char const * from, sal_Int32 fromLength, char const * to,
+ sal_Int32 toLength, sal_Int32 * index = 0) const
+ {
+ rtl_uString * s = 0;
+ sal_Int32 i = 0;
+ rtl_uString_newReplaceFirstAsciiLAsciiL(
+ &s, pData, from, fromLength, to, toLength, index == 0 ? &i : index);
+ return OUString(s, SAL_NO_ACQUIRE);
+ }
+
+ /**
+ Returns a new string resulting from replacing all occurrences of a given
+ substring with another substring.
+
+ Replacing subsequent occurrences picks up only after a given replacement.
+ That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
+
+ @param from the substring to be replaced
+
+ @param to the replacing substring
+
+ @since LibreOffice 3.6
+ */
+ OUString replaceAll(OUString const & from, OUString const & to) const {
+ rtl_uString * s = 0;
+ rtl_uString_newReplaceAll(&s, pData, from.pData, to.pData);
+ return OUString(s, SAL_NO_ACQUIRE);
+ }
+
+ /**
+ Returns a new string resulting from replacing all occurrences of a given
+ substring with another substring.
+
+ Replacing subsequent occurrences picks up only after a given replacement.
+ That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
+
+ @param from pointer to the substring to be replaced; must not be null and
+ must point to memory of at least \p fromLength ASCII bytes
+
+ @param fromLength the length of the \p from substring; must be
+ non-negative
+
+ @param to the replacing substring
+
+ @since LibreOffice 3.6
+ */
+ OUString replaceAllAsciiL(
+ char const * from, sal_Int32 fromLength, OUString const & to) const
+ {
+ rtl_uString * s = 0;
+ rtl_uString_newReplaceAllAsciiL(&s, pData, from, fromLength, to.pData);
+ return OUString(s, SAL_NO_ACQUIRE);
+ }
+
+ /**
+ Returns a new string resulting from replacing all occurrences of a given
+ substring with another substring.
+
+ Replacing subsequent occurrences picks up only after a given replacement.
+ That is, replacing from "xa" to "xx" in "xaa" results in "xxa", not "xxx".
+
+ @param from pointer to the substring to be replaced; must not be null and
+ must point to memory of at least \p fromLength ASCII bytes
+
+ @param fromLength the length of the \p from substring; must be
+ non-negative
+
+ @param to pointer to the substring to be replaced; must not be null and
+ must point to memory of at least \p toLength ASCII bytes
+
+ @param toLength the length of the \p to substring; must be non-negative
+
+ @since LibreOffice 3.6
+ */
+ OUString replaceAllAsciiLAsciiL(
+ char const * from, sal_Int32 fromLength, char const * to,
+ sal_Int32 toLength) const
+ {
+ rtl_uString * s = 0;
+ rtl_uString_newReplaceAllAsciiLAsciiL(
+ &s, pData, from, fromLength, to, toLength);
+ return OUString(s, SAL_NO_ACQUIRE);
+ }
+
+ /**
Converts from this string all ASCII uppercase characters (65-90)
to ASCII lowercase characters (97-122).
@@ -1150,6 +1336,24 @@ public:
}
/**
+ Returns a token from the string.
+
+ The same as getToken(sal_Int32, sal_Unicode, sal_Int32 &), but always
+ passing in 0 as the start index in the third argument.
+
+ @param count the number of the token to return, starting with 0
+ @param separator the character which separates the tokens
+
+ @return the given token, or an empty string
+
+ @since LibreOffice 3.6
+ */
+ OUString getToken(sal_Int32 count, sal_Unicode separator) const {
+ sal_Int32 n = 0;
+ return getToken(count, separator, n);
+ }
+
+ /**
Returns the Boolean value from this string.
This function can't be used for language specific conversion.
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: */
diff --git a/sal/rtl/source/string.cxx b/sal/rtl/source/string.cxx
index c43b564a2099..9c5393f32779 100644
--- a/sal/rtl/source/string.cxx
+++ b/sal/rtl/source/string.cxx
@@ -25,10 +25,16 @@
* for a copy of the LGPLv3 License.
*
************************************************************************/
+
+#include "sal/config.h"
+
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
#pragma warning(disable:4738) // storing 32-bit float result in memory, possible loss of performance
#endif
+#include <cassert>
+#include <cstdlib>
+
#include <rtl/memory.h>
#include <osl/interlck.h>
#include <rtl/alloc.h>
@@ -324,4 +330,57 @@ sal_Bool SAL_CALL rtl_convertUStringToString(rtl_String ** pTarget,
nFlags, sal_True);
}
+void rtl_string_newReplaceFirst(
+ rtl_String ** newStr, rtl_String * str, char const * from,
+ sal_Int32 fromLength, 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(fromLength >= 0);
+ assert(toLength >= 0);
+ sal_Int32 i = rtl_str_indexOfStr_WithLength(
+ str->buffer + *index, str->length - *index, from, fromLength);
+ if (i == -1) {
+ rtl_string_assign(newStr, str);
+ } else {
+ assert(i <= str->length - *index);
+ i += *index;
+ assert(fromLength <= str->length);
+ if (str->length - fromLength > SAL_MAX_INT32 - toLength) {
+ std::abort();
+ }
+ sal_Int32 n = str->length - fromLength + toLength;
+ rtl_string_acquire(str); // in case *newStr == str
+ rtl_string_new_WithLength(newStr, n);
+ if (n != 0) {
+ (*newStr)->length = n;
+ assert(i >= 0 && i < str->length);
+ rtl_copyMemory((*newStr)->buffer, str->buffer, i);
+ rtl_copyMemory((*newStr)->buffer + i, to, toLength);
+ rtl_copyMemory(
+ (*newStr)->buffer + i + toLength, str->buffer + i + fromLength,
+ str->length - i - fromLength);
+ }
+ rtl_string_release(str);
+ }
+ *index = i;
+}
+
+void rtl_string_newReplaceAll(
+ rtl_String ** newStr, rtl_String * str, char const * from,
+ sal_Int32 fromLength, char const * to, sal_Int32 toLength)
+ SAL_THROW_EXTERN_C()
+{
+ rtl_string_assign(newStr, str);
+ for (sal_Int32 i = 0;; i += toLength) {
+ rtl_string_newReplaceFirst(
+ newStr, *newStr, from, fromLength, to, toLength, &i);
+ if (i == -1) {
+ break;
+ }
+ }
+}
+
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sal/rtl/source/ustring.cxx b/sal/rtl/source/ustring.cxx
index a37353c99161..0156ceae9a08 100644
--- a/sal/rtl/source/ustring.cxx
+++ b/sal/rtl/source/ustring.cxx
@@ -25,10 +25,16 @@
* for a copy of the LGPLv3 License.
*
************************************************************************/
+
+#include "sal/config.h"
+
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
#pragma warning(disable:4738) // storing 32-bit float result in memory, possible loss of performance
#endif
+#include <cassert>
+#include <cstdlib>
+
#include <rtl/memory.h>
#include <osl/diagnose.h>
#include <osl/interlck.h>
@@ -994,4 +1000,176 @@ sal_Bool rtl_convertStringToUString(
return (sal_Bool) ((info & RTL_TEXTTOUNICODE_INFO_ERROR) == 0);
}
+void rtl_uString_newReplaceFirst(
+ rtl_uString ** newStr, rtl_uString * str, rtl_uString const * from,
+ rtl_uString const * to, sal_Int32 * index) SAL_THROW_EXTERN_C()
+{
+ assert(str != 0);
+ assert(index != 0);
+ assert(*index >= 0 && *index <= str->length);
+ assert(from != 0);
+ assert(to != 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 - to->length) {
+ std::abort();
+ }
+ sal_Int32 n = str->length - from->length + to->length;
+ rtl_uString_acquire(str); // in case *newStr == str
+ rtl_uString_new_WithLength(newStr, n);
+ if (n != 0) {
+ (*newStr)->length = n;
+ assert(i >= 0 && i < str->length);
+ rtl_copyMemory(
+ (*newStr)->buffer, str->buffer, i * sizeof (sal_Unicode));
+ rtl_copyMemory(
+ (*newStr)->buffer + i, to->buffer,
+ to->length * sizeof (sal_Unicode));
+ rtl_copyMemory(
+ (*newStr)->buffer + i + to->length,
+ str->buffer + i + from->length,
+ (str->length - i - from->length) * sizeof (sal_Unicode));
+ }
+ rtl_uString_release(str);
+ }
+ *index = i;
+}
+
+void rtl_uString_newReplaceFirstAsciiL(
+ rtl_uString ** newStr, rtl_uString * str, char const * from,
+ sal_Int32 fromLength, rtl_uString const * to, sal_Int32 * index)
+ SAL_THROW_EXTERN_C()
+{
+ assert(str != 0);
+ assert(index != 0);
+ assert(*index >= 0 && *index <= str->length);
+ assert(fromLength >= 0);
+ assert(to != 0);
+ sal_Int32 i = rtl_ustr_indexOfAscii_WithLength(
+ str->buffer + *index, str->length - *index, from, fromLength);
+ if (i == -1) {
+ rtl_uString_assign(newStr, str);
+ } else {
+ assert(i <= str->length - *index);
+ i += *index;
+ assert(fromLength <= str->length);
+ if (str->length - fromLength > SAL_MAX_INT32 - to->length) {
+ std::abort();
+ }
+ sal_Int32 n = str->length - fromLength + to->length;
+ 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);
+ rtl_copyMemory(
+ (*newStr)->buffer, str->buffer, i * sizeof (sal_Unicode));
+ rtl_copyMemory(
+ (*newStr)->buffer + i, to->buffer,
+ to->length * sizeof (sal_Unicode));
+ rtl_copyMemory(
+ (*newStr)->buffer + i + to->length,
+ str->buffer + i + fromLength,
+ (str->length - i - fromLength) * 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,
+ sal_Int32 * index) SAL_THROW_EXTERN_C()
+{
+ assert(str != 0);
+ assert(index != 0);
+ assert(*index >= 0 && *index <= str->length);
+ assert(fromLength >= 0);
+ assert(to != 0);
+ assert(toLength >= 0);
+ sal_Int32 i = rtl_ustr_indexOfAscii_WithLength(
+ str->buffer + *index, str->length - *index, from, fromLength);
+ if (i == -1) {
+ rtl_uString_assign(newStr, str);
+ } else {
+ assert(i <= str->length - *index);
+ i += *index;
+ assert(fromLength <= str->length);
+ if (str->length - fromLength > SAL_MAX_INT32 - toLength) {
+ std::abort();
+ }
+ sal_Int32 n = str->length - fromLength + 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);
+ rtl_copyMemory(
+ (*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];
+ }
+ rtl_copyMemory(
+ (*newStr)->buffer + i + toLength,
+ str->buffer + i + fromLength,
+ (str->length - i - fromLength) * sizeof (sal_Unicode));
+ }
+ rtl_uString_release(str);
+ }
+ *index = i;
+}
+
+void rtl_uString_newReplaceAll(
+ rtl_uString ** newStr, rtl_uString * str, rtl_uString const * from,
+ rtl_uString const * to) SAL_THROW_EXTERN_C()
+{
+ assert(to != 0);
+ rtl_uString_assign(newStr, str);
+ for (sal_Int32 i = 0;; i += to->length) {
+ rtl_uString_newReplaceFirst(newStr, *newStr, from, to, &i);
+ if (i == -1) {
+ break;
+ }
+ }
+}
+
+void rtl_uString_newReplaceAllAsciiL(
+ rtl_uString ** newStr, rtl_uString * str, char const * from,
+ sal_Int32 fromLength, rtl_uString const * to) SAL_THROW_EXTERN_C()
+{
+ assert(to != 0);
+ rtl_uString_assign(newStr, str);
+ for (sal_Int32 i = 0;; i += to->length) {
+ rtl_uString_newReplaceFirstAsciiL(
+ newStr, *newStr, from, fromLength, to, &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)
+ SAL_THROW_EXTERN_C()
+{
+ assert(toLength >= 0);
+ rtl_uString_assign(newStr, str);
+ for (sal_Int32 i = 0;; i += toLength) {
+ rtl_uString_newReplaceFirstAsciiLAsciiL(
+ newStr, *newStr, from, fromLength, to, toLength, &i);
+ if (i == -1) {
+ break;
+ }
+ }
+}
+
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sal/util/sal.map b/sal/util/sal.map
index 338854dabf3b..d6c3ab1fa842 100644
--- a/sal/util/sal.map
+++ b/sal/util/sal.map
@@ -612,6 +612,14 @@ LIBO_UDK_3.6 { # symbols available in >= LibO 3.6
global:
osl_unmapMappedFile;
osl_identicalDirectoryItem;
+ rtl_string_newReplaceAll;
+ rtl_string_newReplaceFirst;
+ rtl_uString_newReplaceAll;
+ rtl_uString_newReplaceAllAsciiL;
+ rtl_uString_newReplaceAllAsciiLAsciiL;
+ rtl_uString_newReplaceFirst;
+ rtl_uString_newReplaceFirstAsciiL;
+ rtl_uString_newReplaceFirstAsciiLAsciiL;
} UDK_3.10;
PRIVATE_1.0 {