1 #ifndef LIBFILEZILLA_FORMAT_HEADER
2 #define LIBFILEZILLA_FORMAT_HEADER
10 #ifdef LFZ_FORMAT_DEBUG
12 #define format_assert(pred) assert((pred))
14 #define format_assert(pred)
40 explicit operator bool()
const {
return type != 0; }
43 template<
typename Arg>
44 bool is_negative([[maybe_unused]] Arg && v)
46 if constexpr (std::is_signed_v<std::decay_t<Arg>>) {
56 template<
typename String,
bool Un
signed,
typename Arg>
57 typename std::enable_if_t<std::is_integral_v<std::decay_t<Arg>> && !std::is_enum_v<std::decay_t<Arg>>, String> integral_to_string(field
const& f, Arg && arg)
59 std::decay_t<Arg> v = arg;
63 format_assert(!Unsigned || !std::is_signed_v<std::decay_t<Arg>> || arg >= 0);
65 if (is_negative(arg)) {
68 else if (f.flags & always_sign) {
71 else if (f.flags & pad_blank) {
76 typename String::value_type buf[
sizeof(v) * 4 + 1];
77 auto *
const end = buf +
sizeof(v) * 4 + 1;
81 int const mod = std::abs(
static_cast<int>(v % 10));
87 if (f.flags & with_width) {
88 if (lead && width > 0) {
94 if (f.flags & pad_0) {
98 if (
static_cast<size_t>(end - p) < width) {
99 ret.append(width - (end - p),
'0');
104 if (
static_cast<size_t>(end - p) < width && !(f.flags & left_align)) {
105 ret.append(width - (end - p),
' ');
111 if (
static_cast<size_t>(end - p) < width && f.flags & left_align) {
112 ret.append(width - (end - p),
' ');
122 return String(p, end);
127 template<
typename String,
bool Un
signed,
typename Arg>
128 typename std::enable_if_t<std::is_enum_v<std::decay_t<Arg>>, String> integral_to_string(field
const& f, Arg && arg)
130 return integral_to_string<String, Unsigned>(f,
static_cast<std::underlying_type_t<std::decay_t<Arg>
>>(arg));
134 template<
typename String,
bool Un
signed,
typename Arg>
135 typename std::enable_if_t<!std::is_integral_v<std::decay_t<Arg>> && !std::is_enum_v<std::decay_t<Arg>>, String> integral_to_string(field
const&, Arg &&)
141 template<
typename String,
class Arg,
typename =
void>
142 struct has_toString : std::false_type {};
144 template<
typename String,
class Arg>
145 struct has_toString<String, Arg, std::void_t<decltype(toString<String>(std::declval<Arg>()))>> : std::true_type {};
153 template <
typename String>
154 static constexpr
bool is_formattable_as = std::disjunction<
155 std::is_enum<std::decay_t<Arg>>,
156 std::is_arithmetic<std::decay_t<Arg>>,
157 std::is_pointer<std::decay_t<Arg>>,
158 std::is_same<String, std::decay_t<Arg>>,
159 has_toString<String, Arg>
165 template<
typename String,
bool Lowercase,
typename Arg>
166 String integral_to_hex_string(Arg && arg) noexcept
168 if constexpr (std::is_enum_v<std::decay_t<Arg>>) {
170 return integral_to_hex_string<String, Lowercase>(
static_cast<std::underlying_type_t<std::decay_t<Arg>
>>(arg));
172 else if constexpr (std::is_signed_v<std::decay_t<Arg>>) {
173 return integral_to_hex_string<String, Lowercase>(
static_cast<std::make_unsigned_t<std::decay_t<Arg>
>>(arg));
175 else if constexpr (std::is_integral_v<std::decay_t<Arg>>) {
176 std::decay_t<Arg> v = arg;
177 typename String::value_type buf[
sizeof(v) * 2];
178 auto*
const end = buf +
sizeof(v) * 2;
182 *(--p) = fz::int_to_hex_char<typename String::value_type, Lowercase>(v & 0xf);
186 return String(p, end);
195 template<
typename String,
typename Arg>
196 String pointer_to_string(Arg&& arg) noexcept
198 if constexpr (std::is_pointer_v<std::decay_t<Arg>>) {
199 return String({
'0',
'x'}) + integral_to_hex_string<String, true>(
reinterpret_cast<uintptr_t
>(arg));
207 template<
typename String,
typename Arg>
208 String char_to_string(Arg&& arg)
210 if constexpr (std::is_integral_v<std::decay_t<Arg>>) {
211 return String({
static_cast<typename String::value_type
>(
static_cast<unsigned char>(arg))});
220 template<
typename String>
221 void pad_arg(String& s, field
const& f)
223 if (f.flags & with_width && s.size() < f.width) {
224 if (f.flags & left_align) {
225 s += String(f.width - s.size(),
' ');
228 s = String(f.width - s.size(), (f.flags & pad_0) ?
'0' :
' ') + s;
233 template<
typename String,
typename Arg>
234 String format_arg(field
const& f, Arg&& arg)
238 if constexpr (std::is_same_v<String, std::decay_t<Arg>>) {
241 else if constexpr (has_toString<String, Arg>::value) {
244 ret = toString<String>(std::forward<Arg>(arg));
252 else if (f.type ==
'd' || f.type ==
'i') {
253 ret = integral_to_string<String, false>(f, std::forward<Arg>(arg));
255 else if (f.type ==
'u') {
256 ret = integral_to_string<String, true>(f, std::forward<Arg>(arg));
258 else if (f.type ==
'x') {
259 ret = integral_to_hex_string<String, true>(std::forward<Arg>(arg));
262 else if (f.type ==
'X') {
263 ret = integral_to_hex_string<String, false>(std::forward<Arg>(arg));
266 else if (f.type ==
'p') {
267 ret = pointer_to_string<String>(std::forward<Arg>(arg));
270 else if (f.type ==
'c') {
271 ret = char_to_string<String>(std::forward<Arg>(arg));
279 template<
typename String,
typename... Args>
280 String extract_arg(field
const&,
size_t)
286 template<
typename String,
typename Arg,
typename... Args>
287 String extract_arg(field
const& f,
size_t arg_n, Arg&& arg, Args&&...args)
292 ret = format_arg<String>(f, std::forward<Arg>(arg));
295 ret = extract_arg<String>(f, arg_n - 1, std::forward<Args>(args)...);
301 template<
typename InString,
typename OutString>
302 field get_field(InString
const& fmt,
typename InString::size_type & pos,
size_t& arg_n, OutString & ret)
305 if (++pos >= fmt.size()) {
311 if (fmt[pos] ==
'%') {
319 if (fmt[pos] ==
'0') {
322 else if (fmt[pos] ==
' ') {
323 f.flags |= pad_blank;
325 else if (fmt[pos] ==
'-') {
327 f.flags |= left_align;
329 else if (fmt[pos] ==
'+') {
330 f.flags &= ~pad_blank;
331 f.flags |= always_sign;
336 if (++pos >= fmt.size()) {
343 while (fmt[pos] >=
'0' && fmt[pos] <=
'9') {
344 f.flags |= with_width;
346 f.width += fmt[pos] -
'0';
347 if (++pos >= fmt.size()) {
352 if (f.width > 10000) {
357 if (fmt[pos] ==
'$') {
360 if (++pos >= fmt.size()) {
370 if (c ==
'h' || c ==
'l' || c ==
'L' || c ==
'j' || c ==
'z' || c ==
't') {
371 if (++pos >= fmt.size()) {
381 f.type =
static_cast<char>(fmt[pos++]);
385 template<
typename String,
typename Arg,
int N>
386 constexpr
bool check_argument()
389 argument<N>::template of_type<Arg>::template is_formattable_as<String>,
390 "Argument cannot be formatted by fz::sprintf()"
393 return argument<N>::template of_type<Arg>::template is_formattable_as<String>;
396 template<
typename String,
typename... Args, std::size_t... Is>
397 constexpr
bool check_arguments(std::index_sequence<Is...>)
399 return (check_argument<String, Args, Is>() && ...);
402 template<
typename InString,
typename CharType =
typename InString::value_type,
typename OutString = std::basic_
string<CharType>,
typename... Args>
403 OutString do_sprintf(InString
const& fmt, Args&&... args)
408 typename InString::size_type start = 0, pos;
411 while ((pos = fmt.find(
'%', start)) != InString::npos) {
414 ret += fmt.substr(start, pos - start);
416 field f = detail::get_field(fmt, pos, arg_n, ret);
418 format_assert(arg_n <
sizeof...(args));
419 ret += detail::extract_arg<OutString>(f, arg_n++, std::forward<Args>(args)...);
426 ret += fmt.substr(start);
455 template<
typename... Args>
456 std::string
sprintf(std::string_view
const& fmt, Args&&... args)
458 detail::check_arguments<std::string, Args...>(std::index_sequence_for<Args...>());
460 return detail::do_sprintf(fmt, std::forward<Args>(args)...);
463 template<
typename... Args>
464 std::wstring
sprintf(std::wstring_view
const& fmt, Args&&... args)
466 detail::check_arguments<std::wstring, Args...>(std::index_sequence_for<Args...>());
468 return detail::do_sprintf(fmt, std::forward<Args>(args)...);
Functions to encode/decode strings.
type
Definition: logger.hpp:16
The namespace used by libfilezilla.
Definition: apply.hpp:17
std::string sprintf(std::string_view const &fmt, Args &&... args)
A simple type-safe sprintf replacement.
Definition: format.hpp:456
String types and assorted functions.