From 468c01056f05a279986c67504f8a3887f65b27c6 Mon Sep 17 00:00:00 2001 From: "Daniel J. Hofmann" Date: Wed, 2 Sep 2015 20:09:53 +0200 Subject: [PATCH] Replace custom replace utility with the stdlib's replace algorithm. This removes the custom `replaceAll` function, replacing it with `std::replace` from the stdlib's `` header. This also removes the respective unit test. More importantly, this removes the dependency on the `` header in the `string_util.hpp` header. --- algorithms/object_encoder.hpp | 12 +++++------- unit_tests/algorithms/string_util.cpp | 10 ---------- util/string_util.hpp | 7 ------- 3 files changed, 5 insertions(+), 24 deletions(-) diff --git a/algorithms/object_encoder.hpp b/algorithms/object_encoder.hpp index 3ffac410d..581b3967c 100644 --- a/algorithms/object_encoder.hpp +++ b/algorithms/object_encoder.hpp @@ -28,14 +28,13 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef OBJECT_ENCODER_HPP #define OBJECT_ENCODER_HPP -#include "../util/string_util.hpp" - #include #include #include #include #include +#include #include #include @@ -66,8 +65,8 @@ struct ObjectEncoder encoded.resize(sizeof(ObjectT)); encoded.assign(base64_t(&data[0]), base64_t(&data[0] + (data.size() - number_of_padded_chars))); - replaceAll(encoded, "+", "-"); - replaceAll(encoded, "/", "_"); + std::replace(begin(encoded), end(encoded), '+', '-'); + std::replace(begin(encoded), end(encoded), '/', '_'); } template static void DecodeFromBase64(const std::string &input, ObjectT &object) @@ -75,9 +74,8 @@ struct ObjectEncoder try { std::string encoded(input); - // replace "-" with "+" and "_" with "/" - replaceAll(encoded, "-", "+"); - replaceAll(encoded, "_", "/"); + std::replace(begin(encoded), end(encoded), '-', '+'); + std::replace(begin(encoded), end(encoded), '_', '/'); std::copy(binary_t(encoded.begin()), binary_t(encoded.begin() + encoded.length()), reinterpret_cast(&object)); diff --git a/unit_tests/algorithms/string_util.cpp b/unit_tests/algorithms/string_util.cpp index 865ddd5a2..cf82eba5b 100644 --- a/unit_tests/algorithms/string_util.cpp +++ b/unit_tests/algorithms/string_util.cpp @@ -34,16 +34,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. BOOST_AUTO_TEST_SUITE(string_util) -BOOST_AUTO_TEST_CASE(replace_all_test) -{ - std::string input{"ababababababa"}; - const std::string sub{"a"}; - const std::string other{"c"}; - replaceAll(input, sub, other); - - BOOST_CHECK_EQUAL(input, "cbcbcbcbcbcbc"); -} - BOOST_AUTO_TEST_CASE(json_escaping) { std::string input{"\b\\"}; diff --git a/util/string_util.hpp b/util/string_util.hpp index 6e80a4e05..8075e659c 100644 --- a/util/string_util.hpp +++ b/util/string_util.hpp @@ -28,8 +28,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef STRING_UTIL_HPP #define STRING_UTIL_HPP -#include - #include #include @@ -83,11 +81,6 @@ template static inline char *printInt(char *buffer, return buffer; } -inline void replaceAll(std::string &s, const std::string &sub, const std::string &other) -{ - boost::replace_all(s, sub, other); -} - inline std::string escape_JSON(const std::string &input) { // escape and skip reallocations if possible