• 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 #include "internal_macros.h"
8 
9 namespace benchmark {
10 
11 void AppendHumanReadable(int n, std::string* str);
12 
13 std::string HumanReadableNumber(double n, double one_k = 1024.0);
14 
15 #ifdef __GNUC__
16 __attribute__((format(printf, 1, 2)))
17 #endif
18 std::string
19 StrFormat(const char* format, ...);
20 
StrCatImp(std::ostream & out)21 inline std::ostream& StrCatImp(std::ostream& out) BENCHMARK_NOEXCEPT {
22   return out;
23 }
24 
25 template <class First, class... Rest>
StrCatImp(std::ostream & out,First && f,Rest &&...rest)26 inline std::ostream& StrCatImp(std::ostream& out, First&& f, Rest&&... rest) {
27   out << std::forward<First>(f);
28   return StrCatImp(out, std::forward<Rest>(rest)...);
29 }
30 
31 template <class... Args>
StrCat(Args &&...args)32 inline std::string StrCat(Args&&... args) {
33   std::ostringstream ss;
34   StrCatImp(ss, std::forward<Args>(args)...);
35   return ss.str();
36 }
37 
38 void ReplaceAll(std::string* str, const std::string& from,
39                 const std::string& to);
40 
41 #ifdef BENCHMARK_STL_ANDROID_GNUSTL
42 /*
43  * GNU STL in Android NDK lacks support for some C++11 functions, including
44  * stoul, stoi, stod. We reimplement them here using C functions strtoul,
45  * strtol, strtod. Note that reimplemented functions are in benchmark::
46  * namespace, not std:: namespace.
47  */
48 unsigned long stoul(const std::string& str, size_t* pos = nullptr,
49                            int base = 10);
50 int stoi(const std::string& str, size_t* pos = nullptr, int base = 10);
51 double stod(const std::string& str, size_t* pos = nullptr);
52 #else
53 using std::stoul;
54 using std::stoi;
55 using std::stod;
56 #endif
57 
58 }  // end namespace benchmark
59 
60 #endif  // BENCHMARK_STRING_UTIL_H_
61