Warning: This file is not a C or C++ file. It does not have highlighting.
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_FORMATTER_STRING_H |
11 | #define _LIBCPP___FORMAT_FORMATTER_STRING_H |
12 | |
13 | #include <__assert> |
14 | #include <__config> |
15 | #include <__format/concepts.h> |
16 | #include <__format/format_parse_context.h> |
17 | #include <__format/formatter.h> |
18 | #include <__format/formatter_output.h> |
19 | #include <__format/parser_std_format_spec.h> |
20 | #include <__format/write_escaped.h> |
21 | #include <cstddef> |
22 | #include <string> |
23 | #include <string_view> |
24 | |
25 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
26 | # pragma GCC system_header |
27 | #endif |
28 | |
29 | _LIBCPP_BEGIN_NAMESPACE_STD |
30 | |
31 | #if _LIBCPP_STD_VER >= 20 |
32 | |
33 | template <__fmt_char_type _CharT> |
34 | struct __formatter_string { |
35 | public: |
36 | template <class _ParseContext> |
37 | _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) { |
38 | typename _ParseContext::iterator __result = __parser_.__parse(__ctx, __format_spec::__fields_string); |
39 | __format_spec::__process_display_type_string(__parser_.__type_); |
40 | return __result; |
41 | } |
42 | |
43 | template <class _FormatContext> |
44 | _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator |
45 | format(basic_string_view<_CharT> __str, _FormatContext& __ctx) const { |
46 | # if _LIBCPP_STD_VER >= 23 |
47 | if (__parser_.__type_ == __format_spec::__type::__debug) |
48 | return __formatter::__format_escaped_string(__str, __ctx.out(), __parser_.__get_parsed_std_specifications(__ctx)); |
49 | # endif |
50 | |
51 | return __formatter::__write_string(__str, __ctx.out(), __parser_.__get_parsed_std_specifications(__ctx)); |
52 | } |
53 | |
54 | # if _LIBCPP_STD_VER >= 23 |
55 | _LIBCPP_HIDE_FROM_ABI constexpr void set_debug_format() { __parser_.__type_ = __format_spec::__type::__debug; } |
56 | # endif |
57 | |
58 | __format_spec::__parser<_CharT> __parser_{.__alignment_ = __format_spec::__alignment::__left}; |
59 | }; |
60 | |
61 | // Formatter const char*. |
62 | template <__fmt_char_type _CharT> |
63 | struct formatter<const _CharT*, _CharT> : public __formatter_string<_CharT> { |
64 | using _Base _LIBCPP_NODEBUG = __formatter_string<_CharT>; |
65 | |
66 | template <class _FormatContext> |
67 | _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(const _CharT* __str, _FormatContext& __ctx) const { |
68 | _LIBCPP_ASSERT_INTERNAL(__str, "The basic_format_arg constructor should have prevented an invalid pointer."); |
69 | // Converting the input to a basic_string_view means the data is looped over twice; |
70 | // - once to determine the length, and |
71 | // - once to process the data. |
72 | // |
73 | // This sounds slower than writing the output directly. However internally |
74 | // the output algorithms have optimizations for "bulk" operations, which |
75 | // makes this faster than a single-pass character-by-character output. |
76 | return _Base::format(basic_string_view<_CharT>(__str), __ctx); |
77 | } |
78 | }; |
79 | |
80 | // Formatter char*. |
81 | template <__fmt_char_type _CharT> |
82 | struct formatter<_CharT*, _CharT> : public formatter<const _CharT*, _CharT> { |
83 | using _Base _LIBCPP_NODEBUG = formatter<const _CharT*, _CharT>; |
84 | |
85 | template <class _FormatContext> |
86 | _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(_CharT* __str, _FormatContext& __ctx) const { |
87 | return _Base::format(__str, __ctx); |
88 | } |
89 | }; |
90 | |
91 | // Formatter char[]. |
92 | template <__fmt_char_type _CharT, size_t _Size> |
93 | struct formatter<_CharT[_Size], _CharT> : public __formatter_string<_CharT> { |
94 | using _Base _LIBCPP_NODEBUG = __formatter_string<_CharT>; |
95 | |
96 | template <class _FormatContext> |
97 | _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator |
98 | format(const _CharT (&__str)[_Size], _FormatContext& __ctx) const { |
99 | const _CharT* const __pzero = char_traits<_CharT>::find(__str, _Size, _CharT{}); |
100 | _LIBCPP_ASSERT_VALID_INPUT_RANGE(__pzero != nullptr, "formatting a non-null-terminated array"); |
101 | return _Base::format(basic_string_view<_CharT>(__str, static_cast<size_t>(__pzero - __str)), __ctx); |
102 | } |
103 | }; |
104 | |
105 | // Formatter std::string. |
106 | template <__fmt_char_type _CharT, class _Traits, class _Allocator> |
107 | struct formatter<basic_string<_CharT, _Traits, _Allocator>, _CharT> : public __formatter_string<_CharT> { |
108 | using _Base _LIBCPP_NODEBUG = __formatter_string<_CharT>; |
109 | |
110 | template <class _FormatContext> |
111 | _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator |
112 | format(const basic_string<_CharT, _Traits, _Allocator>& __str, _FormatContext& __ctx) const { |
113 | // Drop _Traits and _Allocator to have one std::basic_string formatter. |
114 | return _Base::format(basic_string_view<_CharT>(__str.data(), __str.size()), __ctx); |
115 | } |
116 | }; |
117 | |
118 | // Formatter std::string_view. |
119 | template <__fmt_char_type _CharT, class _Traits> |
120 | struct formatter<basic_string_view<_CharT, _Traits>, _CharT> : public __formatter_string<_CharT> { |
121 | using _Base _LIBCPP_NODEBUG = __formatter_string<_CharT>; |
122 | |
123 | template <class _FormatContext> |
124 | _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator |
125 | format(basic_string_view<_CharT, _Traits> __str, _FormatContext& __ctx) const { |
126 | // Drop _Traits to have one std::basic_string_view formatter. |
127 | return _Base::format(basic_string_view<_CharT>(__str.data(), __str.size()), __ctx); |
128 | } |
129 | }; |
130 | |
131 | # if _LIBCPP_HAS_WIDE_CHARACTERS |
132 | template <> |
133 | struct formatter<char*, wchar_t> : __disabled_formatter {}; |
134 | template <> |
135 | struct formatter<const char*, wchar_t> : __disabled_formatter {}; |
136 | template <size_t _Size> |
137 | struct formatter<char[_Size], wchar_t> : __disabled_formatter {}; |
138 | template <class _Traits, class _Allocator> |
139 | struct formatter<basic_string<char, _Traits, _Allocator>, wchar_t> : __disabled_formatter {}; |
140 | template <class _Traits> |
141 | struct formatter<basic_string_view<char, _Traits>, wchar_t> : __disabled_formatter {}; |
142 | # endif // _LIBCPP_HAS_WIDE_CHARACTERS |
143 | |
144 | # if _LIBCPP_STD_VER >= 23 |
145 | template <> |
146 | inline constexpr bool enable_nonlocking_formatter_optimization<char*> = true; |
147 | template <> |
148 | inline constexpr bool enable_nonlocking_formatter_optimization<const char*> = true; |
149 | template <size_t _Size> |
150 | inline constexpr bool enable_nonlocking_formatter_optimization<char[_Size]> = true; |
151 | template <class _Traits, class _Allocator> |
152 | inline constexpr bool enable_nonlocking_formatter_optimization<basic_string<char, _Traits, _Allocator>> = true; |
153 | template <class _Traits> |
154 | inline constexpr bool enable_nonlocking_formatter_optimization<basic_string_view<char, _Traits>> = true; |
155 | |
156 | # if _LIBCPP_HAS_WIDE_CHARACTERS |
157 | template <> |
158 | inline constexpr bool enable_nonlocking_formatter_optimization<wchar_t*> = true; |
159 | template <> |
160 | inline constexpr bool enable_nonlocking_formatter_optimization<const wchar_t*> = true; |
161 | template <size_t _Size> |
162 | inline constexpr bool enable_nonlocking_formatter_optimization<wchar_t[_Size]> = true; |
163 | template <class _Traits, class _Allocator> |
164 | inline constexpr bool enable_nonlocking_formatter_optimization<basic_string<wchar_t, _Traits, _Allocator>> = true; |
165 | template <class _Traits> |
166 | inline constexpr bool enable_nonlocking_formatter_optimization<basic_string_view<wchar_t, _Traits>> = true; |
167 | # endif // _LIBCPP_HAS_WIDE_CHARACTERS |
168 | # endif // _LIBCPP_STD_VER >= 23 |
169 | #endif // _LIBCPP_STD_VER >= 20 |
170 | |
171 | _LIBCPP_END_NAMESPACE_STD |
172 | |
173 | #endif // _LIBCPP___FORMAT_FORMATTER_STRING_H |
174 |
Warning: This file is not a C or C++ file. It does not have highlighting.