From c2fe0e1dec423d172501589de3f201b02967f0e3 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Fri, 17 Aug 2012 22:46:24 +0200 Subject: [PATCH] Use template versions of to_string() --- src/common/strings/formatting.cpp | 49 +++++++++---------------------- src/common/strings/formatting.h | 18 +++++++++--- 2 files changed, 28 insertions(+), 39 deletions(-) diff --git a/src/common/strings/formatting.cpp b/src/common/strings/formatting.cpp index 168fbe63c..99f8287be 100644 --- a/src/common/strings/formatting.cpp +++ b/src/common/strings/formatting.cpp @@ -50,28 +50,6 @@ format_timecode(int64_t timecode, return result; } -static boost::format s_bf_single_value("%1%"); - -std::string -to_string(int64_t value) { - return (s_bf_single_value % value).str(); -} - -std::string -to_string(int value) { - return (s_bf_single_value % value).str(); -} - -std::string -to_string(uint64_t value) { - return (s_bf_single_value % value).str(); -} - -std::string -to_string(unsigned int value) { - return (s_bf_single_value % value).str(); -} - std::string to_string(double value, unsigned int precision) { @@ -79,30 +57,31 @@ to_string(double value, for (size_t i = 0; i < precision; ++i) scale *= 10; - return to_string((int64_t)(value * scale), scale, precision); + return to_string(static_cast(value * scale), scale, precision); } std::string to_string(int64_t numerator, int64_t denominator, unsigned int precision) { - std::string output = (s_bf_single_value % (numerator / denominator)).str(); + std::string output = to_string(numerator / denominator); int64_t fractional_part = numerator % denominator; - if (0 != fractional_part) { - static boost::format s_bf_precision_format_format(".%%0%1%d"); + if (0 == fractional_part) + return output; - std::string format = (s_bf_precision_format_format % precision).str(); - output += (boost::format(format) % fractional_part).str(); - std::string::iterator end = output.end() - 1; + static boost::format s_bf_precision_format_format(".%%0%1%d"); - while (*end == '0') - --end; - if (*end == '.') - --end; + std::string format = (s_bf_precision_format_format % precision).str(); + output += (boost::format(format) % fractional_part).str(); + std::string::iterator end = output.end() - 1; - output.erase(end + 1, output.end()); - } + while (*end == '0') + --end; + if (*end == '.') + --end; + + output.erase(end + 1, output.end()); return output; } diff --git a/src/common/strings/formatting.h b/src/common/strings/formatting.h index ed22ad8c3..437d790ff 100644 --- a/src/common/strings/formatting.h +++ b/src/common/strings/formatting.h @@ -17,6 +17,7 @@ #include "common/common_pch.h" #include +#include #include "common/strings/editing.h" #include "common/timecode.h" @@ -73,10 +74,11 @@ std::wstring format_paragraph(const std::wstring &text_to_wrap, void fix_format(const char *fmt, std::string &new_fmt); -std::string to_string(int value); -std::string to_string(unsigned int value); -std::string to_string(int64_t value); -std::string to_string(uint64_t value); +inline std::string +to_string(std::string const &value) { + return value; +} + std::string to_string(double value, unsigned int precision); std::string to_string(int64_t numerator, int64_t denominator, unsigned int precision); @@ -87,6 +89,14 @@ to_string(basic_timecode_c const &timecode) { } +template +std::string +to_string(T const &value) { + std::stringstream ss; + ss << value; + return ss.str(); +} + std::string to_hex(const unsigned char *buf, size_t size, bool compact = false); inline std::string to_hex(const std::string &buf,