Use template versions of to_string()

This commit is contained in:
Moritz Bunkus 2012-08-17 22:46:24 +02:00
parent 8c16bc3fbb
commit c2fe0e1dec
2 changed files with 28 additions and 39 deletions

View File

@ -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<int64_t>(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;
}

View File

@ -17,6 +17,7 @@
#include "common/common_pch.h"
#include <ostream>
#include <sstream>
#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<T> const &timecode) {
}
template<typename T>
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,