16 const char kBigSIUnits[] =
"kMGTPEZY";
18 const char kBigIECUnits[] =
"KMGTPEZY";
20 const char kSmallSIUnits[] =
"munpfazy";
24 "SI and IEC unit arrays must be the same size");
26 "Small SI and Big SI unit arrays must be the same size");
28 static const int64_t kUnitsSize =
arraysize(kBigSIUnits);
35 std::stringstream mantissa_stream;
38 mantissa_stream <<
"-";
44 const double adjusted_threshold =
45 std::max(thresh, 1.0 / std::pow(10.0,
precision));
46 const double big_threshold = adjusted_threshold * one_k;
47 const double small_threshold = adjusted_threshold;
49 const double simple_threshold = 0.01;
51 if (
val > big_threshold) {
56 if (scaled <= big_threshold) {
57 mantissa_stream << scaled;
59 *mantissa = mantissa_stream.str();
63 mantissa_stream <<
val;
65 }
else if (
val < small_threshold) {
67 if (
val < simple_threshold) {
71 if (scaled >= small_threshold) {
72 mantissa_stream << scaled;
73 *exponent = -
static_cast<int64_t
>(
i + 1);
74 *mantissa = mantissa_stream.str();
79 mantissa_stream <<
val;
82 mantissa_stream <<
val;
85 *mantissa = mantissa_stream.str();
89 if (exponent == 0)
return "";
91 const int64_t
index = (exponent > 0 ? exponent - 1 : -exponent - 1);
92 if (
index >= kUnitsSize)
return "";
95 (exponent > 0 ? (iec ? kBigIECUnits : kBigSIUnits) : kSmallSIUnits);
112 std::stringstream ss;
132 std::array<char, 256> local_buff;
133 std::size_t
size = local_buff.size();
136 auto ret = vsnprintf(local_buff.data(),
size, msg, args_cp);
142 if (
static_cast<std::size_t
>(ret) <
size)
147 size =
static_cast<std::size_t
>(ret) + 1;
148 auto buff_ptr = std::unique_ptr<char[]>(
new char[
size]);
151 ret = vsnprintf(buff_ptr.get(),
size, msg,
args);
165 std::size_t
start = 0;
166 while ((
start =
str->find(from,
start)) != std::string::npos) {
167 str->replace(
start, from.length(), to);
168 start += to.length();