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_WRITE_ESCAPED_H
11#define _LIBCPP___FORMAT_WRITE_ESCAPED_H
12
13#include <__algorithm/ranges_copy.h>
14#include <__algorithm/ranges_for_each.h>
15#include <__charconv/to_chars_integral.h>
16#include <__charconv/to_chars_result.h>
17#include <__chrono/statically_widen.h>
18#include <__format/escaped_output_table.h>
19#include <__format/extended_grapheme_cluster_table.h>
20#include <__format/formatter_output.h>
21#include <__format/parser_std_format_spec.h>
22#include <__format/unicode.h>
23#include <__iterator/back_insert_iterator.h>
24#include <__memory/addressof.h>
25#include <__system_error/errc.h>
26#include <__type_traits/make_unsigned.h>
27#include <__utility/move.h>
28#include <string_view>
29
30#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
31# pragma GCC system_header
32#endif
33
34_LIBCPP_PUSH_MACROS
35#include <__undef_macros>
36
37_LIBCPP_BEGIN_NAMESPACE_STD
38
39namespace __formatter {
40
41#if _LIBCPP_STD_VER >= 20
42
43/// Writes a string using format's width estimation algorithm.
44///
45/// \note When \c _LIBCPP_HAS_UNICODE is false the function assumes the input is ASCII.
46template <class _CharT>
47auto __write_string(basic_string_view<_CharT> __str,
48 output_iterator<const _CharT&> auto __out_it,
49 __format_spec::__parsed_specifications<_CharT> __specs) -> decltype(__out_it) {
50 if (!__specs.__has_precision())
51 return __formatter::__write_string_no_precision(__str, std::move(__out_it), __specs);
52
53 int __size = __formatter::__truncate(__str, __specs.__precision_);
54
55 return __formatter::__write(__str.begin(), __str.end(), std::move(__out_it), __specs, __size);
56}
57
58#endif // _LIBCPP_STD_VER >= 20
59#if _LIBCPP_STD_VER >= 23
60
61struct __nul_terminator {};
62
63template <class _CharT>
64bool operator==(const _CharT* __cstr, __nul_terminator) {
65 return *__cstr == _CharT('\0');
66}
67
68template <class _CharT>
69void __write_escaped_code_unit(basic_string<_CharT>& __str, char32_t __value, const _CharT* __prefix) {
70 back_insert_iterator __out_it{__str};
71 std::ranges::copy(__prefix, __nul_terminator{}, __out_it);
72
73 char __buffer[8];
74 to_chars_result __r = std::to_chars(first: std::begin(array&: __buffer), last: std::end(array&: __buffer), __value, base: 16);
75 _LIBCPP_ASSERT_INTERNAL(__r.ec == errc(0), "Internal buffer too small");
76 std::ranges::copy(std::begin(array&: __buffer), __r.ptr, __out_it);
77
78 __str += _CharT('}');
79}
80
81// [format.string.escaped]/2.2.1.2
82// ...
83// then the sequence \u{hex-digit-sequence} is appended to E, where
84// hex-digit-sequence is the shortest hexadecimal representation of C using
85// lower-case hexadecimal digits.
86template <class _CharT>
87void __write_well_formed_escaped_code_unit(basic_string<_CharT>& __str, char32_t __value) {
88 __formatter::__write_escaped_code_unit(__str, __value, _LIBCPP_STATICALLY_WIDEN(_CharT, "\\u{"));
89}
90
91// [format.string.escaped]/2.2.3
92// Otherwise (X is a sequence of ill-formed code units), each code unit U is
93// appended to E in order as the sequence \x{hex-digit-sequence}, where
94// hex-digit-sequence is the shortest hexadecimal representation of U using
95// lower-case hexadecimal digits.
96template <class _CharT>
97void __write_escape_ill_formed_code_unit(basic_string<_CharT>& __str, char32_t __value) {
98 __formatter::__write_escaped_code_unit(__str, __value, _LIBCPP_STATICALLY_WIDEN(_CharT, "\\x{"));
99}
100
101template <class _CharT>
102[[nodiscard]] bool __is_escaped_sequence_written(basic_string<_CharT>& __str, bool __last_escaped, char32_t __value) {
103# if !_LIBCPP_HAS_UNICODE
104 // For ASCII assume everything above 127 is printable.
105 if (__value > 127)
106 return false;
107# endif
108
109 // [format.string.escaped]/2.2.1.2.1
110 // CE is UTF-8, UTF-16, or UTF-32 and C corresponds to a Unicode scalar
111 // value whose Unicode property General_Category has a value in the groups
112 // Separator (Z) or Other (C), as described by UAX #44 of the Unicode Standard,
113 if (!__escaped_output_table::__needs_escape(code_point: __value))
114 // [format.string.escaped]/2.2.1.2.2
115 // CE is UTF-8, UTF-16, or UTF-32 and C corresponds to a Unicode scalar
116 // value with the Unicode property Grapheme_Extend=Yes as described by UAX
117 // #44 of the Unicode Standard and C is not immediately preceded in S by a
118 // character P appended to E without translation to an escape sequence,
119 if (!__last_escaped || __extended_grapheme_custer_property_boundary::__get_property(code_point: __value) !=
120 __extended_grapheme_custer_property_boundary::__property::__Extend)
121 return false;
122
123 __formatter::__write_well_formed_escaped_code_unit(__str, __value);
124 return true;
125}
126
127template <class _CharT>
128[[nodiscard]] constexpr char32_t __to_char32(_CharT __value) {
129 return static_cast<make_unsigned_t<_CharT>>(__value);
130}
131
132enum class __escape_quotation_mark { __apostrophe, __double_quote };
133
134// [format.string.escaped]/2
135template <class _CharT>
136[[nodiscard]] bool __is_escaped_sequence_written(
137 basic_string<_CharT>& __str, char32_t __value, bool __last_escaped, __escape_quotation_mark __mark) {
138 // 2.2.1.1 - Mapped character in [tab:format.escape.sequences]
139 switch (__value) {
140 case _CharT('\t'):
141 __str += _LIBCPP_STATICALLY_WIDEN(_CharT, "\\t");
142 return true;
143 case _CharT('\n'):
144 __str += _LIBCPP_STATICALLY_WIDEN(_CharT, "\\n");
145 return true;
146 case _CharT('\r'):
147 __str += _LIBCPP_STATICALLY_WIDEN(_CharT, "\\r");
148 return true;
149 case _CharT('\''):
150 if (__mark == __escape_quotation_mark::__apostrophe)
151 __str += _LIBCPP_STATICALLY_WIDEN(_CharT, R"(\')");
152 else
153 __str += __value;
154 return true;
155 case _CharT('"'):
156 if (__mark == __escape_quotation_mark::__double_quote)
157 __str += _LIBCPP_STATICALLY_WIDEN(_CharT, R"(\")");
158 else
159 __str += __value;
160 return true;
161 case _CharT('\\'):
162 __str += _LIBCPP_STATICALLY_WIDEN(_CharT, R"(\\)");
163 return true;
164
165 // 2.2.1.2 - Space
166 case _CharT(' '):
167 __str += __value;
168 return true;
169 }
170
171 // 2.2.2
172 // Otherwise, if X is a shift sequence, the effect on E and further
173 // decoding of S is unspecified.
174 // For now shift sequences are ignored and treated as Unicode. Other parts
175 // of the format library do the same. It's unknown how ostream treats them.
176 // TODO FMT determine what to do with shift sequences.
177
178 // 2.2.1.2.1 and 2.2.1.2.2 - Escape
179 return __formatter::__is_escaped_sequence_written(__str, __last_escaped, __formatter::__to_char32(__value));
180}
181
182template <class _CharT>
183void __escape(basic_string<_CharT>& __str, basic_string_view<_CharT> __values, __escape_quotation_mark __mark) {
184 __unicode::__code_point_view<_CharT> __view{__values.begin(), __values.end()};
185
186 // When the first code unit has the property Grapheme_Extend=Yes it needs to
187 // be escaped. This happens when the previous code unit was also escaped.
188 bool __escape = true;
189 while (!__view.__at_end()) {
190 auto __first = __view.__position();
191 typename __unicode::__consume_result __result = __view.__consume();
192 if (__result.__status == __unicode::__consume_result::__ok) {
193 __escape = __formatter::__is_escaped_sequence_written(__str, __result.__code_point, __escape, __mark);
194 if (!__escape)
195 // 2.2.1.3 - Add the character
196 ranges::copy(__first, __view.__position(), std::back_insert_iterator(__str));
197 } else {
198 // 2.2.3 sequence of ill-formed code units
199 ranges::for_each(__first, __view.__position(), [&](_CharT __value) {
200 __formatter::__write_escape_ill_formed_code_unit(__str, __formatter::__to_char32(__value));
201 });
202 }
203 }
204}
205
206template <class _CharT>
207auto __format_escaped_char(_CharT __value,
208 output_iterator<const _CharT&> auto __out_it,
209 __format_spec::__parsed_specifications<_CharT> __specs) -> decltype(__out_it) {
210 basic_string<_CharT> __str;
211 __str += _CharT('\'');
212 __formatter::__escape(__str, basic_string_view{std::addressof(__value), 1}, __escape_quotation_mark::__apostrophe);
213 __str += _CharT('\'');
214 return __formatter::__write(__str.data(), __str.data() + __str.size(), std::move(__out_it), __specs, __str.size());
215}
216
217template <class _CharT>
218auto __format_escaped_string(basic_string_view<_CharT> __values,
219 output_iterator<const _CharT&> auto __out_it,
220 __format_spec::__parsed_specifications<_CharT> __specs) -> decltype(__out_it) {
221 basic_string<_CharT> __str;
222 __str += _CharT('"');
223 __formatter::__escape(__str, __values, __escape_quotation_mark::__double_quote);
224 __str += _CharT('"');
225 return __formatter::__write_string(basic_string_view{__str}, std::move(__out_it), __specs);
226}
227
228#endif // _LIBCPP_STD_VER >= 23
229
230} // namespace __formatter
231
232_LIBCPP_END_NAMESPACE_STD
233
234_LIBCPP_POP_MACROS
235
236#endif // _LIBCPP___FORMAT_WRITE_ESCAPED_H
237