-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathToString.cpp
More file actions
255 lines (233 loc) · 6.9 KB
/
ToString.cpp
File metadata and controls
255 lines (233 loc) · 6.9 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* ToString.cpp - String Tools
*
* Author : Alexander J. Yee
* Date Created : 07/07/2013
* Last Modified : 08/24/2014
*
*/
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#include <sstream>
#include <iomanip>
#include "PublicLibs/CompilerSettings.h"
#include "PublicLibs/ConsoleIO/Label.h"
#include "ToString.h"
namespace ymp{
namespace StringTools{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Helpers
YM_NO_INLINE std::string tostr_u_commas(uiL_t x){
// Prints out x with comma separators.
std::string str = std::to_string(x);
std::string out;
const char* ptr = str.c_str();
upL_t len = str.size();
upL_t commas = (len + 2) / 3 - 1;
upL_t shift = len - commas * 3;
while (1){
char ch = *ptr++;
if (ch == '\0')
break;
if (shift == 0){
out += ',';
shift = 3;
}
out += ch;
shift--;
}
return out;
}
YM_NO_INLINE std::string tostr_s_commas(siL_t x){
if (x < 0)
return std::string("-") + tostr_u_commas(-x);
else
return tostr_u_commas(x);
}
YM_NO_INLINE std::string tostr_u_bytes(uiL_t bytes){
// Prints out bytes in one of the following forms:
// 0.xx suffix
// x.xx suffix
// xx.x suffix
// xxx suffix
const char* BYTE_NAMES[] = {
" bytes",
" KiB",
" MiB",
" GiB",
" TiB",
" PiB",
" EiB",
};
std::string out;
if (bytes < 1000){
if (bytes < 10){
out += " ";
}
out += std::to_string(bytes);
out += BYTE_NAMES[0];
return out;
}
int suffix_index = 1;
while (bytes >= 1024000){
bytes >>= 10;
suffix_index++;
}
bytes *= 1000;
bytes >>= 10;
// .xxx or (1.00)
if (bytes < 1000){
bytes += 5;
bytes /= 10;
if (bytes == 100){
out += "1.00";
out += BYTE_NAMES[suffix_index];
return out;
}
out += "0.";
out += std::to_string(bytes);
out += BYTE_NAMES[suffix_index];
return out;
}
// x.xx or (10.0)
if (bytes < 10000){
bytes += 5;
bytes /= 10;
if (bytes == 1000){
out += "10.0";
out += BYTE_NAMES[suffix_index];
return out;
}
out += std::to_string(bytes / 100);
bytes %= 100;
out += ".";
if (bytes >= 10){
out += std::to_string(bytes);
}else{
out += "0";
out += std::to_string(bytes);
}
out += BYTE_NAMES[suffix_index];
return out;
}
// xx.x or (0.98)
if (bytes < 100000){
bytes += 50;
bytes /= 100;
if (bytes == 1000){
out += " 100";
out += BYTE_NAMES[suffix_index];
return out;
}
out += std::to_string(bytes / 10);
bytes %= 10;
out += ".";
out += std::to_string(bytes);
out += BYTE_NAMES[suffix_index];
return out;
}
// xxx or (1.00)
{
bytes += 500;
bytes /= 1000;
if (bytes == 1000){
out += "0.98";
out += BYTE_NAMES[suffix_index + 1];
return out;
}
out += " ";
out += std::to_string(bytes);
out += BYTE_NAMES[suffix_index];
return out;
}
}
YM_NO_INLINE std::string tostr_s_bytes(siL_t bytes){
if (bytes < 0){
return std::string("-") + tostr_u_bytes(-bytes);
}else{
return tostr_u_bytes(bytes);
}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
YM_NO_INLINE std::string tostr(uiL_t x, NumberFormat format){
switch (format){
case COMMAS:
return tostr_u_commas(x);
case BYTES:
return tostr_u_bytes(x);
case BYTES_EXPANDED:{
auto out = tostr_u_commas(x);
out += " (";
out += tostr_u_bytes(x);
out += ")";
return out;
}
default:
return std::to_string(x);
}
}
YM_NO_INLINE std::string tostr(siL_t x, NumberFormat format){
switch (format){
case COMMAS:
return tostr_s_commas(x);
case BYTES:
return tostr_s_bytes(x);
case BYTES_EXPANDED:{
auto out = tostr_s_commas(x);
out += " (";
out += tostr_s_bytes(x);
out += ")";
return out;
}
default:
return std::to_string(x);
}
}
YM_NO_INLINE std::string tostrln(uiL_t x, NumberFormat format){
return tostr(x, format) += "\r\n";
}
YM_NO_INLINE std::string tostrln(siL_t x, NumberFormat format){
return tostr(x, format) += "\r\n";
}
YM_NO_INLINE std::string tostr_width(uiL_t x, int width){
std::ostringstream out;
out << std::setfill('0');
out << std::setw(width);
out << x;
return out.str();
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Float
YM_NO_INLINE std::string tostr_float(double x, int precision){
std::ostringstream out;
out << std::setprecision(precision);
out << x;
return out.str();
}
YM_NO_INLINE std::string tostrln_float(double x, int precision){
return tostr_float(x, precision) += "\r\n";
}
YM_NO_INLINE std::string tostr_fixed(double x, int precision){
std::ostringstream out;
out << std::setprecision(precision);
out << std::fixed;
out << x;
return out.str();
}
YM_NO_INLINE std::string tostrln_fixed(double x, int precision){
return tostr_fixed(x, precision) += "\r\n";
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
}
}