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 # pragma clang include_instead(<format>) 20 #endif 21 22 _LIBCPP_BEGIN_NAMESPACE_STD 23 24 #if _LIBCPP_STD_VER > 17 25 26 // TODO FMT Remove this once we require compilers with proper C++20 support. 27 // If the compiler has no concepts support, the format header will be disabled. 28 // Without concepts support enable_if needs to be used and that too much effort 29 // to support compilers with partial C++20 support. 30 #if !defined(_LIBCPP_HAS_NO_CONCEPTS) 31 32 template <class _CharT> 33 class _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT basic_format_parse_context { 34 public: 35 using char_type = _CharT; 36 using const_iterator = typename basic_string_view<_CharT>::const_iterator; 37 using iterator = const_iterator; 38 39 _LIBCPP_HIDE_FROM_ABI 40 constexpr explicit basic_format_parse_context(basic_string_view<_CharT> __fmt, 41 size_t __num_args = 0) noexcept 42 : __begin_(__fmt.begin()), 43 __end_(__fmt.end()), 44 __indexing_(__unknown), 45 __next_arg_id_(0), 46 __num_args_(__num_args) {} 47 48 basic_format_parse_context(const basic_format_parse_context&) = delete; 49 basic_format_parse_context& 50 operator=(const basic_format_parse_context&) = delete; 51 52 _LIBCPP_HIDE_FROM_ABI constexpr const_iterator begin() const noexcept { 53 return __begin_; 54 } 55 _LIBCPP_HIDE_FROM_ABI constexpr const_iterator end() const noexcept { 56 return __end_; 57 } 58 _LIBCPP_HIDE_FROM_ABI constexpr void advance_to(const_iterator __it) { 59 __begin_ = __it; 60 } 61 62 _LIBCPP_HIDE_FROM_ABI constexpr size_t next_arg_id() { 63 if (__indexing_ == __manual) 64 __throw_format_error("Using automatic argument numbering in manual " 65 "argument numbering mode"); 66 67 if (__indexing_ == __unknown) 68 __indexing_ = __automatic; 69 return __next_arg_id_++; 70 } 71 _LIBCPP_HIDE_FROM_ABI constexpr void check_arg_id(size_t __id) { 72 if (__indexing_ == __automatic) 73 __throw_format_error("Using manual argument numbering in automatic " 74 "argument numbering mode"); 75 76 if (__indexing_ == __unknown) 77 __indexing_ = __manual; 78 79 // Throws an exception to make the expression a non core constant 80 // expression as required by: 81 // [format.parse.ctx]/11 82 // Remarks: Call expressions where id >= num_args_ are not core constant 83 // expressions ([expr.const]). 84 // Note: the Throws clause [format.parse.ctx]/10 doesn't specify the 85 // behavior when id >= num_args_. 86 if (is_constant_evaluated() && __id >= __num_args_) 87 __throw_format_error("Argument index outside the valid range"); 88 } 89 90 private: 91 iterator __begin_; 92 iterator __end_; 93 enum _Indexing { __unknown, __manual, __automatic }; 94 _Indexing __indexing_; 95 size_t __next_arg_id_; 96 size_t __num_args_; 97 }; 98 99 using format_parse_context = basic_format_parse_context<char>; 100 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 101 using wformat_parse_context = basic_format_parse_context<wchar_t>; 102 #endif 103 104 #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS) 105 106 #endif //_LIBCPP_STD_VER > 17 107 108 _LIBCPP_END_NAMESPACE_STD 109 110 #endif // _LIBCPP___FORMAT_FORMAT_PARSE_CONTEXT_H 111