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 namespace qx
15 {
16 
17 /**
18  @struct basic_format_string_strong_checks
19  @brief QX_FMT_NS::basic_format_string wrapper that performs additional compile time checks
20  @details Checks braces balance and matching pairs of braces to the number of arguments
21  in exchange for some format features, such as nested replacement fields
22  @tparam char_t - char type
23  @tparam args_t - template parameter pack type
24 **/
25 template<class char_t, class... args_t>
26 struct basic_format_string_strong_checks : public QX_FMT_NS::basic_format_string<char_t, args_t...>
27 {
28  /**
29  @brief basic_format_string object constructor
30  @tparam T - string view convertible type
31  @param value - format string to check
32  **/
33  template<class T>
34  requires std::convertible_to<const T&, qx::basic_string_view<char_t>>
35  consteval basic_format_string_strong_checks(const T& value);
36 
37  /**
38  @brief Check braces balance and args num
39  @tparam T - string view convertible type
40  @param value - format string to check
41  @retval - save format string as input
42  **/
43  template<class T>
44  static consteval const T& parse_format_string(const T& value);
45 };
46 
47 template<class... args_t>
49 
50 template<class char_t, class... args_t>
51 concept format_acceptable_args_c =
52  !(qx::tuple_utils::contains_v<
53  tuple_utils::remove_t<details::all_char_types, std::tuple<char_t>>,
54  std::remove_cv_t<std::remove_pointer_t<std::decay_t<args_t>>>>
55  || ...);
56 
57 template<class char_t>
59 {
60  template<class format_parse_context_t>
61  constexpr auto parse(format_parse_context_t& ctx)
62  {
63  auto it = ctx.begin();
64 
65  if (it != ctx.end() && *it != QX_CHAR_PREFIX(char_t, '}'))
66  throw QX_FMT_NS::format_error("unknown spec");
67 
68  return it;
69  }
70 };
71 
72 // "{:sh}" -> bShort is true
73 template<class char_t>
75 {
76  bool bShort = false;
77 
78  template<class format_parse_context_t>
79  constexpr auto parse(format_parse_context_t& ctx)
80  {
81  auto it = ctx.begin();
82  if (it != ctx.end() && *it == QX_CHAR_PREFIX(char_t, 's'))
83  {
84  ++it;
85  if (it != ctx.end() && *it == QX_CHAR_PREFIX(char_t, 'h'))
86  {
87  ++it;
88  bShort = true;
89  }
90  }
91 
92  if (it != ctx.end() && *it != QX_CHAR_PREFIX(char_t, '}'))
93  throw QX_FMT_NS::format_error("unknown spec");
94 
95  return it;
96  }
97 };
98 
99 } // namespace qx
100 
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:255
QX_FMT_NS::basic_format_string wrapper that performs additional compile time checks.
Definition: format_string.h:27
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.