Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:49

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP___FORMAT_FORMAT_PARSE_CONTEXT_H
0011 #define _LIBCPP___FORMAT_FORMAT_PARSE_CONTEXT_H
0012 
0013 #include <__config>
0014 #include <__format/format_error.h>
0015 #include <__type_traits/is_constant_evaluated.h>
0016 #include <string_view>
0017 
0018 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0019 #  pragma GCC system_header
0020 #endif
0021 
0022 _LIBCPP_BEGIN_NAMESPACE_STD
0023 
0024 #if _LIBCPP_STD_VER >= 20
0025 
0026 template <class _CharT>
0027 class _LIBCPP_TEMPLATE_VIS basic_format_parse_context {
0028 public:
0029   using char_type      = _CharT;
0030   using const_iterator = typename basic_string_view<_CharT>::const_iterator;
0031   using iterator       = const_iterator;
0032 
0033   _LIBCPP_HIDE_FROM_ABI constexpr explicit basic_format_parse_context(
0034       basic_string_view<_CharT> __fmt, size_t __num_args = 0) noexcept
0035       : __begin_(__fmt.begin()),
0036         __end_(__fmt.end()),
0037         __indexing_(__unknown),
0038         __next_arg_id_(0),
0039         __num_args_(__num_args) {}
0040 
0041   basic_format_parse_context(const basic_format_parse_context&)            = delete;
0042   basic_format_parse_context& operator=(const basic_format_parse_context&) = delete;
0043 
0044   _LIBCPP_HIDE_FROM_ABI constexpr const_iterator begin() const noexcept { return __begin_; }
0045   _LIBCPP_HIDE_FROM_ABI constexpr const_iterator end() const noexcept { return __end_; }
0046   _LIBCPP_HIDE_FROM_ABI constexpr void advance_to(const_iterator __it) { __begin_ = __it; }
0047 
0048   _LIBCPP_HIDE_FROM_ABI constexpr size_t next_arg_id() {
0049     if (__indexing_ == __manual)
0050       std::__throw_format_error("Using automatic argument numbering in manual argument numbering mode");
0051 
0052     if (__indexing_ == __unknown)
0053       __indexing_ = __automatic;
0054 
0055     // Throws an exception to make the expression a non core constant
0056     // expression as required by:
0057     // [format.parse.ctx]/8
0058     //   Remarks: Let cur-arg-id be the value of next_arg_id_ prior to this
0059     //   call. Call expressions where cur-arg-id >= num_args_ is true are not
0060     //   core constant expressions (7.7 [expr.const]).
0061     // Note: the Throws clause [format.parse.ctx]/9 doesn't specify the
0062     // behavior when id >= num_args_.
0063     if (is_constant_evaluated() && __next_arg_id_ >= __num_args_)
0064       std::__throw_format_error("Argument index outside the valid range");
0065 
0066     return __next_arg_id_++;
0067   }
0068   _LIBCPP_HIDE_FROM_ABI constexpr void check_arg_id(size_t __id) {
0069     if (__indexing_ == __automatic)
0070       std::__throw_format_error("Using manual argument numbering in automatic argument numbering mode");
0071 
0072     if (__indexing_ == __unknown)
0073       __indexing_ = __manual;
0074 
0075     // Throws an exception to make the expression a non core constant
0076     // expression as required by:
0077     // [format.parse.ctx]/11
0078     //   Remarks: Call expressions where id >= num_args_ are not core constant
0079     //   expressions ([expr.const]).
0080     // Note: the Throws clause [format.parse.ctx]/10 doesn't specify the
0081     // behavior when id >= num_args_.
0082     if (is_constant_evaluated() && __id >= __num_args_)
0083       std::__throw_format_error("Argument index outside the valid range");
0084   }
0085 
0086 private:
0087   iterator __begin_;
0088   iterator __end_;
0089   enum _Indexing { __unknown, __manual, __automatic };
0090   _Indexing __indexing_;
0091   size_t __next_arg_id_;
0092   size_t __num_args_;
0093 };
0094 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(basic_format_parse_context);
0095 
0096 using format_parse_context = basic_format_parse_context<char>;
0097 #  if _LIBCPP_HAS_WIDE_CHARACTERS
0098 using wformat_parse_context = basic_format_parse_context<wchar_t>;
0099 #  endif
0100 
0101 #endif // _LIBCPP_STD_VER >= 20
0102 
0103 _LIBCPP_END_NAMESPACE_STD
0104 
0105 #endif // _LIBCPP___FORMAT_FORMAT_PARSE_CONTEXT_H