adds a string_replace_all utility function

This commit is contained in:
Peter Thorson
2013-04-20 08:56:19 -05:00
parent 466c9bd626
commit 91250fcd54
3 changed files with 21 additions and 4 deletions

View File

@@ -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()

View File

@@ -61,6 +61,17 @@ inline std::string to_hex(const char* input,size_t length) {
return to_hex(reinterpret_cast<const uint8_t*>(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

View File

@@ -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);