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
13 #include <__cxx03/__config>
14 #include <__cxx03/__format/format_error.h>
15 #include <__cxx03/__type_traits/is_constant_evaluated.h>
16 #include <__cxx03/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
;
33 _LIBCPP_HIDE_FROM_ABI
constexpr explicit basic_format_parse_context(
34 basic_string_view
<_CharT
> __fmt
, size_t __num_args
= 0) noexcept
35 : __begin_(__fmt
.begin()),
37 __indexing_(__unknown
),
39 __num_args_(__num_args
) {}
41 basic_format_parse_context(const basic_format_parse_context
&) = delete;
42 basic_format_parse_context
& operator=(const basic_format_parse_context
&) = delete;
44 _LIBCPP_HIDE_FROM_ABI
constexpr const_iterator
begin() const noexcept
{ return __begin_
; }
45 _LIBCPP_HIDE_FROM_ABI
constexpr const_iterator
end() const noexcept
{ return __end_
; }
46 _LIBCPP_HIDE_FROM_ABI
constexpr void advance_to(const_iterator __it
) { __begin_
= __it
; }
48 _LIBCPP_HIDE_FROM_ABI
constexpr size_t next_arg_id() {
49 if (__indexing_
== __manual
)
50 std::__throw_format_error("Using automatic argument numbering in manual argument numbering mode");
52 if (__indexing_
== __unknown
)
53 __indexing_
= __automatic
;
55 // Throws an exception to make the expression a non core constant
56 // expression as required by:
57 // [format.parse.ctx]/8
58 // Remarks: Let cur-arg-id be the value of next_arg_id_ prior to this
59 // call. Call expressions where cur-arg-id >= num_args_ is true are not
60 // core constant expressions (7.7 [expr.const]).
61 // Note: the Throws clause [format.parse.ctx]/9 doesn't specify the
62 // behavior when id >= num_args_.
63 if (is_constant_evaluated() && __next_arg_id_
>= __num_args_
)
64 std::__throw_format_error("Argument index outside the valid range");
66 return __next_arg_id_
++;
68 _LIBCPP_HIDE_FROM_ABI
constexpr void check_arg_id(size_t __id
) {
69 if (__indexing_
== __automatic
)
70 std::__throw_format_error("Using manual argument numbering in automatic argument numbering mode");
72 if (__indexing_
== __unknown
)
73 __indexing_
= __manual
;
75 // Throws an exception to make the expression a non core constant
76 // expression as required by:
77 // [format.parse.ctx]/11
78 // Remarks: Call expressions where id >= num_args_ are not core constant
79 // expressions ([expr.const]).
80 // Note: the Throws clause [format.parse.ctx]/10 doesn't specify the
81 // behavior when id >= num_args_.
82 if (is_constant_evaluated() && __id
>= __num_args_
)
83 std::__throw_format_error("Argument index outside the valid range");
89 enum _Indexing
{ __unknown
, __manual
, __automatic
};
90 _Indexing __indexing_
;
91 size_t __next_arg_id_
;
94 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(basic_format_parse_context
);
96 using format_parse_context
= basic_format_parse_context
<char>;
97 # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
98 using wformat_parse_context
= basic_format_parse_context
<wchar_t>;
101 #endif //_LIBCPP_STD_VER >= 20
103 _LIBCPP_END_NAMESPACE_STD
105 #endif // _LIBCPP___FORMAT_FORMAT_PARSE_CONTEXT_H