2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_IOMANIP
11 #define _LIBCPP_IOMANIP
18 // types T1, T2, ... are unspecified implementation types
19 T1 resetiosflags(ios_base::fmtflags mask);
20 T2 setiosflags (ios_base::fmtflags mask);
22 template<charT> T4 setfill(charT c);
23 T5 setprecision(int n);
25 template <class moneyT> T7 get_money(moneyT& mon, bool intl = false);
26 template <class charT, class moneyT> T8 put_money(const moneyT& mon, bool intl = false);
27 template <class charT> T9 get_time(struct tm* tmb, const charT* fmt);
28 template <class charT> T10 put_time(const struct tm* tmb, const charT* fmt);
30 template <class charT>
31 T11 quoted(const charT* s, charT delim=charT('"'), charT escape=charT('\\')); // C++14
33 template <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
37 template <class charT, class traits, class Allocator>
38 T13 quoted(basic_string<charT, traits, Allocator>& s,
39 charT delim=charT('"'), charT escape=charT('\\')); // C++14
45 #include <__assert> // all public C++ headers provide the assertion handler
50 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
51 # pragma GCC system_header
54 _LIBCPP_BEGIN_NAMESPACE_STD
60 ios_base::fmtflags __mask_;
62 _LIBCPP_INLINE_VISIBILITY
63 explicit __iom_t1(ios_base::fmtflags __m) : __mask_(__m) {}
65 template <class _CharT, class _Traits>
67 _LIBCPP_INLINE_VISIBILITY
68 basic_istream<_CharT, _Traits>&
69 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t1& __x)
71 __is.unsetf(__x.__mask_);
75 template <class _CharT, class _Traits>
77 _LIBCPP_INLINE_VISIBILITY
78 basic_ostream<_CharT, _Traits>&
79 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t1& __x)
81 __os.unsetf(__x.__mask_);
86 inline _LIBCPP_INLINE_VISIBILITY
88 resetiosflags(ios_base::fmtflags __mask)
90 return __iom_t1(__mask);
97 ios_base::fmtflags __mask_;
99 _LIBCPP_INLINE_VISIBILITY
100 explicit __iom_t2(ios_base::fmtflags __m) : __mask_(__m) {}
102 template <class _CharT, class _Traits>
104 _LIBCPP_INLINE_VISIBILITY
105 basic_istream<_CharT, _Traits>&
106 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t2& __x)
108 __is.setf(__x.__mask_);
112 template <class _CharT, class _Traits>
114 _LIBCPP_INLINE_VISIBILITY
115 basic_ostream<_CharT, _Traits>&
116 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t2& __x)
118 __os.setf(__x.__mask_);
123 inline _LIBCPP_INLINE_VISIBILITY
125 setiosflags(ios_base::fmtflags __mask)
127 return __iom_t2(__mask);
136 _LIBCPP_INLINE_VISIBILITY
137 explicit __iom_t3(int __b) : __base_(__b) {}
139 template <class _CharT, class _Traits>
141 _LIBCPP_INLINE_VISIBILITY
142 basic_istream<_CharT, _Traits>&
143 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t3& __x)
145 __is.setf(__x.__base_ == 8 ? ios_base::oct :
146 __x.__base_ == 10 ? ios_base::dec :
147 __x.__base_ == 16 ? ios_base::hex :
148 ios_base::fmtflags(0), ios_base::basefield);
152 template <class _CharT, class _Traits>
154 _LIBCPP_INLINE_VISIBILITY
155 basic_ostream<_CharT, _Traits>&
156 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t3& __x)
158 __os.setf(__x.__base_ == 8 ? ios_base::oct :
159 __x.__base_ == 10 ? ios_base::dec :
160 __x.__base_ == 16 ? ios_base::hex :
161 ios_base::fmtflags(0), ios_base::basefield);
166 inline _LIBCPP_INLINE_VISIBILITY
170 return __iom_t3(__base);
175 template<class _CharT>
180 _LIBCPP_INLINE_VISIBILITY
181 explicit __iom_t4(_CharT __c) : __fill_(__c) {}
183 template <class _Traits>
185 _LIBCPP_INLINE_VISIBILITY
186 basic_ostream<_CharT, _Traits>&
187 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t4& __x)
189 __os.fill(__x.__fill_);
194 template<class _CharT>
195 inline _LIBCPP_INLINE_VISIBILITY
199 return __iom_t4<_CharT>(__c);
208 _LIBCPP_INLINE_VISIBILITY
209 explicit __iom_t5(int __n) : __n_(__n) {}
211 template <class _CharT, class _Traits>
213 _LIBCPP_INLINE_VISIBILITY
214 basic_istream<_CharT, _Traits>&
215 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t5& __x)
217 __is.precision(__x.__n_);
221 template <class _CharT, class _Traits>
223 _LIBCPP_INLINE_VISIBILITY
224 basic_ostream<_CharT, _Traits>&
225 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t5& __x)
227 __os.precision(__x.__n_);
232 inline _LIBCPP_INLINE_VISIBILITY
234 setprecision(int __n)
236 return __iom_t5(__n);
245 _LIBCPP_INLINE_VISIBILITY
246 explicit __iom_t6(int __n) : __n_(__n) {}
248 template <class _CharT, class _Traits>
250 _LIBCPP_INLINE_VISIBILITY
251 basic_istream<_CharT, _Traits>&
252 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t6& __x)
254 __is.width(__x.__n_);
258 template <class _CharT, class _Traits>
260 _LIBCPP_INLINE_VISIBILITY
261 basic_ostream<_CharT, _Traits>&
262 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t6& __x)
264 __os.width(__x.__n_);
269 inline _LIBCPP_INLINE_VISIBILITY
273 return __iom_t6(__n);
278 template <class _MoneyT> class __iom_t7;
280 template <class _CharT, class _Traits, class _MoneyT>
281 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
282 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x);
284 template <class _MoneyT>
290 _LIBCPP_INLINE_VISIBILITY
291 __iom_t7(_MoneyT& __mon, bool __intl)
292 : __mon_(__mon), __intl_(__intl) {}
294 template <class _CharT, class _Traits, class _Mp>
296 basic_istream<_CharT, _Traits>&
297 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_Mp>& __x);
300 template <class _CharT, class _Traits, class _MoneyT>
301 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
302 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x)
304 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
307 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
308 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
311 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
312 typedef money_get<_CharT, _Ip> _Fp;
313 ios_base::iostate __err = ios_base::goodbit;
314 const _Fp& __mf = std::use_facet<_Fp>(__is.getloc());
315 __mf.get(_Ip(__is), _Ip(), __x.__intl_, __is, __err, __x.__mon_);
316 __is.setstate(__err);
318 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
322 __is.__set_badbit_and_consider_rethrow();
324 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
328 template <class _MoneyT>
329 inline _LIBCPP_INLINE_VISIBILITY
331 get_money(_MoneyT& __mon, bool __intl = false)
333 return __iom_t7<_MoneyT>(__mon, __intl);
338 template <class _MoneyT> class __iom_t8;
340 template <class _CharT, class _Traits, class _MoneyT>
341 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
342 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x);
344 template <class _MoneyT>
347 const _MoneyT& __mon_;
350 _LIBCPP_INLINE_VISIBILITY
351 __iom_t8(const _MoneyT& __mon, bool __intl)
352 : __mon_(__mon), __intl_(__intl) {}
354 template <class _CharT, class _Traits, class _Mp>
356 basic_ostream<_CharT, _Traits>&
357 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_Mp>& __x);
360 template <class _CharT, class _Traits, class _MoneyT>
361 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
362 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x)
364 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
367 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
368 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
371 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
372 typedef money_put<_CharT, _Op> _Fp;
373 const _Fp& __mf = std::use_facet<_Fp>(__os.getloc());
374 if (__mf.put(_Op(__os), __x.__intl_, __os, __os.fill(), __x.__mon_).failed())
375 __os.setstate(ios_base::badbit);
377 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
381 __os.__set_badbit_and_consider_rethrow();
383 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
387 template <class _MoneyT>
388 inline _LIBCPP_INLINE_VISIBILITY
390 put_money(const _MoneyT& __mon, bool __intl = false)
392 return __iom_t8<_MoneyT>(__mon, __intl);
397 template <class _CharT> class __iom_t9;
399 template <class _CharT, class _Traits>
400 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
401 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x);
403 template <class _CharT>
407 const _CharT* __fmt_;
409 _LIBCPP_INLINE_VISIBILITY
410 __iom_t9(tm* __tm, const _CharT* __fmt)
411 : __tm_(__tm), __fmt_(__fmt) {}
413 template <class _Cp, class _Traits>
415 basic_istream<_Cp, _Traits>&
416 operator>>(basic_istream<_Cp, _Traits>& __is, const __iom_t9<_Cp>& __x);
419 template <class _CharT, class _Traits>
420 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
421 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x)
423 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
426 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
427 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
430 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
431 typedef time_get<_CharT, _Ip> _Fp;
432 ios_base::iostate __err = ios_base::goodbit;
433 const _Fp& __tf = std::use_facet<_Fp>(__is.getloc());
434 __tf.get(_Ip(__is), _Ip(), __is, __err, __x.__tm_,
435 __x.__fmt_, __x.__fmt_ + _Traits::length(__x.__fmt_));
436 __is.setstate(__err);
438 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
442 __is.__set_badbit_and_consider_rethrow();
444 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
448 template <class _CharT>
449 inline _LIBCPP_INLINE_VISIBILITY
451 get_time(tm* __tm, const _CharT* __fmt)
453 return __iom_t9<_CharT>(__tm, __fmt);
458 template <class _CharT> class __iom_t10;
460 template <class _CharT, class _Traits>
461 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
462 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x);
464 template <class _CharT>
468 const _CharT* __fmt_;
470 _LIBCPP_INLINE_VISIBILITY
471 __iom_t10(const tm* __tm, const _CharT* __fmt)
472 : __tm_(__tm), __fmt_(__fmt) {}
474 template <class _Cp, class _Traits>
476 basic_ostream<_Cp, _Traits>&
477 operator<<(basic_ostream<_Cp, _Traits>& __os, const __iom_t10<_Cp>& __x);
480 template <class _CharT, class _Traits>
481 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
482 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x)
484 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
487 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
488 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
491 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
492 typedef time_put<_CharT, _Op> _Fp;
493 const _Fp& __tf = std::use_facet<_Fp>(__os.getloc());
494 if (__tf.put(_Op(__os), __os, __os.fill(), __x.__tm_,
495 __x.__fmt_, __x.__fmt_ + _Traits::length(__x.__fmt_)).failed())
496 __os.setstate(ios_base::badbit);
498 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
502 __os.__set_badbit_and_consider_rethrow();
504 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
508 template <class _CharT>
509 inline _LIBCPP_INLINE_VISIBILITY
511 put_time(const tm* __tm, const _CharT* __fmt)
513 return __iom_t10<_CharT>(__tm, __fmt);
516 template <class _CharT, class _Traits>
517 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
518 __quoted_output(basic_ostream<_CharT, _Traits>& __os,
519 const _CharT *__first, const _CharT *__last, _CharT __delim, _CharT __escape)
521 basic_string<_CharT, _Traits> __str;
522 __str.push_back(__delim);
523 for (; __first != __last; ++__first) {
524 if (_Traits::eq(*__first, __escape) || _Traits::eq(*__first, __delim))
525 __str.push_back(__escape);
526 __str.push_back(*__first);
528 __str.push_back(__delim);
529 return _VSTD::__put_character_sequence(__os, __str.data(), __str.size());
532 template <class _CharT, class _Traits, class _String>
533 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
534 __quoted_input(basic_istream<_CharT, _Traits>& __is, _String& __string, _CharT __delim, _CharT __escape)
542 if (!_Traits::eq(__c, __delim)) {
543 // no delimiter, read the whole string
549 __save_flags<_CharT, _Traits> __sf(__is);
555 if (_Traits::eq(__c, __escape)) {
559 } else if (_Traits::eq(__c, __delim))
561 __string.push_back(__c);
566 template <class _CharT, class _Traits>
567 struct _LIBCPP_HIDDEN __quoted_output_proxy
569 const _CharT *__first_;
570 const _CharT *__last_;
574 _LIBCPP_HIDE_FROM_ABI
575 explicit __quoted_output_proxy(const _CharT *__f, const _CharT *__l, _CharT __d, _CharT __e)
576 : __first_(__f), __last_(__l), __delim_(__d), __escape_(__e) {}
578 template<class _T2, __enable_if_t<_IsSame<_Traits, void>::value || _IsSame<_Traits, _T2>::value>* = nullptr>
579 friend _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _T2>&
580 operator<<(basic_ostream<_CharT, _T2>& __os, const __quoted_output_proxy& __p) {
581 return std::__quoted_output(__os, __p.__first_, __p.__last_, __p.__delim_, __p.__escape_);
585 template <class _CharT, class _Traits, class _Allocator>
586 struct _LIBCPP_HIDDEN __quoted_proxy
588 basic_string<_CharT, _Traits, _Allocator>& __string_;
592 _LIBCPP_HIDE_FROM_ABI
593 explicit __quoted_proxy(basic_string<_CharT, _Traits, _Allocator>& __s, _CharT __d, _CharT __e)
594 : __string_(__s), __delim_(__d), __escape_(__e) {}
596 friend _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
597 operator<<(basic_ostream<_CharT, _Traits>& __os, const __quoted_proxy& __p) {
598 return std::__quoted_output(__os, __p.__string_.data(), __p.__string_.data() + __p.__string_.size(), __p.__delim_, __p.__escape_);
601 friend _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
602 operator>>(basic_istream<_CharT, _Traits>& __is, const __quoted_proxy& __p) {
603 return std::__quoted_input(__is, __p.__string_, __p.__delim_, __p.__escape_);
607 template <class _CharT, class _Traits, class _Allocator>
608 _LIBCPP_HIDE_FROM_ABI
609 __quoted_output_proxy<_CharT, _Traits>
610 __quoted(const basic_string<_CharT, _Traits, _Allocator>& __s, _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
612 return __quoted_output_proxy<_CharT, _Traits>(__s.data(), __s.data() + __s.size(), __delim, __escape);
615 template <class _CharT, class _Traits, class _Allocator>
616 _LIBCPP_HIDE_FROM_ABI
617 __quoted_proxy<_CharT, _Traits, _Allocator>
618 __quoted(basic_string<_CharT, _Traits, _Allocator>& __s, _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
620 return __quoted_proxy<_CharT, _Traits, _Allocator>(__s, __delim, __escape);
623 #if _LIBCPP_STD_VER >= 14
625 template <class _CharT>
626 _LIBCPP_HIDE_FROM_ABI
627 auto quoted(const _CharT *__s, _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
629 const _CharT *__end = __s;
630 while (*__end) ++__end;
631 return __quoted_output_proxy<_CharT, void>(__s, __end, __delim, __escape);
634 template <class _CharT, class _Traits, class _Allocator>
635 _LIBCPP_HIDE_FROM_ABI
636 auto quoted(const basic_string<_CharT, _Traits, _Allocator>& __s, _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
638 return __quoted_output_proxy<_CharT, _Traits>(__s.data(), __s.data() + __s.size(), __delim, __escape);
641 template <class _CharT, class _Traits, class _Allocator>
642 _LIBCPP_HIDE_FROM_ABI
643 auto quoted(basic_string<_CharT, _Traits, _Allocator>& __s, _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
645 return __quoted_proxy<_CharT, _Traits, _Allocator>(__s, __delim, __escape);
648 template <class _CharT, class _Traits>
649 _LIBCPP_HIDE_FROM_ABI
650 auto quoted(basic_string_view<_CharT, _Traits> __sv, _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
652 return __quoted_output_proxy<_CharT, _Traits>(__sv.data(), __sv.data() + __sv.size(), __delim, __escape);
655 #endif // _LIBCPP_STD_VER >= 14
657 _LIBCPP_END_NAMESPACE_STD
659 #endif // _LIBCPP_IOMANIP