Botan 3.0.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 <sstream>
12#include <string_view>
13#include <string>
14#include <locale>
15
16namespace Botan {
17
18namespace fmt_detail {
19
20inline void do_fmt(std::ostringstream& oss, std::string_view format)
21 {
22 oss << format;
23 }
24
25template<typename T, typename... Ts>
26void do_fmt(std::ostringstream& oss, std::string_view format, const T& val, const Ts&... rest)
27 {
28 size_t i = 0;
29
30 while(i < format.size())
31 {
32 if(format[i] == '{' && (format.size() > (i + 1)) && format.at(i + 1) == '}')
33 {
34 oss << val;
35 return do_fmt(oss, format.substr(i + 2), rest...);
36 }
37 else
38 {
39 oss << format[i];
40 }
41
42 i += 1;
43 }
44 }
45
46}
47
48
49/**
50* Simple formatter utility.
51*
52* Should be replaced with std::format once that's available on all our
53* supported compilers.
54*
55* '{}' markers in the format string are replaced by the arguments.
56* Unlike std::format, there is no support for escaping or for any kind
57* of conversion flags.
58*/
59template<typename... T>
60std::string fmt(std::string_view format, const T&... args)
61 {
62 std::ostringstream oss;
63 oss.imbue(std::locale::classic());
64 fmt_detail::do_fmt(oss, format, args...);
65 return oss.str();
66 }
67
68}
69
70#endif
FE_25519 T
Definition: ge.cpp:36
void do_fmt(std::ostringstream &oss, std::string_view format)
Definition: fmt.h:20
Definition: alg_id.cpp:12
std::string fmt(std::string_view format, const T &... args)
Definition: fmt.h:60