From 91250fcd5487307793ec5b495bbbc91eed888758 Mon Sep 17 00:00:00 2001 From: Peter Thorson Date: Sat, 20 Apr 2013 08:56:19 -0500 Subject: [PATCH] adds a string_replace_all utility function --- test/utility/utilities.cpp | 8 ++++++++ websocketpp/impl/utilities_impl.hpp | 11 +++++++++++ websocketpp/utilities.hpp | 6 ++---- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/test/utility/utilities.cpp b/test/utility/utilities.cpp index 5dc394e65f..3dd78fba2b 100644 --- a/test/utility/utilities.cpp +++ b/test/utility/utilities.cpp @@ -56,4 +56,12 @@ BOOST_AUTO_TEST_CASE( substr_not_found ) { BOOST_CHECK(websocketpp::utility::ci_find_substr(haystack,needle) == haystack.end()); } +BOOST_AUTO_TEST_CASE( string_replace_all ) { + std::string source = "foo \"bar\" baz"; + std::string dest = "foo \\\"bar\\\" baz"; + + using websocketpp::utility::string_replace_all; + BOOST_CHECK_EQUAL(string_replace_all(source,"\"","\\\""),dest); +} + BOOST_AUTO_TEST_SUITE_END() diff --git a/websocketpp/impl/utilities_impl.hpp b/websocketpp/impl/utilities_impl.hpp index 0762d26e00..b3ff1a0858 100644 --- a/websocketpp/impl/utilities_impl.hpp +++ b/websocketpp/impl/utilities_impl.hpp @@ -61,6 +61,17 @@ inline std::string to_hex(const char* input,size_t length) { return to_hex(reinterpret_cast(input),length); } +inline std::string string_replace_all(std::string subject, const std::string& + search, const std::string& replace) +{ + size_t pos = 0; + while((pos = subject.find(search, pos)) != std::string::npos) { + subject.replace(pos, search.length(), replace); + pos += replace.length(); + } + return subject; +} + } // namespace utility } // namespace websocketpp diff --git a/websocketpp/utilities.hpp b/websocketpp/utilities.hpp index 15687954f6..991ab3dcce 100644 --- a/websocketpp/utilities.hpp +++ b/websocketpp/utilities.hpp @@ -68,10 +68,8 @@ typename T::const_iterator ci_find_substr(const T& str1, -/// Host to network long long -//uint64_t htonll(uint64_t src); -/// Network to host long long -//uint64_t ntohll(uint64_t src); +std::string string_replace_all(std::string subject, const std::string& search, + const std::string& replace); /// Convert std::string to ascii printed string of hex digits std::string to_hex(const std::string& input);