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