ZeroErr
format.h
Go to the documentation of this file.
1 #pragma once
2 #include "zeroerr/print.h"
3 
4 #include <sstream>
5 #include <string>
6 
7 namespace zeroerr {
8 
9 
24 template <typename... T>
25 std::string format(const char* fmt, T... args) {
26  std::stringstream ss;
27  bool parse_name = false;
28  Printer print;
29 
30  print.isQuoted = false;
31  print.isCompact = true;
32  print.line_break = "";
33  std::string str_args[] = {print(args)...};
34 
35  int j = 0;
36  for (const char* i = fmt; *i != '\0'; i++) {
37  switch (*i) {
38  case '{': parse_name = true; break;
39  case '}':
40  parse_name = false;
41  ss << str_args[j++];
42  break;
43  default:
44  if (!parse_name) ss << *i;
45  break;
46  }
47  }
48  return ss.str();
49 }
50 
51 } // namespace zeroerr
Definition: benchmark.cpp:17
std::string format(const char *fmt, T... args)
Format a string with arguments.
Definition: format.h:25
A functor class Printer for printing a value of any type.
Definition: print.h:80
bool isQuoted
Definition: print.h:123
std::string str() const
Definition: print.h:270
bool isCompact
Definition: print.h:122
const char * line_break
Definition: print.h:125