blob: 731aa2c04c3e0abed1e168867d91f6428a6305ef [file] [log] [blame]
Eric Fiselier60e26b72015-03-09 16:18:10 -04001#ifndef BENCHMARK_STRING_UTIL_H_
2#define BENCHMARK_STRING_UTIL_H_
Eric Fiseliera187aa02015-03-06 17:01:05 -05003
Eric Fiseliera187aa02015-03-06 17:01:05 -05004#include <sstream>
Dominic Hamon332f6772016-10-07 11:35:03 -07005#include <string>
Eric Fiseliera187aa02015-03-06 17:01:05 -05006#include <utility>
Dominic Hamon974cd5a2022-08-04 15:33:35 +01007#include <vector>
Dominic Hamonfcef4fb2021-11-10 16:04:32 +00008
dominicb1c4a752023-07-14 13:56:01 +01009#include "benchmark/benchmark.h"
Sergiu Deitsch9e47d072022-02-14 11:48:53 +010010#include "benchmark/export.h"
Dominic Hamon974cd5a2022-08-04 15:33:35 +010011#include "check.h"
Jean-Louis Leroyd49516b2015-04-13 13:45:16 -040012#include "internal_macros.h"
Eric Fiseliera187aa02015-03-06 17:01:05 -050013
14namespace benchmark {
15
dominicb1c4a752023-07-14 13:56:01 +010016BENCHMARK_EXPORT
dominicb1c4a752023-07-14 13:56:01 +010017std::string HumanReadableNumber(double n, Counter::OneK one_k);
Eric Fiseliera187aa02015-03-06 17:01:05 -050018
Sergiu Deitsch9e47d072022-02-14 11:48:53 +010019BENCHMARK_EXPORT
Daniel Harveye3666562019-03-26 11:50:53 +010020#if defined(__MINGW32__)
21__attribute__((format(__MINGW_PRINTF_FORMAT, 1, 2)))
22#elif defined(__GNUC__)
Roman Lebedevc9f26932018-11-27 03:55:05 +030023__attribute__((format(printf, 1, 2)))
24#endif
25std::string
26StrFormat(const char* format, ...);
Eric Fiseliera187aa02015-03-06 17:01:05 -050027
Wink Saville61497232018-03-07 03:20:06 -080028inline std::ostream& StrCatImp(std::ostream& out) BENCHMARK_NOEXCEPT {
Eric Fiseliera187aa02015-03-06 17:01:05 -050029 return out;
30}
31
Dominic Hamon332f6772016-10-07 11:35:03 -070032template <class First, class... Rest>
BaaMeow4c2af072018-06-01 06:14:19 -040033inline std::ostream& StrCatImp(std::ostream& out, First&& f, Rest&&... rest) {
Eric Fiseliera187aa02015-03-06 17:01:05 -050034 out << std::forward<First>(f);
Wink Saville61497232018-03-07 03:20:06 -080035 return StrCatImp(out, std::forward<Rest>(rest)...);
Eric Fiseliera187aa02015-03-06 17:01:05 -050036}
37
Dominic Hamon332f6772016-10-07 11:35:03 -070038template <class... Args>
39inline std::string StrCat(Args&&... args) {
Eric Fiseliera187aa02015-03-06 17:01:05 -050040 std::ostringstream ss;
Wink Saville61497232018-03-07 03:20:06 -080041 StrCatImp(ss, std::forward<Args>(args)...);
Eric Fiseliera187aa02015-03-06 17:01:05 -050042 return ss.str();
43}
44
Sergiu Deitsch9e47d072022-02-14 11:48:53 +010045BENCHMARK_EXPORT
Mircea Trofin376ebc22021-04-28 01:25:29 -070046std::vector<std::string> StrSplit(const std::string& str, char delim);
47
Vy Nguyen8722d6f2021-11-17 12:57:36 -050048// Disable lint checking for this block since it re-implements C functions.
49// NOLINTBEGIN
Marat Dukhan7fb3c562018-06-05 03:36:26 -070050#ifdef BENCHMARK_STL_ANDROID_GNUSTL
51/*
52 * GNU STL in Android NDK lacks support for some C++11 functions, including
53 * stoul, stoi, stod. We reimplement them here using C functions strtoul,
54 * strtol, strtod. Note that reimplemented functions are in benchmark::
55 * namespace, not std:: namespace.
56 */
57unsigned long stoul(const std::string& str, size_t* pos = nullptr,
Dominic Hamonfcef4fb2021-11-10 16:04:32 +000058 int base = 10);
Marat Dukhan7fb3c562018-06-05 03:36:26 -070059int stoi(const std::string& str, size_t* pos = nullptr, int base = 10);
60double stod(const std::string& str, size_t* pos = nullptr);
61#else
Vy Nguyen91ed7ee2021-11-19 06:12:59 -050062using std::stod; // NOLINT(misc-unused-using-decls)
63using std::stoi; // NOLINT(misc-unused-using-decls)
64using std::stoul; // NOLINT(misc-unused-using-decls)
Marat Dukhan7fb3c562018-06-05 03:36:26 -070065#endif
Vy Nguyen8722d6f2021-11-17 12:57:36 -050066// NOLINTEND
Marat Dukhan7fb3c562018-06-05 03:36:26 -070067
Dominic Hamon332f6772016-10-07 11:35:03 -070068} // end namespace benchmark
Eric Fiseliera187aa02015-03-06 17:01:05 -050069
Dominic Hamon332f6772016-10-07 11:35:03 -070070#endif // BENCHMARK_STRING_UTIL_H_