blob: fc5f8b0304b046cdf2ff590f96c6af75202b4da2 [file] [log] [blame]
Austin Schuhcbc17402019-01-21 21:00:30 -08001#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
9namespace benchmark {
10
11void AppendHumanReadable(int n, std::string* str);
12
13std::string HumanReadableNumber(double n, double one_k = 1024.0);
14
15#ifdef __GNUC__
16__attribute__((format(printf, 1, 2)))
17#endif
18std::string
19StrFormat(const char* format, ...);
20
21inline std::ostream& StrCatImp(std::ostream& out) BENCHMARK_NOEXCEPT {
22 return out;
23}
24
25template <class First, class... Rest>
26inline 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
31template <class... Args>
32inline std::string StrCat(Args&&... args) {
33 std::ostringstream ss;
34 StrCatImp(ss, std::forward<Args>(args)...);
35 return ss.str();
36}
37
38void 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 */
48unsigned long stoul(const std::string& str, size_t* pos = nullptr,
49 int base = 10);
50int stoi(const std::string& str, size_t* pos = nullptr, int base = 10);
51double stod(const std::string& str, size_t* pos = nullptr);
52#else
53using std::stoul;
54using std::stoi;
55using std::stod;
56#endif
57
58} // end namespace benchmark
59
60#endif // BENCHMARK_STRING_UTIL_H_