common/Util.cpp | 16 +++++++++++----- common/Util.hpp | 2 +- test/WhiteBoxTests.cpp | 12 ++++++++++++ 3 files changed, 24 insertions(+), 6 deletions(-)
New commits: commit 484fcd63a52807637c3c34ee877801fab11c8505 Author: Ashod Nakashian <[email protected]> Date: Sun May 14 21:44:16 2017 -0400 wsd: corrections to replace and unittests Change-Id: I0e03deaa710ba722a97c9f857b514f8ecb0e1682 Reviewed-on: https://gerrit.libreoffice.org/37609 Reviewed-by: Ashod Nakashian <[email protected]> Tested-by: Ashod Nakashian <[email protected]> diff --git a/common/Util.cpp b/common/Util.cpp index 612f971e..803ebfb0 100644 --- a/common/Util.cpp +++ b/common/Util.cpp @@ -242,14 +242,20 @@ namespace Util return 0; } - std::string replace(const std::string& s, const std::string& a, const std::string& b) + std::string replace(std::string result, const std::string& a, const std::string& b) { - std::string result = s; - std::string::size_type pos; - while ((pos = result.find(a)) != std::string::npos) + const size_t aSize = a.size(); + if (aSize > 0) { - result = result.replace(pos, a.size(), b); + const size_t bSize = b.size(); + std::string::size_type pos = 0; + while ((pos = result.find(a, pos)) != std::string::npos) + { + result = result.replace(pos, aSize, b); + pos += bSize; // Skip the replacee to avoid endless recursion. + } } + return result; } diff --git a/common/Util.hpp b/common/Util.hpp index 63bffc97..d2803a42 100644 --- a/common/Util.hpp +++ b/common/Util.hpp @@ -103,7 +103,7 @@ namespace Util /// Example: "procmemstats: pid=123 rss=12400 pss=566" std::string getMemoryStats(FILE* file); - std::string replace(const std::string& s, const std::string& a, const std::string& b); + std::string replace(std::string s, const std::string& a, const std::string& b); std::string formatLinesForLog(const std::string& s); diff --git a/test/WhiteBoxTests.cpp b/test/WhiteBoxTests.cpp index 7ddba7d2..effb7659 100644 --- a/test/WhiteBoxTests.cpp +++ b/test/WhiteBoxTests.cpp @@ -27,6 +27,7 @@ class WhiteBoxTests : public CPPUNIT_NS::TestFixture CPPUNIT_TEST(testLOOLProtocolFunctions); CPPUNIT_TEST(testMessageAbbreviation); CPPUNIT_TEST(testTokenizer); + CPPUNIT_TEST(testReplace); CPPUNIT_TEST(testRegexListMatcher); CPPUNIT_TEST(testRegexListMatcher_Init); CPPUNIT_TEST(testEmptyCellCursor); @@ -37,6 +38,7 @@ class WhiteBoxTests : public CPPUNIT_NS::TestFixture void testLOOLProtocolFunctions(); void testMessageAbbreviation(); void testTokenizer(); + void testReplace(); void testRegexListMatcher(); void testRegexListMatcher_Init(); void testEmptyCellCursor(); @@ -218,6 +220,16 @@ void WhiteBoxTests::testTokenizer() CPPUNIT_ASSERT_EQUAL(std::string("ver=-1"), tokens[8]); } +void WhiteBoxTests::testReplace() +{ + CPPUNIT_ASSERT_EQUAL(std::string("zesz one zwo flee"), Util::replace("test one two flee", "t", "z")); + CPPUNIT_ASSERT_EQUAL(std::string("testt one two flee"), Util::replace("test one two flee", "tes", "test")); + CPPUNIT_ASSERT_EQUAL(std::string("testest one two flee"), Util::replace("test one two flee", "tes", "testes")); + CPPUNIT_ASSERT_EQUAL(std::string("tete one two flee"), Util::replace("tettet one two flee", "tet", "te")); + CPPUNIT_ASSERT_EQUAL(std::string("t one two flee"), Util::replace("test one two flee", "tes", "")); + CPPUNIT_ASSERT_EQUAL(std::string("test one two flee"), Util::replace("test one two flee", "", "X")); +} + void WhiteBoxTests::testRegexListMatcher() { Util::RegexListMatcher matcher; _______________________________________________ Libreoffice-commits mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits
