• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef BENCHMARK_STRING_UTIL_H_
2 #define BENCHMARK_STRING_UTIL_H_
3 
4 #include <sstream>
5 #include <string>
6 #include <utility>
7 
8 #include "internal_macros.h"
9 
10 namespace benchmark {
11 
12 void AppendHumanReadable(int n, std::string* str);
13 
14 std::string HumanReadableNumber(double n, double one_k = 1024.0);
15 
16 #if defined(__MINGW32__)
17 __attribute__((format(__MINGW_PRINTF_FORMAT, 1, 2)))
18 #elif defined(__GNUC__)
19 __attribute__((format(printf, 1, 2)))
20 #endif
21 std::string
22 StrFormat(const char* format, ...);
23 
StrCatImp(std::ostream & out)24 inline std::ostream& StrCatImp(std::ostream& out) BENCHMARK_NOEXCEPT {
25   return out;
26 }
27 
28 template <class First, class... Rest>
StrCatImp(std::ostream & out,First && f,Rest &&...rest)29 inline std::ostream& StrCatImp(std::ostream& out, First&& f, Rest&&... rest) {
30   out << std::forward<First>(f);
31   return StrCatImp(out, std::forward<Rest>(rest)...);
32 }
33 
34 template <class... Args>
StrCat(Args &&...args)35 inline std::string StrCat(Args&&... args) {
36   std::ostringstream ss;
37   StrCatImp(ss, std::forward<Args>(args)...);
38   return ss.str();
39 }
40 
41 std::vector<std::string> StrSplit(const std::string& str, char delim);
42 
43 // Disable lint checking for this block since it re-implements C functions.
44 // NOLINTBEGIN
45 #ifdef BENCHMARK_STL_ANDROID_GNUSTL
46 /*
47  * GNU STL in Android NDK lacks support for some C++11 functions, including
48  * stoul, stoi, stod. We reimplement them here using C functions strtoul,
49  * strtol, strtod. Note that reimplemented functions are in benchmark::
50  * namespace, not std:: namespace.
51  */
52 unsigned long stoul(const std::string& str, size_t* pos = nullptr,
53                     int base = 10);
54 int stoi(const std::string& str, size_t* pos = nullptr, int base = 10);
55 double stod(const std::string& str, size_t* pos = nullptr);
56 #else
57 using std::stod;   // NOLINT(misc-unused-using-decls)
58 using std::stoi;   // NOLINT(misc-unused-using-decls)
59 using std::stoul;  // NOLINT(misc-unused-using-decls)
60 #endif
61 // NOLINTEND
62 
63 }  // end namespace benchmark
64 
65 #endif  // BENCHMARK_STRING_UTIL_H_
66