From b3917aaadf02dce193c58ebd4bcde05a73a42eb4 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Tue, 16 Apr 2019 20:54:29 +0900 Subject: [PATCH] refactor: use snprintf to show char in hex instead of std::ostringstream. --- toml/combinator.hpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/toml/combinator.hpp b/toml/combinator.hpp index ac8dd14..52aaaae 100644 --- a/toml/combinator.hpp +++ b/toml/combinator.hpp @@ -9,7 +9,10 @@ #include #include #include +#include #include +#include +#include #include // they scans characters and returns region if it matches to the condition. @@ -38,10 +41,12 @@ inline std::string show_char(const char c) } else { - std::ostringstream oss; - oss << "0x" << std::hex << std::setfill('0') << std::setw(2) - << static_cast(c); - return oss.str(); + std::array buf; + buf.fill('\0'); + const auto r = std::snprintf( + buf.data(), buf.size(), "0x%02x", static_cast(c) & 0xFF); + assert(r == buf.size() - 1); + return std::string(buf.data()); } }