Add string
This commit is contained in:
parent
a9c72e393a
commit
cb0af6b863
@ -122,21 +122,35 @@ template <> void Renderer<std::vector<char>>::write(const char *str)
|
|||||||
{
|
{
|
||||||
out.insert(out.end(), str, str + strlen(str));
|
out.insert(out.end(), str, str + strlen(str));
|
||||||
}
|
}
|
||||||
|
template <> void Renderer<std::vector<char>>::write(char ch) { out.push_back(ch); }
|
||||||
|
|
||||||
|
|
||||||
template <> void Renderer<std::ostream>::write(const std::string &str) { out << str; }
|
template <> void Renderer<std::ostream>::write(const std::string &str) { out << str; }
|
||||||
|
|
||||||
template <> void Renderer<std::ostream>::write(const char *str) { out << str; }
|
template <> void Renderer<std::ostream>::write(const char *str) { out << str; }
|
||||||
|
|
||||||
template <> void Renderer<std::vector<char>>::write(char ch) { out.push_back(ch); }
|
|
||||||
|
|
||||||
template <> void Renderer<std::ostream>::write(char ch) { out << ch; }
|
template <> void Renderer<std::ostream>::write(char ch) { out << ch; }
|
||||||
|
|
||||||
|
|
||||||
|
template <> void Renderer<std::string>::write(const std::string &str) { out += str; }
|
||||||
|
|
||||||
|
template <> void Renderer<std::string>::write(const char *str) { out += str; }
|
||||||
|
|
||||||
|
template <> void Renderer<std::string>::write(char ch) { out += ch; }
|
||||||
|
|
||||||
|
|
||||||
inline void render(std::ostream &out, const Object &object)
|
inline void render(std::ostream &out, const Object &object)
|
||||||
{
|
{
|
||||||
Value value = object;
|
Value value = object;
|
||||||
mapbox::util::apply_visitor(Renderer(out), value);
|
mapbox::util::apply_visitor(Renderer(out), value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void render(std::string &out, const Object &object)
|
||||||
|
{
|
||||||
|
Value value = object;
|
||||||
|
mapbox::util::apply_visitor(Renderer(out), value);
|
||||||
|
}
|
||||||
|
|
||||||
inline void render(std::vector<char> &out, const Object &object)
|
inline void render(std::vector<char> &out, const Object &object)
|
||||||
{
|
{
|
||||||
Value value = object;
|
Value value = object;
|
||||||
|
|||||||
@ -6,6 +6,7 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
// #ifdef _WIN32
|
// #ifdef _WIN32
|
||||||
// #pragma optimize("", off)
|
// #pragma optimize("", off)
|
||||||
@ -130,17 +131,28 @@ int main(int, char **)
|
|||||||
}
|
}
|
||||||
json::Object obj{{{"arr", arr}}};
|
json::Object obj{{{"arr", arr}}};
|
||||||
|
|
||||||
|
TIMER_START(string);
|
||||||
|
std::string out_str;
|
||||||
|
json::render(out_str, obj);
|
||||||
|
TIMER_STOP(string);
|
||||||
|
std::cout << "String: " << TIMER_MSEC(string) << "ms" << std::endl;
|
||||||
|
|
||||||
TIMER_START(stringstream);
|
TIMER_START(stringstream);
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
json::render(ss, obj);
|
json::render(ss, obj);
|
||||||
std::string s{ss.str()};
|
std::string out_ss_str{ss.str()};
|
||||||
TIMER_STOP(stringstream);
|
TIMER_STOP(stringstream);
|
||||||
std::cout << "String: " << TIMER_MSEC(stringstream) << "ms" << std::endl;
|
|
||||||
|
std::cout << "Stringstream: " << TIMER_MSEC(stringstream) << "ms" << std::endl;
|
||||||
TIMER_START(vector);
|
TIMER_START(vector);
|
||||||
std::vector<char> vec;
|
std::vector<char> out_vec;
|
||||||
json::render(vec, obj);
|
json::render(out_vec, obj);
|
||||||
TIMER_STOP(vector);
|
TIMER_STOP(vector);
|
||||||
std::cout << "Vector: " << TIMER_MSEC(vector) << "ms" << std::endl;
|
std::cout << "Vector: " << TIMER_MSEC(vector) << "ms" << std::endl;
|
||||||
|
|
||||||
|
if (std::string{out_vec.begin(), out_vec.end()} != out_str || out_str != out_ss_str) {
|
||||||
|
throw std::logic_error("Vector and string are not equal");
|
||||||
|
}
|
||||||
// (void)s;
|
// (void)s;
|
||||||
// std::cerr << ss << "\n";
|
// std::cerr << ss << "\n";
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user