From 1304746bce46cfbef1b486ac727daaf4292d445d Mon Sep 17 00:00:00 2001 From: Francisco Paisana Date: Tue, 15 Dec 2020 11:31:10 +0000 Subject: [PATCH] use of fmt lib in asn1 utils --- lib/include/srslte/asn1/asn1_utils.h | 11 +++++----- lib/src/asn1/asn1_utils.cc | 31 ++++++++++++++-------------- srsenb/hdr/parser.h | 1 + 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/lib/include/srslte/asn1/asn1_utils.h b/lib/include/srslte/asn1/asn1_utils.h index 67c5ae166..c4757f416 100644 --- a/lib/include/srslte/asn1/asn1_utils.h +++ b/lib/include/srslte/asn1/asn1_utils.h @@ -13,14 +13,13 @@ #ifndef SRSASN_COMMON_UTILS_H #define SRSASN_COMMON_UTILS_H +#include "srslte/srslog/bundled/fmt/format.h" #include #include #include #include #include #include -#include -#include /* va_list, va_start, va_arg, va_end */ #include #include #include @@ -1301,6 +1300,8 @@ private: JsonWriter *******************/ +using json_buffer = fmt::basic_memory_buffer; + class json_writer { public: @@ -1321,9 +1322,9 @@ public: std::string to_string() const; private: - std::stringstream ss; - std::string ident; - enum separator_t { COMMA, NEWLINE, NONE }; + json_buffer buffer; + std::string ident; + enum separator_t { COMMA = 0, NEWLINE, NONE }; separator_t sep; }; diff --git a/lib/src/asn1/asn1_utils.cc b/lib/src/asn1/asn1_utils.cc index b08ade621..92b61a46e 100644 --- a/lib/src/asn1/asn1_utils.cc +++ b/lib/src/asn1/asn1_utils.cc @@ -12,7 +12,9 @@ #include "srslte/asn1/asn1_utils.h" #include "srslte/common/logmap.h" +#include "srslte/srslog/bundled/fmt/core.h" #include +#include /* va_list, va_start, va_arg, va_end */ #include namespace asn1 { @@ -1513,13 +1515,11 @@ json_writer::json_writer() : ident(""), sep(NONE) {} void json_writer::write_fieldname(const std::string& fieldname) { - if (sep == COMMA) { - ss << ",\n" << ident; - } else if (sep == NEWLINE) { - ss << "\n" << ident; - } + constexpr static const char* septable[] = {",\n", "\n", ""}; + + fmt::format_to(buffer, "{}{}", septable[sep], sep != NONE ? ident : ""); if (not fieldname.empty()) { - ss << "\"" << fieldname << "\": "; + fmt::format_to(buffer, "\"{}\": ", fieldname); } sep = NONE; } @@ -1527,7 +1527,7 @@ void json_writer::write_fieldname(const std::string& fieldname) void json_writer::write_str(const std::string& fieldname, const std::string& value) { write_fieldname(fieldname); - ss << "\"" << value << "\""; + fmt::format_to(buffer, "\"{}\"", value); sep = COMMA; } void json_writer::write_str(const std::string& value) @@ -1538,7 +1538,7 @@ void json_writer::write_str(const std::string& value) void json_writer::write_int(const std::string& fieldname, int64_t value) { write_fieldname(fieldname); - ss << value; + fmt::format_to(buffer, "{}", value); sep = COMMA; } void json_writer::write_int(int64_t value) @@ -1549,7 +1549,7 @@ void json_writer::write_int(int64_t value) void json_writer::write_bool(const std::string& fieldname, bool value) { write_fieldname(fieldname); - ss << (value ? "true" : "false"); + fmt::format_to(buffer, "{}", value ? "true" : "false"); sep = COMMA; } void json_writer::write_bool(bool value) @@ -1560,7 +1560,7 @@ void json_writer::write_bool(bool value) void json_writer::write_null(const std::string& fieldname) { write_fieldname(fieldname); - ss << "null"; + fmt::format_to(buffer, "null"); sep = COMMA; } void json_writer::write_null() @@ -1571,33 +1571,34 @@ void json_writer::write_null() void json_writer::start_obj(const std::string& fieldname) { write_fieldname(fieldname); - ss << "{"; + fmt::format_to(buffer, "{{"); ident += " "; sep = NEWLINE; } void json_writer::end_obj() { ident.erase(ident.size() - 2, 2); - ss << "\n" << ident << "}"; + fmt::format_to(buffer, "\n{}}}", ident); sep = COMMA; } + void json_writer::start_array(const std::string& fieldname) { write_fieldname(fieldname); - ss << "["; + fmt::format_to(buffer, "["); ident += " "; sep = NEWLINE; } void json_writer::end_array() { ident.erase(ident.size() - 2, 2); - ss << "\n" << ident << "]"; + fmt::format_to(buffer, "\n{}]", ident); sep = COMMA; } std::string json_writer::to_string() const { - return ss.str(); + return std::string(buffer.data(), buffer.size()); } } // namespace asn1 diff --git a/srsenb/hdr/parser.h b/srsenb/hdr/parser.h index c75c0dc67..cb9e43563 100644 --- a/srsenb/hdr/parser.h +++ b/srsenb/hdr/parser.h @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include