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 `<algorithm>` header. This also removes the respective unit test. More importantly, this removes the dependency on the `<boost/algorithm/string.hpp>` header in the `string_util.hpp` header.
This commit is contained in:
parent
397078758e
commit
468c01056f
@ -28,14 +28,13 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
#ifndef OBJECT_ENCODER_HPP
|
#ifndef OBJECT_ENCODER_HPP
|
||||||
#define OBJECT_ENCODER_HPP
|
#define OBJECT_ENCODER_HPP
|
||||||
|
|
||||||
#include "../util/string_util.hpp"
|
|
||||||
|
|
||||||
#include <boost/assert.hpp>
|
#include <boost/assert.hpp>
|
||||||
#include <boost/archive/iterators/base64_from_binary.hpp>
|
#include <boost/archive/iterators/base64_from_binary.hpp>
|
||||||
#include <boost/archive/iterators/binary_from_base64.hpp>
|
#include <boost/archive/iterators/binary_from_base64.hpp>
|
||||||
#include <boost/archive/iterators/transform_width.hpp>
|
#include <boost/archive/iterators/transform_width.hpp>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <iterator>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -66,8 +65,8 @@ struct ObjectEncoder
|
|||||||
encoded.resize(sizeof(ObjectT));
|
encoded.resize(sizeof(ObjectT));
|
||||||
encoded.assign(base64_t(&data[0]),
|
encoded.assign(base64_t(&data[0]),
|
||||||
base64_t(&data[0] + (data.size() - number_of_padded_chars)));
|
base64_t(&data[0] + (data.size() - number_of_padded_chars)));
|
||||||
replaceAll(encoded, "+", "-");
|
std::replace(begin(encoded), end(encoded), '+', '-');
|
||||||
replaceAll(encoded, "/", "_");
|
std::replace(begin(encoded), end(encoded), '/', '_');
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class ObjectT> static void DecodeFromBase64(const std::string &input, ObjectT &object)
|
template <class ObjectT> static void DecodeFromBase64(const std::string &input, ObjectT &object)
|
||||||
@ -75,9 +74,8 @@ struct ObjectEncoder
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::string encoded(input);
|
std::string encoded(input);
|
||||||
// replace "-" with "+" and "_" with "/"
|
std::replace(begin(encoded), end(encoded), '-', '+');
|
||||||
replaceAll(encoded, "-", "+");
|
std::replace(begin(encoded), end(encoded), '_', '/');
|
||||||
replaceAll(encoded, "_", "/");
|
|
||||||
|
|
||||||
std::copy(binary_t(encoded.begin()), binary_t(encoded.begin() + encoded.length()),
|
std::copy(binary_t(encoded.begin()), binary_t(encoded.begin() + encoded.length()),
|
||||||
reinterpret_cast<char *>(&object));
|
reinterpret_cast<char *>(&object));
|
||||||
|
@ -34,16 +34,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE(string_util)
|
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)
|
BOOST_AUTO_TEST_CASE(json_escaping)
|
||||||
{
|
{
|
||||||
std::string input{"\b\\"};
|
std::string input{"\b\\"};
|
||||||
|
@ -28,8 +28,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
#ifndef STRING_UTIL_HPP
|
#ifndef STRING_UTIL_HPP
|
||||||
#define STRING_UTIL_HPP
|
#define STRING_UTIL_HPP
|
||||||
|
|
||||||
#include <boost/algorithm/string.hpp>
|
|
||||||
|
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
|
|
||||||
#include <random>
|
#include <random>
|
||||||
@ -83,11 +81,6 @@ template <int length, int precision> static inline char *printInt(char *buffer,
|
|||||||
return 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)
|
inline std::string escape_JSON(const std::string &input)
|
||||||
{
|
{
|
||||||
// escape and skip reallocations if possible
|
// escape and skip reallocations if possible
|
||||||
|
Loading…
Reference in New Issue
Block a user