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 <string_view> 16 17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 18 # pragma GCC system_header 19 #endif 20 21 _LIBCPP_BEGIN_NAMESPACE_STD 22 23 #if _LIBCPP_STD_VER > 17 24 25 template <class _CharT> 26 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT basic_format_parse_context { 27 public: 28 using char_type = _CharT; 29 using const_iterator = typename basic_string_view<_CharT>::const_iterator; 30 using iterator = const_iterator; 31 32 _LIBCPP_HIDE_FROM_ABI 33 constexpr explicit basic_format_parse_context(basic_string_view<_CharT> __fmt, 34 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& 43 operator=(const basic_format_parse_context&) = delete; 44 45 _LIBCPP_HIDE_FROM_ABI constexpr const_iterator begin() const noexcept { 46 return __begin_; 47 } 48 _LIBCPP_HIDE_FROM_ABI constexpr const_iterator end() const noexcept { 49 return __end_; 50 } 51 _LIBCPP_HIDE_FROM_ABI constexpr void advance_to(const_iterator __it) { 52 __begin_ = __it; 53 } 54 55 _LIBCPP_HIDE_FROM_ABI constexpr size_t next_arg_id() { 56 if (__indexing_ == __manual) 57 __throw_format_error("Using automatic argument numbering in manual " 58 "argument numbering mode"); 59 60 if (__indexing_ == __unknown) 61 __indexing_ = __automatic; 62 return __next_arg_id_++; 63 } 64 _LIBCPP_HIDE_FROM_ABI constexpr void check_arg_id(size_t __id) { 65 if (__indexing_ == __automatic) 66 __throw_format_error("Using manual argument numbering in automatic " 67 "argument numbering mode"); 68 69 if (__indexing_ == __unknown) 70 __indexing_ = __manual; 71 72 // Throws an exception to make the expression a non core constant 73 // expression as required by: 74 // [format.parse.ctx]/11 75 // Remarks: Call expressions where id >= num_args_ are not core constant 76 // expressions ([expr.const]). 77 // Note: the Throws clause [format.parse.ctx]/10 doesn't specify the 78 // behavior when id >= num_args_. 79 if (is_constant_evaluated() && __id >= __num_args_) 80 __throw_format_error("Argument index outside the valid range"); 81 } 82 83 private: 84 iterator __begin_; 85 iterator __end_; 86 enum _Indexing { __unknown, __manual, __automatic }; 87 _Indexing __indexing_; 88 size_t __next_arg_id_; 89 size_t __num_args_; 90 }; 91 92 using format_parse_context = basic_format_parse_context<char>; 93 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 94 using wformat_parse_context = basic_format_parse_context<wchar_t>; 95 #endif 96 97 #endif //_LIBCPP_STD_VER > 17 98 99 _LIBCPP_END_NAMESPACE_STD 100 101 #endif // _LIBCPP___FORMAT_FORMAT_PARSE_CONTEXT_H 102