Botan 3.3.0
Crypto and TLS for C&
fmt.h
Go to the documentation of this file.
1/*
2* (C) 2023 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#ifndef BOTAN_UTIL_FMT_H_
8#define BOTAN_UTIL_FMT_H_
9
10#include <botan/types.h>
11#include <locale>
12#include <sstream>
13#include <string>
14#include <string_view>
15
16namespace Botan {
17
18namespace fmt_detail {
19
20inline void do_fmt(std::ostringstream& oss, std::string_view format) {
21 oss << format;
22}
23
24template <typename T, typename... Ts>
25void do_fmt(std::ostringstream& oss, std::string_view format, const T& val, const Ts&... rest) {
26 size_t i = 0;
27
28 while(i < format.size()) {
29 if(format[i] == '{' && (format.size() > (i + 1)) && format.at(i + 1) == '}') {
30 oss << val;
31 return do_fmt(oss, format.substr(i + 2), rest...);
32 } else {
33 oss << format[i];
34 }
35
36 i += 1;
37 }
38}
39
40} // namespace fmt_detail
41
42/**
43* Simple formatter utility.
44*
45* Should be replaced with std::format once that's available on all our
46* supported compilers.
47*
48* '{}' markers in the format string are replaced by the arguments.
49* Unlike std::format, there is no support for escaping or for any kind
50* of conversion flags.
51*/
52template <typename... T>
53std::string fmt(std::string_view format, const T&... args) {
54 std::ostringstream oss;
55 oss.imbue(std::locale::classic());
56 fmt_detail::do_fmt(oss, format, args...);
57 return oss.str();
58}
59
60} // namespace Botan
61
62#endif
FE_25519 T
Definition ge.cpp:34
void do_fmt(std::ostringstream &oss, std::string_view format)
Definition fmt.h:20
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53