Kuzu C++ API
Loading...
Searching...
No Matches
string_format.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <string_view>
5#if USE_STD_FORMAT
6#include <format>
7#else
8#include "internal.h"
9#endif
10
11namespace kuzu {
12namespace common {
13
14#if USE_STD_FORMAT
15
16template<typename... Args>
17inline std::string stringFormat(std::format_string<Args...> format, Args&&... args) {
18 return std::format(format, std::forward<Args>(args)...);
19}
20
21#else
22
24#define MAP_STD_TO_STRING(typ) \
25 inline std::string map(typ v) { \
26 return std::to_string(v); \
27 }
28
30MAP_STD_TO_STRING(unsigned short)
32MAP_STD_TO_STRING(unsigned int)
34MAP_STD_TO_STRING(unsigned long)
35MAP_STD_TO_STRING(long long)
36MAP_STD_TO_STRING(unsigned long long)
39#undef MAP_STD_TO_STRING
40
41#define MAP_SELF(typ) \
42 inline typ map(typ v) { \
43 return v; \
44 }
45MAP_SELF(const char*);
46// Also covers std::string
47MAP_SELF(std::string_view)
48
49// Chars are mapped to themselves, but signed char and unsigned char (which are used for int8_t and
50// uint8_t respectively), need to be cast to be properly output as integers. This is consistent with
51// fmt's behavior.
52MAP_SELF(char)
53inline std::string map(signed char v) {
54 return std::to_string(int(v));
55}
56inline std::string map(unsigned char v) {
57 return std::to_string(unsigned(v));
58}
59#undef MAP_SELF
60
61template<typename... Args>
62inline void stringFormatHelper(std::string& ret, std::string_view format, Args&&... args) {
63 size_t bracket = format.find('{');
64 if (bracket == std::string_view::npos) {
65 ret += format;
66 return;
67 }
68 ret += format.substr(0, bracket);
69 if (format.substr(bracket, 4) == "{{}}") {
70 // Escaped {}.
71 ret += "{}";
72 return stringFormatHelper(ret, format.substr(bracket + 4), std::forward<Args>(args)...);
73 } else if (format.substr(bracket, 2) == "{}") {
74 // Formatted {}.
75 throw InternalException("Not enough values for string_format.");
76 }
77 // Something else.
78 ret.push_back('{');
79 return stringFormatHelper(ret, format.substr(bracket + 1), std::forward<Args>(args)...);
80}
81
82template<typename Arg, typename... Args>
83inline void stringFormatHelper(std::string& ret, std::string_view format, Arg&& arg,
84 Args&&... args) {
85 size_t bracket = format.find('{');
86 if (bracket == std::string_view::npos) {
87 throw InternalException("Too many values for string_format.");
88 }
89 ret += format.substr(0, bracket);
90 if (format.substr(bracket, 4) == "{{}}") {
91 // Escaped {}.
92 ret += "{}";
93 return stringFormatHelper(ret, format.substr(bracket + 4), std::forward<Arg>(arg),
94 std::forward<Args>(args)...);
95 } else if (format.substr(bracket, 2) == "{}") {
96 // Formatted {}.
97 ret += map(arg);
98 return stringFormatHelper(ret, format.substr(bracket + 2), std::forward<Args>(args)...);
99 }
100 // Something else.
101 ret.push_back('{');
102 return stringFormatHelper(ret, format.substr(bracket + 1), std::forward<Arg>(arg),
103 std::forward<Args>(args)...);
104}
105} // namespace string_format_detail
106
107// Formats `args` according to `format`. Accepts {} for formatting the argument and {{}} for
108// a literal {}. Formatting is done with std::ostream::operator<<.
109template<typename... Args>
110inline std::string stringFormat(std::string_view format, Args&&... args) {
111 std::string ret;
112 ret.reserve(32); // Optimistic pre-allocation.
113 string_format_detail::stringFormatHelper(ret, format, std::forward<Args>(args)...);
114 return ret;
115}
116
117#endif
118
119} // namespace common
120} // namespace kuzu
Definition internal.h:9
Definition string_format.h:23
std::string map(signed char v)
Definition string_format.h:53
void stringFormatHelper(std::string &ret, std::string_view format, Args &&... args)
Definition string_format.h:62
Definition array_utils.h:7
std::string stringFormat(std::string_view format, Args &&... args)
Definition string_format.h:110
Definition array_utils.h:7
#define MAP_STD_TO_STRING(typ)
Definition string_format.h:24
#define MAP_SELF(typ)
Definition string_format.h:41