HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
format_check.hpp
1// Copyright Take Vos 2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "utility/module.hpp"
8#include <string_view>
9#include <type_traits>
10
11namespace hi::inline v1 {
12
21constexpr int format_count(std::string_view fmt) noexcept
22{
23 auto num_args = 0;
24 auto o_count = 0;
25 auto c_count = 0;
26 auto is_open = false;
27 auto prev = ' ';
28 for (auto c : fmt) {
29 if (c != prev) {
30 if (o_count % 2) {
31 if (is_open) {
32 return -1;
33 }
34 is_open = true;
35
36 } else if (c_count % 2) {
37 if (not is_open) {
38 return -2;
39 }
40 ++num_args;
41 is_open = false;
42 }
43 }
44
45 o_count = c == '{' ? o_count + 1 : 0;
46 c_count = c == '}' ? c_count + 1 : 0;
47 prev = c;
48 }
49
50 if (c_count % 2) {
51 if (not is_open) {
52 return -2;
53 }
54 ++num_args;
55
56 } else if (is_open) {
57 return -3;
58 }
59
60 return num_args;
61}
62
63#define hi_format_argument_check(arg) \
64 static_assert( \
65 ::std::is_default_constructible_v<std::formatter<std::decay_t<decltype(arg)>>>, \
66 "std::format, argument '" #arg "' does not have a specialized std::formatter<>.");
67
80#define hi_format_check(fmt, ...) \
81 static_assert(::hi::format_count(fmt) != -1, "std::format, Unexpected '{' inside argument-format."); \
82 static_assert(::hi::format_count(fmt) != -2, "std::format, Unexpected '}' without corresponding '{'."); \
83 static_assert(::hi::format_count(fmt) != -3, "std::format, Missing '}' at end of format string."); \
84 static_assert( \
85 ::hi::format_count(fmt) == hi_num_va_args(__VA_ARGS__), "std::format, invalid number of arguments for format string."); \
86 hi_for_each(hi_format_argument_check, __VA_ARGS__)
87
88} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm.hpp:13
constexpr int format_count(std::string_view fmt) noexcept
Count arguments of a std::format format string.
Definition format_check.hpp:21