qxLib
format_string.inl
Go to the documentation of this file.
1 /**
2 
3  @file format_string.inl
4  @author Khrapov
5  @date 16.06.2023
6  @copyright (c) Nick Khrapov, 2023. All right reserved.
7 
8 **/
9 
10 namespace qx
11 {
12 
13 template<class char_t, class... args_t>
14 template<class T>
15  requires std::convertible_to<const T&, qx::basic_string_view<char_t>>
17  : QX_FMT_NS::basic_format_string<char_t, args_t...>(parse_format_string(value))
18 {
19 }
20 
21 template<class char_t, class... args_t>
22 template<class T>
24 {
25  size_t nBracesBalance = 0;
26  size_t nNumBracesPairs = 0;
27  basic_string_view<char_t> svFormatString = value;
28 
29  for (size_t i = 0; i < svFormatString.size(); ++i)
30  {
31  if (svFormatString[i] == QX_CHAR_PREFIX(char_t, '{'))
32  {
33  if (i + 1 < svFormatString.size() && svFormatString[i + 1] == QX_CHAR_PREFIX(char_t, '{'))
34  {
35  ++i;
36  continue;
37  }
38 
39  ++nBracesBalance;
40  }
41  else if (svFormatString[i] == QX_CHAR_PREFIX(char_t, '}'))
42  {
43  if (i + 1 < svFormatString.size() && svFormatString[i + 1] == QX_CHAR_PREFIX(char_t, '}'))
44  {
45  ++i;
46  continue;
47  }
48 
49  ++nNumBracesPairs;
50  if (nBracesBalance == 0)
51  {
52  throw std::format_error(
53  "Format string error: num of closing braces > num of opening braces at some point of the format "
54  "string");
55  }
56  --nBracesBalance;
57  }
58  }
59 
60  if (nBracesBalance > 0)
61  {
62  throw std::format_error(
63  "Format string error: num of opening braces > num of closing braces at the end of the format string");
64  }
65 
66  if (sizeof...(args_t) != nNumBracesPairs)
67  throw std::format_error("Format string error: num of args != num of braces pairs");
68 
69  return value;
70 }
71 
72 } // namespace qx
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:128
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.