xref: /freebsd-src/contrib/llvm-project/libcxx/include/__format/format_string.h (revision c9ccf3a32da427475985b85d7df023ccfb138c27)
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_STRING_H
11 #define _LIBCPP___FORMAT_FORMAT_STRING_H
12 
13 #include <__config>
14 #include <__debug>
15 #include <__format/format_error.h>
16 #include <cstddef>
17 #include <cstdint>
18 
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 #pragma GCC system_header
21 #endif
22 
23 _LIBCPP_BEGIN_NAMESPACE_STD
24 
25 #if _LIBCPP_STD_VER > 17
26 
27 // TODO FMT Remove this once we require compilers with proper C++20 support.
28 // If the compiler has no concepts support, the format header will be disabled.
29 // Without concepts support enable_if needs to be used and that too much effort
30 // to support compilers with partial C++20 support.
31 #if !defined(_LIBCPP_HAS_NO_CONCEPTS)
32 
33 namespace __format {
34 
35 template <class _CharT>
36 struct _LIBCPP_TEMPLATE_VIS __parse_number_result {
37   const _CharT* __ptr;
38   uint32_t __value;
39 };
40 
41 template <class _CharT>
42 _LIBCPP_HIDE_FROM_ABI constexpr __parse_number_result<_CharT>
43 __parse_number(const _CharT* __begin, const _CharT* __end);
44 
45 /**
46  * The maximum value of a numeric argument.
47  *
48  * This is used for:
49  * * arg-id
50  * * width as value or arg-id.
51  * * precision as value or arg-id.
52  *
53  * The value is compatible with the maximum formatting width and precision
54  * using the `%*` syntax on a 32-bit system.
55  */
56 inline constexpr uint32_t __number_max = INT32_MAX;
57 
58 namespace __detail {
59 template <class _CharT>
60 _LIBCPP_HIDE_FROM_ABI constexpr __parse_number_result<_CharT>
61 __parse_zero(const _CharT* __begin, const _CharT*, auto& __parse_ctx) {
62   __parse_ctx.check_arg_id(0);
63   return {++__begin, 0}; // can never be larger than the maximum.
64 }
65 
66 template <class _CharT>
67 _LIBCPP_HIDE_FROM_ABI constexpr __parse_number_result<_CharT>
68 __parse_automatic(const _CharT* __begin, const _CharT*, auto& __parse_ctx) {
69   size_t __value = __parse_ctx.next_arg_id();
70   _LIBCPP_ASSERT(__value <= __number_max,
71                  "Compilers don't support this number of arguments");
72 
73   return {__begin, uint32_t(__value)};
74 }
75 
76 template <class _CharT>
77 _LIBCPP_HIDE_FROM_ABI constexpr __parse_number_result<_CharT>
78 __parse_manual(const _CharT* __begin, const _CharT* __end, auto& __parse_ctx) {
79   __parse_number_result<_CharT> __r = __parse_number(__begin, __end);
80   __parse_ctx.check_arg_id(__r.__value);
81   return __r;
82 }
83 
84 } // namespace __detail
85 
86 /**
87  * Parses a number.
88  *
89  * The number is used for the 31-bit values @em width and @em precision. This
90  * allows a maximum value of 2147483647.
91  */
92 template <class _CharT>
93 _LIBCPP_HIDE_FROM_ABI constexpr __parse_number_result<_CharT>
94 __parse_number(const _CharT* __begin, const _CharT* __end_input) {
95   static_assert(__format::__number_max == INT32_MAX,
96                 "The algorithm is implemented based on this value.");
97   /*
98    * Limit the input to 9 digits, otherwise we need two checks during every
99    * iteration:
100    * - Are we at the end of the input?
101    * - Does the value exceed width of an uint32_t? (Switching to uint64_t would
102    *   have the same issue, but with a higher maximum.)
103    */
104   const _CharT* __end = __end_input - __begin > 9 ? __begin + 9 : __end_input;
105   uint32_t __value = *__begin - _CharT('0');
106   while (++__begin != __end) {
107     if (*__begin < _CharT('0') || *__begin > _CharT('9'))
108       return {__begin, __value};
109 
110     __value = __value * 10 + *__begin - _CharT('0');
111   }
112 
113   if (__begin != __end_input && *__begin >= _CharT('0') &&
114       *__begin <= _CharT('9')) {
115 
116     /*
117      * There are more than 9 digits, do additional validations:
118      * - Does the 10th digit exceed the maximum allowed value?
119      * - Are there more than 10 digits?
120      * (More than 10 digits always overflows the maximum.)
121      */
122     uint64_t __v = uint64_t(__value) * 10 + *__begin++ - _CharT('0');
123     if (__v > __number_max ||
124         (__begin != __end_input && *__begin >= _CharT('0') &&
125          *__begin <= _CharT('9')))
126       __throw_format_error("The numeric value of the format-spec is too large");
127 
128     __value = __v;
129   }
130 
131   return {__begin, __value};
132 }
133 
134 /**
135  * Multiplexer for all parse functions.
136  *
137  * The parser will return a pointer beyond the last consumed character. This
138  * should be the closing '}' of the arg-id.
139  */
140 template <class _CharT>
141 _LIBCPP_HIDE_FROM_ABI constexpr __parse_number_result<_CharT>
142 __parse_arg_id(const _CharT* __begin, const _CharT* __end, auto& __parse_ctx) {
143   switch (*__begin) {
144   case _CharT('0'):
145     return __detail::__parse_zero(__begin, __end, __parse_ctx);
146 
147   case _CharT(':'):
148     // This case is conditionally valid. It's allowed in an arg-id in the
149     // replacement-field, but not in the std-format-spec. The caller can
150     // provide a better diagnostic, so accept it here unconditionally.
151   case _CharT('}'):
152     return __detail::__parse_automatic(__begin, __end, __parse_ctx);
153   }
154   if (*__begin < _CharT('0') || *__begin > _CharT('9'))
155     __throw_format_error(
156         "The arg-id of the format-spec starts with an invalid character");
157 
158   return __detail::__parse_manual(__begin, __end, __parse_ctx);
159 }
160 
161 } // namespace __format
162 
163 #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)
164 
165 #endif //_LIBCPP_STD_VER > 17
166 
167 _LIBCPP_END_NAMESPACE_STD
168 
169 #endif // _LIBCPP___FORMAT_FORMAT_STRING_H
170