brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.8 KiB · 67b90c7 Raw
106 lines · c
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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___FORMAT_FORMAT_PARSE_CONTEXT_H11#define _LIBCPP___FORMAT_FORMAT_PARSE_CONTEXT_H12 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_header20#endif21 22_LIBCPP_BEGIN_NAMESPACE_STD23 24#if _LIBCPP_STD_VER >= 2025 26template <class _CharT>27class basic_format_parse_context {28public: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) noexcept35      : __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 constant56    // expression as required by:57    // [format.parse.ctx]/858    //   Remarks: Let cur-arg-id be the value of next_arg_id_ prior to this59    //   call. Call expressions where cur-arg-id >= num_args_ is true are not60    //   core constant expressions (7.7 [expr.const]).61    // Note: the Throws clause [format.parse.ctx]/9 doesn't specify the62    // 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 constant76    // expression as required by:77    // [format.parse.ctx]/1178    //   Remarks: Call expressions where id >= num_args_ are not core constant79    //   expressions ([expr.const]).80    // Note: the Throws clause [format.parse.ctx]/10 doesn't specify the81    // 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 86private: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 96using format_parse_context = basic_format_parse_context<char>;97#  if _LIBCPP_HAS_WIDE_CHARACTERS98using wformat_parse_context = basic_format_parse_context<wchar_t>;99#  endif100 101#endif // _LIBCPP_STD_VER >= 20102 103_LIBCPP_END_NAMESPACE_STD104 105#endif // _LIBCPP___FORMAT_FORMAT_PARSE_CONTEXT_H106