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