12 #ifndef ROOT_StringConv 13 #define ROOT_StringConv 18 #include "RConfigure.h" 37 template <
typename value_type>
44 static const char *
const suffix[][2] =
53 value_type unit = si ? 1000 : 1024;
61 }
else if (bytes > 0) {
63 (
int) (
sizeof(suffix) /
sizeof(suffix[0]) - 1));
65 *coeff = bytes /
std::pow(unit, exp);
66 *units = suffix[
exp][!si];
89 size_t size = str.size();
92 const double coeff = stod(std::string(str.data(), str.size()), &cur);
95 while (cur<size && isspace(str[cur])) ++cur;
101 auto result = [coeff,&
exp,&unit,&value]() {
102 double v = exp ? coeff *
std::pow(unit, exp / 3) : coeff;
103 if (v < std::numeric_limits<T>::max()) {
110 if (cur==size)
return result();
112 switch (toupper(str[cur])) {
113 case 'B': exp = 0;
break;
114 case 'K': exp = 3;
break;
115 case 'M': exp = 6;
break;
116 case 'G': exp = 9;
break;
117 case 'T': exp = 12;
break;
118 case 'E': exp = 15;
break;
119 case 'Z': exp = 18;
break;
120 case 'Y': exp = 21;
break;
127 if (cur<size && toupper(str[cur]) ==
'I') {
132 if (cur==size)
return result();
135 switch (toupper(str[cur])) {
138 case '\t': ++cur;
break;
140 case '\0':
return result();
159 template <
typename T>
167 #endif // ROOT_StringConv basic_string_view< char > string_view
Namespace for new ROOT classes and functions.
void ToHumanReadableSize(value_type bytes, Bool_t si, Double_t *coeff, const char **units)
Return the size expressed in 'human readable' format.
EFromHumanReadableSize FromHumanReadableSize(std::string_view str, T &value)
Convert strings like the following into byte counts 5MB, 5 MB, 5M, 3.7GB, 123b, 456kB, 3.7GiB, 5MiB with some amount of forgiveness baked into the parsing.
double pow(double, double)