Austin Schuh | cbc1740 | 2019-01-21 21:00:30 -0800 | [diff] [blame] | 1 | #include "string_util.h" |
| 2 | |
| 3 | #include <array> |
| 4 | #include <cmath> |
| 5 | #include <cstdarg> |
| 6 | #include <cstdio> |
| 7 | #include <memory> |
| 8 | #include <sstream> |
| 9 | |
| 10 | #include "arraysize.h" |
| 11 | |
| 12 | namespace benchmark { |
| 13 | namespace { |
| 14 | |
| 15 | // kilo, Mega, Giga, Tera, Peta, Exa, Zetta, Yotta. |
| 16 | const char kBigSIUnits[] = "kMGTPEZY"; |
| 17 | // Kibi, Mebi, Gibi, Tebi, Pebi, Exbi, Zebi, Yobi. |
| 18 | const char kBigIECUnits[] = "KMGTPEZY"; |
| 19 | // milli, micro, nano, pico, femto, atto, zepto, yocto. |
| 20 | const char kSmallSIUnits[] = "munpfazy"; |
| 21 | |
| 22 | // We require that all three arrays have the same size. |
| 23 | static_assert(arraysize(kBigSIUnits) == arraysize(kBigIECUnits), |
| 24 | "SI and IEC unit arrays must be the same size"); |
| 25 | static_assert(arraysize(kSmallSIUnits) == arraysize(kBigSIUnits), |
| 26 | "Small SI and Big SI unit arrays must be the same size"); |
| 27 | |
| 28 | static const int64_t kUnitsSize = arraysize(kBigSIUnits); |
| 29 | |
| 30 | void ToExponentAndMantissa(double val, double thresh, int precision, |
| 31 | double one_k, std::string* mantissa, |
| 32 | int64_t* exponent) { |
| 33 | std::stringstream mantissa_stream; |
| 34 | |
| 35 | if (val < 0) { |
| 36 | mantissa_stream << "-"; |
| 37 | val = -val; |
| 38 | } |
| 39 | |
| 40 | // Adjust threshold so that it never excludes things which can't be rendered |
| 41 | // in 'precision' digits. |
| 42 | const double adjusted_threshold = |
| 43 | std::max(thresh, 1.0 / std::pow(10.0, precision)); |
| 44 | const double big_threshold = adjusted_threshold * one_k; |
| 45 | const double small_threshold = adjusted_threshold; |
| 46 | // Values in ]simple_threshold,small_threshold[ will be printed as-is |
| 47 | const double simple_threshold = 0.01; |
| 48 | |
| 49 | if (val > big_threshold) { |
| 50 | // Positive powers |
| 51 | double scaled = val; |
| 52 | for (size_t i = 0; i < arraysize(kBigSIUnits); ++i) { |
| 53 | scaled /= one_k; |
| 54 | if (scaled <= big_threshold) { |
| 55 | mantissa_stream << scaled; |
| 56 | *exponent = i + 1; |
| 57 | *mantissa = mantissa_stream.str(); |
| 58 | return; |
| 59 | } |
| 60 | } |
| 61 | mantissa_stream << val; |
| 62 | *exponent = 0; |
| 63 | } else if (val < small_threshold) { |
| 64 | // Negative powers |
| 65 | if (val < simple_threshold) { |
| 66 | double scaled = val; |
| 67 | for (size_t i = 0; i < arraysize(kSmallSIUnits); ++i) { |
| 68 | scaled *= one_k; |
| 69 | if (scaled >= small_threshold) { |
| 70 | mantissa_stream << scaled; |
| 71 | *exponent = -static_cast<int64_t>(i + 1); |
| 72 | *mantissa = mantissa_stream.str(); |
| 73 | return; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | mantissa_stream << val; |
| 78 | *exponent = 0; |
| 79 | } else { |
| 80 | mantissa_stream << val; |
| 81 | *exponent = 0; |
| 82 | } |
| 83 | *mantissa = mantissa_stream.str(); |
| 84 | } |
| 85 | |
| 86 | std::string ExponentToPrefix(int64_t exponent, bool iec) { |
| 87 | if (exponent == 0) return ""; |
| 88 | |
| 89 | const int64_t index = (exponent > 0 ? exponent - 1 : -exponent - 1); |
| 90 | if (index >= kUnitsSize) return ""; |
| 91 | |
| 92 | const char* array = |
| 93 | (exponent > 0 ? (iec ? kBigIECUnits : kBigSIUnits) : kSmallSIUnits); |
| 94 | if (iec) |
| 95 | return array[index] + std::string("i"); |
| 96 | else |
| 97 | return std::string(1, array[index]); |
| 98 | } |
| 99 | |
| 100 | std::string ToBinaryStringFullySpecified(double value, double threshold, |
| 101 | int precision, double one_k = 1024.0) { |
| 102 | std::string mantissa; |
| 103 | int64_t exponent; |
| 104 | ToExponentAndMantissa(value, threshold, precision, one_k, &mantissa, |
| 105 | &exponent); |
| 106 | return mantissa + ExponentToPrefix(exponent, false); |
| 107 | } |
| 108 | |
| 109 | } // end namespace |
| 110 | |
| 111 | void AppendHumanReadable(int n, std::string* str) { |
| 112 | std::stringstream ss; |
| 113 | // Round down to the nearest SI prefix. |
| 114 | ss << ToBinaryStringFullySpecified(n, 1.0, 0); |
| 115 | *str += ss.str(); |
| 116 | } |
| 117 | |
| 118 | std::string HumanReadableNumber(double n, double one_k) { |
| 119 | // 1.1 means that figures up to 1.1k should be shown with the next unit down; |
| 120 | // this softens edge effects. |
| 121 | // 1 means that we should show one decimal place of precision. |
| 122 | return ToBinaryStringFullySpecified(n, 1.1, 1, one_k); |
| 123 | } |
| 124 | |
| 125 | std::string StrFormatImp(const char* msg, va_list args) { |
| 126 | // we might need a second shot at this, so pre-emptivly make a copy |
| 127 | va_list args_cp; |
| 128 | va_copy(args_cp, args); |
| 129 | |
| 130 | // TODO(ericwf): use std::array for first attempt to avoid one memory |
| 131 | // allocation guess what the size might be |
| 132 | std::array<char, 256> local_buff; |
| 133 | std::size_t size = local_buff.size(); |
| 134 | // 2015-10-08: vsnprintf is used instead of snd::vsnprintf due to a limitation |
| 135 | // in the android-ndk |
| 136 | auto ret = vsnprintf(local_buff.data(), size, msg, args_cp); |
| 137 | |
| 138 | va_end(args_cp); |
| 139 | |
| 140 | // handle empty expansion |
| 141 | if (ret == 0) return std::string{}; |
| 142 | if (static_cast<std::size_t>(ret) < size) |
| 143 | return std::string(local_buff.data()); |
| 144 | |
| 145 | // we did not provide a long enough buffer on our first attempt. |
| 146 | // add 1 to size to account for null-byte in size cast to prevent overflow |
| 147 | size = static_cast<std::size_t>(ret) + 1; |
| 148 | auto buff_ptr = std::unique_ptr<char[]>(new char[size]); |
| 149 | // 2015-10-08: vsnprintf is used instead of snd::vsnprintf due to a limitation |
| 150 | // in the android-ndk |
| 151 | ret = vsnprintf(buff_ptr.get(), size, msg, args); |
| 152 | return std::string(buff_ptr.get()); |
| 153 | } |
| 154 | |
| 155 | std::string StrFormat(const char* format, ...) { |
| 156 | va_list args; |
| 157 | va_start(args, format); |
| 158 | std::string tmp = StrFormatImp(format, args); |
| 159 | va_end(args); |
| 160 | return tmp; |
| 161 | } |
| 162 | |
| 163 | void ReplaceAll(std::string* str, const std::string& from, |
| 164 | const std::string& to) { |
| 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(); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | #ifdef BENCHMARK_STL_ANDROID_GNUSTL |
| 173 | /* |
| 174 | * GNU STL in Android NDK lacks support for some C++11 functions, including |
| 175 | * stoul, stoi, stod. We reimplement them here using C functions strtoul, |
| 176 | * strtol, strtod. Note that reimplemented functions are in benchmark:: |
| 177 | * namespace, not std:: namespace. |
| 178 | */ |
| 179 | unsigned long stoul(const std::string& str, size_t* pos, int base) { |
| 180 | /* Record previous errno */ |
| 181 | const int oldErrno = errno; |
| 182 | errno = 0; |
| 183 | |
| 184 | const char* strStart = str.c_str(); |
| 185 | char* strEnd = const_cast<char*>(strStart); |
| 186 | const unsigned long result = strtoul(strStart, &strEnd, base); |
| 187 | |
| 188 | const int strtoulErrno = errno; |
| 189 | /* Restore previous errno */ |
| 190 | errno = oldErrno; |
| 191 | |
| 192 | /* Check for errors and return */ |
| 193 | if (strtoulErrno == ERANGE) { |
| 194 | throw std::out_of_range( |
| 195 | "stoul failed: " + str + " is outside of range of unsigned long"); |
| 196 | } else if (strEnd == strStart || strtoulErrno != 0) { |
| 197 | throw std::invalid_argument( |
| 198 | "stoul failed: " + str + " is not an integer"); |
| 199 | } |
| 200 | if (pos != nullptr) { |
| 201 | *pos = static_cast<size_t>(strEnd - strStart); |
| 202 | } |
| 203 | return result; |
| 204 | } |
| 205 | |
| 206 | int stoi(const std::string& str, size_t* pos, int base) { |
| 207 | /* Record previous errno */ |
| 208 | const int oldErrno = errno; |
| 209 | errno = 0; |
| 210 | |
| 211 | const char* strStart = str.c_str(); |
| 212 | char* strEnd = const_cast<char*>(strStart); |
| 213 | const long result = strtol(strStart, &strEnd, base); |
| 214 | |
| 215 | const int strtolErrno = errno; |
| 216 | /* Restore previous errno */ |
| 217 | errno = oldErrno; |
| 218 | |
| 219 | /* Check for errors and return */ |
| 220 | if (strtolErrno == ERANGE || long(int(result)) != result) { |
| 221 | throw std::out_of_range( |
| 222 | "stoul failed: " + str + " is outside of range of int"); |
| 223 | } else if (strEnd == strStart || strtolErrno != 0) { |
| 224 | throw std::invalid_argument( |
| 225 | "stoul failed: " + str + " is not an integer"); |
| 226 | } |
| 227 | if (pos != nullptr) { |
| 228 | *pos = static_cast<size_t>(strEnd - strStart); |
| 229 | } |
| 230 | return int(result); |
| 231 | } |
| 232 | |
| 233 | double stod(const std::string& str, size_t* pos) { |
| 234 | /* Record previous errno */ |
| 235 | const int oldErrno = errno; |
| 236 | errno = 0; |
| 237 | |
| 238 | const char* strStart = str.c_str(); |
| 239 | char* strEnd = const_cast<char*>(strStart); |
| 240 | const double result = strtod(strStart, &strEnd); |
| 241 | |
| 242 | /* Restore previous errno */ |
| 243 | const int strtodErrno = errno; |
| 244 | errno = oldErrno; |
| 245 | |
| 246 | /* Check for errors and return */ |
| 247 | if (strtodErrno == ERANGE) { |
| 248 | throw std::out_of_range( |
| 249 | "stoul failed: " + str + " is outside of range of int"); |
| 250 | } else if (strEnd == strStart || strtodErrno != 0) { |
| 251 | throw std::invalid_argument( |
| 252 | "stoul failed: " + str + " is not an integer"); |
| 253 | } |
| 254 | if (pos != nullptr) { |
| 255 | *pos = static_cast<size_t>(strEnd - strStart); |
| 256 | } |
| 257 | return result; |
| 258 | } |
| 259 | #endif |
| 260 | |
| 261 | } // end namespace benchmark |