2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___FORMAT_FORMAT_PARSE_CONTEXT_H
11 #define _LIBCPP___FORMAT_FORMAT_PARSE_CONTEXT_H
14 #include <__format/format_error.h>
15 #include <__type_traits/is_constant_evaluated.h>
16 #include <string_view>
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 # pragma GCC system_header
22 _LIBCPP_BEGIN_NAMESPACE_STD
24 #if _LIBCPP_STD_VER >= 20
26 template <class _CharT
>
27 class _LIBCPP_TEMPLATE_VIS basic_format_parse_context
{
29 using char_type
= _CharT
;
30 using const_iterator
= typename basic_string_view
<_CharT
>::const_iterator
;
31 using iterator
= const_iterator
;
34 constexpr explicit basic_format_parse_context(basic_string_view
<_CharT
> __fmt
,
35 size_t __num_args
= 0) noexcept
36 : __begin_(__fmt
.begin()),
38 __indexing_(__unknown
),
40 __num_args_(__num_args
) {}
42 basic_format_parse_context(const basic_format_parse_context
&) = delete;
43 basic_format_parse_context
&
44 operator=(const basic_format_parse_context
&) = delete;
46 _LIBCPP_HIDE_FROM_ABI
constexpr const_iterator
begin() const noexcept
{
49 _LIBCPP_HIDE_FROM_ABI
constexpr const_iterator
end() const noexcept
{
52 _LIBCPP_HIDE_FROM_ABI
constexpr void advance_to(const_iterator __it
) {
56 _LIBCPP_HIDE_FROM_ABI
constexpr size_t next_arg_id() {
57 if (__indexing_
== __manual
)
58 std::__throw_format_error("Using automatic argument numbering in manual argument numbering mode");
60 if (__indexing_
== __unknown
)
61 __indexing_
= __automatic
;
63 // Throws an exception to make the expression a non core constant
64 // expression as required by:
65 // [format.parse.ctx]/8
66 // Remarks: Let cur-arg-id be the value of next_arg_id_ prior to this
67 // call. Call expressions where cur-arg-id >= num_args_ is true are not
68 // core constant expressions (7.7 [expr.const]).
69 // Note: the Throws clause [format.parse.ctx]/9 doesn't specify the
70 // behavior when id >= num_args_.
71 if (is_constant_evaluated() && __next_arg_id_
>= __num_args_
)
72 std::__throw_format_error("Argument index outside the valid range");
74 return __next_arg_id_
++;
76 _LIBCPP_HIDE_FROM_ABI
constexpr void check_arg_id(size_t __id
) {
77 if (__indexing_
== __automatic
)
78 std::__throw_format_error("Using manual argument numbering in automatic argument numbering mode");
80 if (__indexing_
== __unknown
)
81 __indexing_
= __manual
;
83 // Throws an exception to make the expression a non core constant
84 // expression as required by:
85 // [format.parse.ctx]/11
86 // Remarks: Call expressions where id >= num_args_ are not core constant
87 // expressions ([expr.const]).
88 // Note: the Throws clause [format.parse.ctx]/10 doesn't specify the
89 // behavior when id >= num_args_.
90 if (is_constant_evaluated() && __id
>= __num_args_
)
91 std::__throw_format_error("Argument index outside the valid range");
97 enum _Indexing
{ __unknown
, __manual
, __automatic
};
98 _Indexing __indexing_
;
99 size_t __next_arg_id_
;
102 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(basic_format_parse_context
);
104 using format_parse_context
= basic_format_parse_context
<char>;
105 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
106 using wformat_parse_context
= basic_format_parse_context
<wchar_t>;
109 #endif //_LIBCPP_STD_VER >= 20
111 _LIBCPP_END_NAMESPACE_STD
113 #endif // _LIBCPP___FORMAT_FORMAT_PARSE_CONTEXT_H