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_IOMANIP
11#define _LIBCPP_IOMANIP
12
13/*
14 iomanip synopsis
15
16namespace std {
17
18// types T1, T2, ... are unspecified implementation types
19T1 resetiosflags(ios_base::fmtflags mask);
20T2 setiosflags (ios_base::fmtflags mask);
21T3 setbase(int base);
22template<charT> T4 setfill(charT c);
23T5 setprecision(int n);
24T6 setw(int n);
25template <class moneyT> T7 get_money(moneyT& mon, bool intl = false);
26template <class charT, class moneyT> T8 put_money(const moneyT& mon, bool intl = false);
27template <class charT> T9 get_time(struct tm* tmb, const charT* fmt);
28template <class charT> T10 put_time(const struct tm* tmb, const charT* fmt);
29
30template <class charT>
31 T11 quoted(const charT* s, charT delim=charT('"'), charT escape=charT('\\')); // C++14
32
33template <class charT, class traits, class Allocator>
34 T12 quoted(const basic_string<charT, traits, Allocator>& s,
35 charT delim=charT('"'), charT escape=charT('\\')); // C++14
36
37template <class charT, class traits, class Allocator>
38 T13 quoted(basic_string<charT, traits, Allocator>& s,
39 charT delim=charT('"'), charT escape=charT('\\')); // C++14
40
41} // std
42
43*/
44
45#include <__config>
46#include <istream>
47#include <version>
48
49#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
50# pragma GCC system_header
51#endif
52
53_LIBCPP_BEGIN_NAMESPACE_STD
54
55// resetiosflags
56
57class __iom_t1 {
58 ios_base::fmtflags __mask_;
59
60public:
61 _LIBCPP_HIDE_FROM_ABI explicit __iom_t1(ios_base::fmtflags __m) : __mask_(__m) {}
62
63 template <class _CharT, class _Traits>
64 friend _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
65 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t1& __x) {
66 __is.unsetf(__x.__mask_);
67 return __is;
68 }
69
70 template <class _CharT, class _Traits>
71 friend _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
72 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t1& __x) {
73 __os.unsetf(__x.__mask_);
74 return __os;
75 }
76};
77
78inline _LIBCPP_HIDE_FROM_ABI __iom_t1 resetiosflags(ios_base::fmtflags __mask) { return __iom_t1(__mask); }
79
80// setiosflags
81
82class __iom_t2 {
83 ios_base::fmtflags __mask_;
84
85public:
86 _LIBCPP_HIDE_FROM_ABI explicit __iom_t2(ios_base::fmtflags __m) : __mask_(__m) {}
87
88 template <class _CharT, class _Traits>
89 friend _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
90 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t2& __x) {
91 __is.setf(__x.__mask_);
92 return __is;
93 }
94
95 template <class _CharT, class _Traits>
96 friend _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
97 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t2& __x) {
98 __os.setf(__x.__mask_);
99 return __os;
100 }
101};
102
103inline _LIBCPP_HIDE_FROM_ABI __iom_t2 setiosflags(ios_base::fmtflags __mask) { return __iom_t2(__mask); }
104
105// setbase
106
107class __iom_t3 {
108 int __base_;
109
110public:
111 _LIBCPP_HIDE_FROM_ABI explicit __iom_t3(int __b) : __base_(__b) {}
112
113 template <class _CharT, class _Traits>
114 friend _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
115 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t3& __x) {
116 __is.setf(__x.__base_ == 8 ? ios_base::oct
117 : __x.__base_ == 10 ? ios_base::dec
118 : __x.__base_ == 16 ? ios_base::hex
119 : ios_base::fmtflags(0),
120 ios_base::basefield);
121 return __is;
122 }
123
124 template <class _CharT, class _Traits>
125 friend _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
126 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t3& __x) {
127 __os.setf(__x.__base_ == 8 ? ios_base::oct
128 : __x.__base_ == 10 ? ios_base::dec
129 : __x.__base_ == 16 ? ios_base::hex
130 : ios_base::fmtflags(0),
131 ios_base::basefield);
132 return __os;
133 }
134};
135
136inline _LIBCPP_HIDE_FROM_ABI __iom_t3 setbase(int __base) { return __iom_t3(__base); }
137
138// setfill
139
140template <class _CharT>
141class __iom_t4 {
142 _CharT __fill_;
143
144public:
145 _LIBCPP_HIDE_FROM_ABI explicit __iom_t4(_CharT __c) : __fill_(__c) {}
146
147 template <class _Traits>
148 friend _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
149 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t4& __x) {
150 __os.fill(__x.__fill_);
151 return __os;
152 }
153};
154
155template <class _CharT>
156inline _LIBCPP_HIDE_FROM_ABI __iom_t4<_CharT> setfill(_CharT __c) {
157 return __iom_t4<_CharT>(__c);
158}
159
160// setprecision
161
162class __iom_t5 {
163 int __n_;
164
165public:
166 _LIBCPP_HIDE_FROM_ABI explicit __iom_t5(int __n) : __n_(__n) {}
167
168 template <class _CharT, class _Traits>
169 friend _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
170 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t5& __x) {
171 __is.precision(__x.__n_);
172 return __is;
173 }
174
175 template <class _CharT, class _Traits>
176 friend _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
177 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t5& __x) {
178 __os.precision(__x.__n_);
179 return __os;
180 }
181};
182
183inline _LIBCPP_HIDE_FROM_ABI __iom_t5 setprecision(int __n) { return __iom_t5(__n); }
184
185// setw
186
187class __iom_t6 {
188 int __n_;
189
190public:
191 _LIBCPP_HIDE_FROM_ABI explicit __iom_t6(int __n) : __n_(__n) {}
192
193 template <class _CharT, class _Traits>
194 friend _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
195 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t6& __x) {
196 __is.width(__x.__n_);
197 return __is;
198 }
199
200 template <class _CharT, class _Traits>
201 friend _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
202 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t6& __x) {
203 __os.width(__x.__n_);
204 return __os;
205 }
206};
207
208inline _LIBCPP_HIDE_FROM_ABI __iom_t6 setw(int __n) { return __iom_t6(__n); }
209
210// get_money
211
212template <class _MoneyT>
213class __iom_t7;
214
215template <class _CharT, class _Traits, class _MoneyT>
216_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
217operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x);
218
219template <class _MoneyT>
220class __iom_t7 {
221 _MoneyT& __mon_;
222 bool __intl_;
223
224public:
225 _LIBCPP_HIDE_FROM_ABI __iom_t7(_MoneyT& __mon, bool __intl) : __mon_(__mon), __intl_(__intl) {}
226
227 template <class _CharT, class _Traits, class _Mp>
228 friend basic_istream<_CharT, _Traits>& operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_Mp>& __x);
229};
230
231template <class _CharT, class _Traits, class _MoneyT>
232_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
233operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x) {
234#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
235 try {
236#endif // _LIBCPP_HAS_NO_EXCEPTIONS
237 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
238 if (__s) {
239 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
240 typedef money_get<_CharT, _Ip> _Fp;
241 ios_base::iostate __err = ios_base::goodbit;
242 const _Fp& __mf = std::use_facet<_Fp>(__is.getloc());
243 __mf.get(_Ip(__is), _Ip(), __x.__intl_, __is, __err, __x.__mon_);
244 __is.setstate(__err);
245 }
246#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
247 } catch (...) {
248 __is.__set_badbit_and_consider_rethrow();
249 }
250#endif // _LIBCPP_HAS_NO_EXCEPTIONS
251 return __is;
252}
253
254template <class _MoneyT>
255inline _LIBCPP_HIDE_FROM_ABI __iom_t7<_MoneyT> get_money(_MoneyT& __mon, bool __intl = false) {
256 return __iom_t7<_MoneyT>(__mon, __intl);
257}
258
259// put_money
260
261template <class _MoneyT>
262class __iom_t8;
263
264template <class _CharT, class _Traits, class _MoneyT>
265_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
266operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x);
267
268template <class _MoneyT>
269class __iom_t8 {
270 const _MoneyT& __mon_;
271 bool __intl_;
272
273public:
274 _LIBCPP_HIDE_FROM_ABI __iom_t8(const _MoneyT& __mon, bool __intl) : __mon_(__mon), __intl_(__intl) {}
275
276 template <class _CharT, class _Traits, class _Mp>
277 friend basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_Mp>& __x);
278};
279
280template <class _CharT, class _Traits, class _MoneyT>
281_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
282operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x) {
283#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
284 try {
285#endif // _LIBCPP_HAS_NO_EXCEPTIONS
286 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
287 if (__s) {
288 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
289 typedef money_put<_CharT, _Op> _Fp;
290 const _Fp& __mf = std::use_facet<_Fp>(__os.getloc());
291 if (__mf.put(_Op(__os), __x.__intl_, __os, __os.fill(), __x.__mon_).failed())
292 __os.setstate(ios_base::badbit);
293 }
294#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
295 } catch (...) {
296 __os.__set_badbit_and_consider_rethrow();
297 }
298#endif // _LIBCPP_HAS_NO_EXCEPTIONS
299 return __os;
300}
301
302template <class _MoneyT>
303inline _LIBCPP_HIDE_FROM_ABI __iom_t8<_MoneyT> put_money(const _MoneyT& __mon, bool __intl = false) {
304 return __iom_t8<_MoneyT>(__mon, __intl);
305}
306
307// get_time
308
309template <class _CharT>
310class __iom_t9;
311
312template <class _CharT, class _Traits>
313_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
314operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x);
315
316template <class _CharT>
317class __iom_t9 {
318 tm* __tm_;
319 const _CharT* __fmt_;
320
321public:
322 _LIBCPP_HIDE_FROM_ABI __iom_t9(tm* __tm, const _CharT* __fmt) : __tm_(__tm), __fmt_(__fmt) {}
323
324 template <class _Cp, class _Traits>
325 friend basic_istream<_Cp, _Traits>& operator>>(basic_istream<_Cp, _Traits>& __is, const __iom_t9<_Cp>& __x);
326};
327
328template <class _CharT, class _Traits>
329_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
330operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x) {
331#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
332 try {
333#endif // _LIBCPP_HAS_NO_EXCEPTIONS
334 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
335 if (__s) {
336 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
337 typedef time_get<_CharT, _Ip> _Fp;
338 ios_base::iostate __err = ios_base::goodbit;
339 const _Fp& __tf = std::use_facet<_Fp>(__is.getloc());
340 __tf.get(_Ip(__is), _Ip(), __is, __err, __x.__tm_, __x.__fmt_, __x.__fmt_ + _Traits::length(__x.__fmt_));
341 __is.setstate(__err);
342 }
343#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
344 } catch (...) {
345 __is.__set_badbit_and_consider_rethrow();
346 }
347#endif // _LIBCPP_HAS_NO_EXCEPTIONS
348 return __is;
349}
350
351template <class _CharT>
352inline _LIBCPP_HIDE_FROM_ABI __iom_t9<_CharT> get_time(tm* __tm, const _CharT* __fmt) {
353 return __iom_t9<_CharT>(__tm, __fmt);
354}
355
356// put_time
357
358template <class _CharT>
359class __iom_t10;
360
361template <class _CharT, class _Traits>
362_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
363operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x);
364
365template <class _CharT>
366class __iom_t10 {
367 const tm* __tm_;
368 const _CharT* __fmt_;
369
370public:
371 _LIBCPP_HIDE_FROM_ABI __iom_t10(const tm* __tm, const _CharT* __fmt) : __tm_(__tm), __fmt_(__fmt) {}
372
373 template <class _Cp, class _Traits>
374 friend basic_ostream<_Cp, _Traits>& operator<<(basic_ostream<_Cp, _Traits>& __os, const __iom_t10<_Cp>& __x);
375};
376
377template <class _CharT, class _Traits>
378_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
379operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x) {
380#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
381 try {
382#endif // _LIBCPP_HAS_NO_EXCEPTIONS
383 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
384 if (__s) {
385 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
386 typedef time_put<_CharT, _Op> _Fp;
387 const _Fp& __tf = std::use_facet<_Fp>(__os.getloc());
388 if (__tf.put(_Op(__os), __os, __os.fill(), __x.__tm_, __x.__fmt_, __x.__fmt_ + _Traits::length(__x.__fmt_))
389 .failed())
390 __os.setstate(ios_base::badbit);
391 }
392#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
393 } catch (...) {
394 __os.__set_badbit_and_consider_rethrow();
395 }
396#endif // _LIBCPP_HAS_NO_EXCEPTIONS
397 return __os;
398}
399
400template <class _CharT>
401inline _LIBCPP_HIDE_FROM_ABI __iom_t10<_CharT> put_time(const tm* __tm, const _CharT* __fmt) {
402 return __iom_t10<_CharT>(__tm, __fmt);
403}
404
405template <class _CharT, class _Traits>
406_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& __quoted_output(
407 basic_ostream<_CharT, _Traits>& __os,
408 const _CharT* __first,
409 const _CharT* __last,
410 _CharT __delim,
411 _CharT __escape) {
412 basic_string<_CharT, _Traits> __str;
413 __str.push_back(__delim);
414 for (; __first != __last; ++__first) {
415 if (_Traits::eq(*__first, __escape) || _Traits::eq(*__first, __delim))
416 __str.push_back(__escape);
417 __str.push_back(*__first);
418 }
419 __str.push_back(__delim);
420 return std::__put_character_sequence(__os, __str.data(), __str.size());
421}
422
423template <class _CharT, class _Traits, class _String>
424_LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
425__quoted_input(basic_istream<_CharT, _Traits>& __is, _String& __string, _CharT __delim, _CharT __escape) {
426 __string.clear();
427 _CharT __c;
428 __is >> __c;
429 if (__is.fail())
430 return __is;
431
432 if (!_Traits::eq(__c, __delim)) {
433 // no delimiter, read the whole string
434 __is.unget();
435 __is >> __string;
436 return __is;
437 }
438
439 __save_flags<_CharT, _Traits> __sf(__is);
440 std::noskipws(str&: __is);
441 while (true) {
442 __is >> __c;
443 if (__is.fail())
444 break;
445 if (_Traits::eq(__c, __escape)) {
446 __is >> __c;
447 if (__is.fail())
448 break;
449 } else if (_Traits::eq(__c, __delim))
450 break;
451 __string.push_back(__c);
452 }
453 return __is;
454}
455
456template <class _CharT, class _Traits>
457struct _LIBCPP_HIDDEN __quoted_output_proxy {
458 const _CharT* __first_;
459 const _CharT* __last_;
460 _CharT __delim_;
461 _CharT __escape_;
462
463 _LIBCPP_HIDE_FROM_ABI explicit __quoted_output_proxy(const _CharT* __f, const _CharT* __l, _CharT __d, _CharT __e)
464 : __first_(__f), __last_(__l), __delim_(__d), __escape_(__e) {}
465
466 template <class _T2, __enable_if_t<_IsSame<_Traits, void>::value || _IsSame<_Traits, _T2>::value, int> = 0>
467 friend _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _T2>&
468 operator<<(basic_ostream<_CharT, _T2>& __os, const __quoted_output_proxy& __p) {
469 return std::__quoted_output(__os, __p.__first_, __p.__last_, __p.__delim_, __p.__escape_);
470 }
471};
472
473template <class _CharT, class _Traits, class _Allocator>
474struct _LIBCPP_HIDDEN __quoted_proxy {
475 basic_string<_CharT, _Traits, _Allocator>& __string_;
476 _CharT __delim_;
477 _CharT __escape_;
478
479 _LIBCPP_HIDE_FROM_ABI explicit __quoted_proxy(basic_string<_CharT, _Traits, _Allocator>& __s, _CharT __d, _CharT __e)
480 : __string_(__s), __delim_(__d), __escape_(__e) {}
481
482 friend _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
483 operator<<(basic_ostream<_CharT, _Traits>& __os, const __quoted_proxy& __p) {
484 return std::__quoted_output(
485 __os, __p.__string_.data(), __p.__string_.data() + __p.__string_.size(), __p.__delim_, __p.__escape_);
486 }
487
488 friend _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
489 operator>>(basic_istream<_CharT, _Traits>& __is, const __quoted_proxy& __p) {
490 return std::__quoted_input(__is, __p.__string_, __p.__delim_, __p.__escape_);
491 }
492};
493
494template <class _CharT, class _Traits, class _Allocator>
495_LIBCPP_HIDE_FROM_ABI __quoted_output_proxy<_CharT, _Traits>
496__quoted(const basic_string<_CharT, _Traits, _Allocator>& __s,
497 _CharT __delim = _CharT('"'),
498 _CharT __escape = _CharT('\\')) {
499 return __quoted_output_proxy<_CharT, _Traits>(__s.data(), __s.data() + __s.size(), __delim, __escape);
500}
501
502template <class _CharT, class _Traits, class _Allocator>
503_LIBCPP_HIDE_FROM_ABI __quoted_proxy<_CharT, _Traits, _Allocator>
504__quoted(basic_string<_CharT, _Traits, _Allocator>& __s, _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\')) {
505 return __quoted_proxy<_CharT, _Traits, _Allocator>(__s, __delim, __escape);
506}
507
508#if _LIBCPP_STD_VER >= 14
509
510template <class _CharT>
511_LIBCPP_HIDE_FROM_ABI auto quoted(const _CharT* __s, _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\')) {
512 const _CharT* __end = __s;
513 while (*__end)
514 ++__end;
515 return __quoted_output_proxy<_CharT, void>(__s, __end, __delim, __escape);
516}
517
518template <class _CharT, class _Traits, class _Allocator>
519_LIBCPP_HIDE_FROM_ABI auto
520quoted(const basic_string<_CharT, _Traits, _Allocator>& __s,
521 _CharT __delim = _CharT('"'),
522 _CharT __escape = _CharT('\\')) {
523 return __quoted_output_proxy<_CharT, _Traits>(__s.data(), __s.data() + __s.size(), __delim, __escape);
524}
525
526template <class _CharT, class _Traits, class _Allocator>
527_LIBCPP_HIDE_FROM_ABI auto
528quoted(basic_string<_CharT, _Traits, _Allocator>& __s, _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\')) {
529 return __quoted_proxy<_CharT, _Traits, _Allocator>(__s, __delim, __escape);
530}
531
532template <class _CharT, class _Traits>
533_LIBCPP_HIDE_FROM_ABI auto
534quoted(basic_string_view<_CharT, _Traits> __sv, _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\')) {
535 return __quoted_output_proxy<_CharT, _Traits>(__sv.data(), __sv.data() + __sv.size(), __delim, __escape);
536}
537
538#endif // _LIBCPP_STD_VER >= 14
539
540_LIBCPP_END_NAMESPACE_STD
541
542#endif // _LIBCPP_IOMANIP
543