1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
#include <cmath>
#include <Common/formatReadable.h>
#include <IO/DoubleConverter.h>
#include <IO/WriteBufferFromString.h>
#include <IO/WriteHelpers.h>
namespace DB
{
namespace ErrorCodes
{
extern const int CANNOT_PRINT_FLOAT_OR_DOUBLE_NUMBER;
}
}
// I wanted to make this ALWAYS_INLINE to prevent flappy performance tests,
// but GCC complains it may not be inlined.
static void formatReadable(double size, DB::WriteBuffer & out,
int precision, const char ** units, size_t units_size, double delimiter)
{
size_t i = 0;
for (; i + 1 < units_size && fabs(size) >= delimiter; ++i)
size /= delimiter;
DB::DoubleConverter<false>::BufferType buffer;
double_conversion::StringBuilder builder{buffer, sizeof(buffer)};
const auto result = DB::DoubleConverter<false>::instance().ToFixed(size, precision, &builder);
if (!result)
throw DB::Exception(DB::ErrorCodes::CANNOT_PRINT_FLOAT_OR_DOUBLE_NUMBER, "Cannot print float or double number");
out.write(buffer, builder.position());
writeCString(units[i], out);
}
void formatReadableSizeWithBinarySuffix(double value, DB::WriteBuffer & out, int precision)
{
const char * units[] = {" B", " KiB", " MiB", " GiB", " TiB", " PiB", " EiB", " ZiB", " YiB"};
formatReadable(value, out, precision, units, sizeof(units) / sizeof(units[0]), 1024);
}
std::string formatReadableSizeWithBinarySuffix(double value, int precision)
{
DB::WriteBufferFromOwnString out;
formatReadableSizeWithBinarySuffix(value, out, precision);
return out.str();
}
void formatReadableSizeWithDecimalSuffix(double value, DB::WriteBuffer & out, int precision)
{
const char * units[] = {" B", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"};
formatReadable(value, out, precision, units, sizeof(units) / sizeof(units[0]), 1000);
}
std::string formatReadableSizeWithDecimalSuffix(double value, int precision)
{
DB::WriteBufferFromOwnString out;
formatReadableSizeWithDecimalSuffix(value, out, precision);
return out.str();
}
void formatReadableQuantity(double value, DB::WriteBuffer & out, int precision)
{
const char * units[] = {"", " thousand", " million", " billion", " trillion", " quadrillion"};
formatReadable(value, out, precision, units, sizeof(units) / sizeof(units[0]), 1000);
}
std::string formatReadableQuantity(double value, int precision)
{
DB::WriteBufferFromOwnString out;
formatReadableQuantity(value, out, precision);
return out.str();
}
|