qxLib
format_string.h
Go to the documentation of this file.
1 /**
2 
3  @file format_string.h
4  @author Khrapov
5  @date 16.06.2023
6  @copyright © Nick Khrapov, 2023. All right reserved.
7 
8 **/
9 #pragma once
10 
13 
14 #include <format>
15 
16 namespace qx
17 {
18 
19 /**
20  @struct basic_format_string_strong_checks
21  @brief std::basic_format_string wrapper that performs additional compile time checks
22  @details Checks braces balance and matching pairs of braces to the number of arguments
23  in exchange for some format features, such as nested replacement fields
24  @tparam char_t - char type
25  @tparam args_t - template parameter pack type
26 **/
27 template<class char_t, class... args_t>
28 struct basic_format_string_strong_checks : public std::basic_format_string<char_t, args_t...>
29 {
30  /**
31  @brief basic_format_string object constructor
32  @tparam T - string view convertible type
33  @param value - format string to check
34  **/
35  template<class T>
36  requires std::convertible_to<const T&, qx::basic_string_view<char_t>>
37  consteval basic_format_string_strong_checks(const T& value);
38 
39  /**
40  @brief Check braces balance and args num
41  @tparam T - string view convertible type
42  @param value - format string to check
43  @retval - save format string as input
44  **/
45  template<class T>
46  static consteval const T& parse_format_string(const T& value);
47 };
48 
49 template<class... args_t>
51 
52 template<class char_t, class... args_t>
53 concept format_acceptable_args_c =
54  !(qx::tuple_utils::contains_v<
55  tuple_utils::remove_t<details::all_char_types, std::tuple<char_t>>,
56  std::remove_cv_t<std::remove_pointer_t<std::decay_t<args_t>>>>
57  || ...);
58 
59 template<class char_t>
61 {
62  template<class format_parse_context_t>
63  constexpr auto parse(format_parse_context_t& ctx)
64  {
65  auto it = ctx.begin();
66 
67  if (it != ctx.end() && *it != QX_CHAR_PREFIX(char_t, '}'))
68  throw std::format_error("unknown spec");
69 
70  return it;
71  }
72 };
73 
74 // "{:sh}" -> bShort is true
75 template<class char_t>
77 {
78  bool bShort = false;
79 
80  template<class format_parse_context_t>
81  constexpr auto parse(format_parse_context_t& ctx)
82  {
83  auto it = ctx.begin();
84  if (it != ctx.end() && *it == QX_CHAR_PREFIX(char_t, 's'))
85  {
86  ++it;
87  if (it != ctx.end() && *it == QX_CHAR_PREFIX(char_t, 'h'))
88  {
89  ++it;
90  bShort = true;
91  }
92  }
93 
94  if (it != ctx.end() && *it != QX_CHAR_PREFIX(char_t, '}'))
95  throw std::format_error("unknown spec");
96 
97  return it;
98  }
99 };
100 
101 } // namespace qx
102 
requires(same_variadic_args_v< args_t... >) const expr auto coalesce(args_t &&... args)
Coalesce function, C# a ?? b analogue.
Definition: coalesce.inl:57
#define QX_CHAR_PREFIX(value_t, ch)
Chose witch of prefixes add to char : L or none.
Definition: string_utils.h:261
std::basic_format_string wrapper that performs additional compile time checks
Definition: format_string.h:29
requires std::convertible_to< const T &, qx::basic_string_view< char_t > > consteval basic_format_string_strong_checks(const T &value)
basic_format_string object constructor
static consteval const T & parse_format_string(const T &value)
Check braces balance and args num.