From d7d953d40a9b1e22ee04c2bd8ca5784fabd611b1 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Tue, 18 Oct 2016 06:22:44 +0200 Subject: [PATCH] fix(string_util): Proper squeezing --- include/utils/string.hpp | 5 ++++- tests/utils/test_string.cpp | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/utils/string.hpp b/include/utils/string.hpp index 7cd80f05..4b584831 100644 --- a/include/utils/string.hpp +++ b/include/utils/string.hpp @@ -55,7 +55,10 @@ namespace string_util { * Replace all occurences of needle in haystack */ inline auto replace_all(const string& haystack, string needle, string replacement) { - return boost::replace_all_copy(haystack, needle, replacement); + auto result = haystack; + while (result.find(needle) != string::npos) + result = replace(result, needle, replacement); + return result; } /** diff --git a/tests/utils/test_string.cpp b/tests/utils/test_string.cpp index 5bcb98a3..26310719 100644 --- a/tests/utils/test_string.cpp +++ b/tests/utils/test_string.cpp @@ -46,7 +46,8 @@ class test_string : public unit_test { } void test_squeeze() { - CPPUNIT_ASSERT_EQUAL(string{"Squeeeze"}, string_util::squeeze("Squeeeeeze", 'e')); + CPPUNIT_ASSERT_EQUAL(string{"Squeze"}, string_util::squeeze("Squeeeeeze", 'e')); + CPPUNIT_ASSERT_EQUAL(string{"bar baz foobar"}, string_util::squeeze("bar baz foobar", ' ')); } void test_strip() {