Warning: This file is not a C or C++ file. It does not have highlighting.
| 1 | // -*- C++ -*- |
|---|---|
| 2 | //===----------------------------------------------------------------------===// |
| 3 | // |
| 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 |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP___FORMAT_FORMAT_PARSE_CONTEXT_H |
| 11 | #define _LIBCPP___FORMAT_FORMAT_PARSE_CONTEXT_H |
| 12 | |
| 13 | #include <__config> |
| 14 | #include <__format/format_error.h> |
| 15 | #include <__type_traits/is_constant_evaluated.h> |
| 16 | #include <string_view> |
| 17 | |
| 18 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 19 | # pragma GCC system_header |
| 20 | #endif |
| 21 | |
| 22 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 23 | |
| 24 | #if _LIBCPP_STD_VER >= 20 |
| 25 | |
| 26 | template <class _CharT> |
| 27 | class basic_format_parse_context { |
| 28 | public: |
| 29 | using char_type = _CharT; |
| 30 | using const_iterator = typename basic_string_view<_CharT>::const_iterator; |
| 31 | using iterator = const_iterator; |
| 32 | |
| 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()), |
| 36 | __end_(__fmt.end()), |
| 37 | __indexing_(__unknown), |
| 38 | __next_arg_id_(0), |
| 39 | __num_args_(__num_args) {} |
| 40 | |
| 41 | basic_format_parse_context(const basic_format_parse_context&) = delete; |
| 42 | basic_format_parse_context& operator=(const basic_format_parse_context&) = delete; |
| 43 | |
| 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; } |
| 47 | |
| 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"); |
| 51 | |
| 52 | if (__indexing_ == __unknown) |
| 53 | __indexing_ = __automatic; |
| 54 | |
| 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"); |
| 65 | |
| 66 | return __next_arg_id_++; |
| 67 | } |
| 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"); |
| 71 | |
| 72 | if (__indexing_ == __unknown) |
| 73 | __indexing_ = __manual; |
| 74 | |
| 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"); |
| 84 | } |
| 85 | |
| 86 | private: |
| 87 | iterator __begin_; |
| 88 | iterator __end_; |
| 89 | enum _Indexing { __unknown, __manual, __automatic }; |
| 90 | _Indexing __indexing_; |
| 91 | size_t __next_arg_id_; |
| 92 | size_t __num_args_; |
| 93 | }; |
| 94 | _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(basic_format_parse_context); |
| 95 | |
| 96 | using format_parse_context = basic_format_parse_context<char>; |
| 97 | # if _LIBCPP_HAS_WIDE_CHARACTERS |
| 98 | using wformat_parse_context = basic_format_parse_context<wchar_t>; |
| 99 | # endif |
| 100 | |
| 101 | #endif // _LIBCPP_STD_VER >= 20 |
| 102 | |
| 103 | _LIBCPP_END_NAMESPACE_STD |
| 104 | |
| 105 | #endif // _LIBCPP___FORMAT_FORMAT_PARSE_CONTEXT_H |
| 106 |
Warning: This file is not a C or C++ file. It does not have highlighting.
