[Scummvm-git-logs] scummvm master -> 0c9e17c57c10cb58d5d0b372b41992663103e448
sev-
noreply at scummvm.org
Mon Feb 27 21:01:19 UTC 2023
This automated email contains information about 2 new commits which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .
Summary:
cc1e8600e2 COMMON: Add replace() methods for U32String
0c9e17c57c TESTS: Add U32String::replace() tests
Commit: cc1e8600e24d4478143f3f0da8b5d845df5d581b
https://github.com/scummvm/scummvm/commit/cc1e8600e24d4478143f3f0da8b5d845df5d581b
Author: hax0kartik (agarwala.kartik at gmail.com)
Date: 2023-02-27T22:01:13+01:00
Commit Message:
COMMON: Add replace() methods for U32String
Changed paths:
common/ustr.cpp
common/ustr.h
diff --git a/common/ustr.cpp b/common/ustr.cpp
index 6e70b0598d7..184a0cacfef 100644
--- a/common/ustr.cpp
+++ b/common/ustr.cpp
@@ -252,6 +252,55 @@ int U32String::vformat(const value_type *fmt, const value_type *fmtEnd, U32Strin
return length;
}
+void U32String::replace(uint32 pos, uint32 count, const U32String &str) {
+ replace(pos, count, str, 0, str._size);
+}
+
+void U32String::replace(iterator begin_, iterator end_, const U32String &str) {
+ replace(begin_ - _str, end_ - begin_, str._str, 0, str._size);
+}
+
+void U32String::replace(uint32 posOri, uint32 countOri, const U32String &str,
+ uint32 posDest, uint32 countDest) {
+ replace(posOri, countOri, str._str, posDest, countDest);
+}
+
+void U32String::replace(uint32 posOri, uint32 countOri, const u32char_type_t *str,
+ uint32 posDest, uint32 countDest) {
+
+ // Prepare string for the replaced text.
+ if (countOri < countDest) {
+ uint32 offset = countDest - countOri; ///< Offset to copy the characters
+ uint32 newSize = _size + offset;
+
+ ensureCapacity(newSize, true);
+
+ _size = newSize;
+
+ // Push the old characters to the end of the string
+ for (uint32 i = _size; i >= posOri + countDest; i--)
+ _str[i] = _str[i - offset];
+
+ } else if (countOri > countDest) {
+ uint32 offset = countOri - countDest; ///< Number of positions that we have to pull back
+
+ makeUnique();
+
+ // Pull the remainder string back
+ for (uint32 i = posOri + countDest; i + offset <= _size; i++)
+ _str[i] = _str[i + offset];
+
+ _size -= offset;
+ } else {
+ makeUnique();
+ }
+
+ // Copy the replaced part of the string
+ for (uint32 i = 0; i < countDest; i++)
+ _str[posOri + i] = str[posDest + i];
+
+}
+
char* U32String::itoa(int num, char* str, uint base) {
if (num < 0) {
str[0] = '-';
diff --git a/common/ustr.h b/common/ustr.h
index 66a8c073669..f04beb03f55 100644
--- a/common/ustr.h
+++ b/common/ustr.h
@@ -185,6 +185,34 @@ public:
/** Transform a U32String into UTF-16 representation (native encoding). The result must be freed. */
uint16 *encodeUTF16Native(uint *len = nullptr) const;
+ /**@{
+ * Functions to replace some amount of chars with chars from some other string.
+ *
+ * @note The implementation follows that of the STL's std::string:
+ * http://www.cplusplus.com/reference/string/string/replace/
+ *
+ * @param pos Starting position for the replace in the original string.
+ * @param count Number of chars to replace from the original string.
+ * @param str Source of the new chars.
+ * @param posOri Same as pos
+ * @param countOri Same as count
+ * @param posDest Initial position to read str from.
+ * @param countDest Number of chars to read from str. npos by default.
+ */
+ // Replace 'count' bytes, starting from 'pos' with str.
+ void replace(uint32 pos, uint32 count, const U32String &str);
+ // Replace the characters in [begin, end) with str._str.
+ void replace(iterator begin, iterator end, const U32String &str);
+ // Replace _str[posOri, posOri + countOri) with
+ // str._str[posDest, posDest + countDest)
+ void replace(uint32 posOri, uint32 countOri, const U32String &str,
+ uint32 posDest, uint32 countDest);
+ // Replace _str[posOri, posOri + countOri) with
+ // str[posDest, posDest + countDest)
+ void replace(uint32 posOri, uint32 countOri, const u32char_type_t *str,
+ uint32 posDest, uint32 countDest);
+ /**@}*/
+
private:
static U32String formatInternal(const U32String *fmt, ...);
Commit: 0c9e17c57c10cb58d5d0b372b41992663103e448
https://github.com/scummvm/scummvm/commit/0c9e17c57c10cb58d5d0b372b41992663103e448
Author: hax0kartik (agarwala.kartik at gmail.com)
Date: 2023-02-27T22:01:13+01:00
Commit Message:
TESTS: Add U32String::replace() tests
Changed paths:
test/common/str.h
diff --git a/test/common/str.h b/test/common/str.h
index a3952927659..6827ec91e94 100644
--- a/test/common/str.h
+++ b/test/common/str.h
@@ -570,6 +570,64 @@ class StringTestSuite : public CxxTest::TestSuite
TS_ASSERT_EQUALS(s4, "TestTestTestTestTestTestTestTestTestTestTest");
}
+ void test_ustring_replace() {
+ // These tests are inspired from the above string tests
+
+ // --------------------------
+ // Tests without displacement
+ // --------------------------
+ Common::U32String testString = Common::U32String("This is the original string.");
+
+ // Positions and sizes as parameters, U32String as replacement
+ testString.replace(12, 8, Common::U32String("newnewne"));
+ TS_ASSERT_EQUALS(testString, Common::U32String("This is the newnewne string."));
+
+ // Using iterators (also a terribly useless program as a test).
+ testString.replace(testString.begin(), testString.end(), Common::U32String("That is the supernew string."));
+ TS_ASSERT_EQUALS(testString, Common::U32String("That is the supernew string."));
+
+ // With substrings.
+ testString.replace(12, 2, Common::U32String("That hy is new."), 5, 2);
+ TS_ASSERT_EQUALS(testString, Common::U32String("That is the hypernew string."));
+
+ // --------------------------
+ // Tests with displacement
+ // --------------------------
+ testString = Common::U32String("Hello World");
+
+ // Positions and sizes as parameters, string as replacement
+ testString.replace(6, 5, Common::U32String("friends"));
+ TS_ASSERT_EQUALS(testString, Common::U32String("Hello friends"));
+
+ // Using iterators (also a terribly useless program as a test)
+ testString.replace(testString.begin() + 5, testString.begin() + 6, Common::U32String(" good "));
+ TS_ASSERT_EQUALS(testString, Common::U32String("Hello good friends"));
+
+ // With substrings
+ testString.replace(6, 0, Common::U32String("Displaced my string"), 10, 3);
+ TS_ASSERT_EQUALS(testString, Common::U32String("Hello my good friends"));
+
+ // -----------------------
+ // Deep copy compliance
+ // -----------------------
+
+ // Makes a deep copy without changing the length of the original
+ Common::U32String s1 = Common::U32String("TestTestTestTestTestTestTestTestTestTestTest");
+ Common::U32String s2(s1);
+ TS_ASSERT_EQUALS(s1, "TestTestTestTestTestTestTestTestTestTestTest");
+ TS_ASSERT_EQUALS(s2, "TestTestTestTestTestTestTestTestTestTestTest");
+ s1.replace(0, 4, Common::U32String("TEST"));
+ TS_ASSERT_EQUALS(s1, "TESTTestTestTestTestTestTestTestTestTestTest");
+ TS_ASSERT_EQUALS(s2, "TestTestTestTestTestTestTestTestTestTestTest");
+
+ // Makes a deep copy when we shorten the string
+ Common::String s3 = "TestTestTestTestTestTestTestTestTestTestTest";
+ Common::String s4(s3);
+ s3.replace(0, 32, Common::U32String(""));
+ TS_ASSERT_EQUALS(s3, "TestTestTest");
+ TS_ASSERT_EQUALS(s4, "TestTestTestTestTestTestTestTestTestTestTest");
+ }
+
void test_find() {
Common::String a("0123012"), b;
More information about the Scummvm-git-logs
mailing list