2 //===--------------------------- iomanip ----------------------------------===//
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
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 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 basic_istream<_CharT, _Traits>&
302 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x)
304 #ifndef _LIBCPP_NO_EXCEPTIONS
307 #endif // _LIBCPP_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 = use_facet<_Fp>(__is.getloc());
315 __mf.get(_Ip(__is), _Ip(), __x.__intl_, __is, __err, __x.__mon_);
316 __is.setstate(__err);
318 #ifndef _LIBCPP_NO_EXCEPTIONS
322 __is.__set_badbit_and_consider_rethrow();
324 #endif // _LIBCPP_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 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 basic_ostream<_CharT, _Traits>&
362 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x)
364 #ifndef _LIBCPP_NO_EXCEPTIONS
367 #endif // _LIBCPP_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 = 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_NO_EXCEPTIONS
381 __os.__set_badbit_and_consider_rethrow();
383 #endif // _LIBCPP_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 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 basic_istream<_CharT, _Traits>&
421 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x)
423 #ifndef _LIBCPP_NO_EXCEPTIONS
426 #endif // _LIBCPP_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 = 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_NO_EXCEPTIONS
442 __is.__set_badbit_and_consider_rethrow();
444 #endif // _LIBCPP_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 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 basic_ostream<_CharT, _Traits>&
482 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x)
484 #ifndef _LIBCPP_NO_EXCEPTIONS
487 #endif // _LIBCPP_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 = 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_NO_EXCEPTIONS
502 __os.__set_badbit_and_consider_rethrow();
504 #endif // _LIBCPP_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, class _ForwardIterator>
517 std::basic_ostream<_CharT, _Traits> &
518 __quoted_output ( basic_ostream<_CharT, _Traits> &__os,
519 _ForwardIterator __first, _ForwardIterator __last, _CharT __delim, _CharT __escape )
521 _VSTD::basic_string<_CharT, _Traits> __str;
522 __str.push_back(__delim);
523 for ( ; __first != __last; ++ __first )
525 if (_Traits::eq (*__first, __escape) || _Traits::eq (*__first, __delim))
526 __str.push_back(__escape);
527 __str.push_back(*__first);
529 __str.push_back(__delim);
530 return __put_character_sequence(__os, __str.data(), __str.size());
533 template <class _CharT, class _Traits, class _String>
534 basic_istream<_CharT, _Traits> &
535 __quoted_input ( basic_istream<_CharT, _Traits> &__is, _String & __string, _CharT __delim, _CharT __escape )
543 if (!_Traits::eq (__c, __delim)) // no delimiter, read the whole string
550 __save_flags<_CharT, _Traits> sf(__is);
557 if (_Traits::eq (__c, __escape))
563 else if (_Traits::eq (__c, __delim))
565 __string.push_back ( __c );
571 template <class _CharT, class _Traits, class _Iter>
572 basic_ostream<_CharT, _Traits>& operator<<(
573 basic_ostream<_CharT, _Traits>& __os,
574 const __quoted_output_proxy<_CharT, _Iter, _Traits> & __proxy)
576 return __quoted_output (__os, __proxy.__first, __proxy.__last, __proxy.__delim, __proxy.__escape);
579 template <class _CharT, class _Traits, class _Allocator>
580 struct __quoted_proxy
582 basic_string<_CharT, _Traits, _Allocator> &__string;
586 __quoted_proxy(basic_string<_CharT, _Traits, _Allocator> &__s, _CharT __d, _CharT __e)
587 : __string(__s), __delim(__d), __escape(__e) {}
590 template <class _CharT, class _Traits, class _Allocator>
591 _LIBCPP_INLINE_VISIBILITY
592 basic_ostream<_CharT, _Traits>& operator<<(
593 basic_ostream<_CharT, _Traits>& __os,
594 const __quoted_proxy<_CharT, _Traits, _Allocator> & __proxy)
596 return __quoted_output (__os, __proxy.__string.cbegin (), __proxy.__string.cend (), __proxy.__delim, __proxy.__escape);
599 // extractor for non-const basic_string& proxies
600 template <class _CharT, class _Traits, class _Allocator>
601 _LIBCPP_INLINE_VISIBILITY
602 basic_istream<_CharT, _Traits>& operator>>(
603 basic_istream<_CharT, _Traits>& __is,
604 const __quoted_proxy<_CharT, _Traits, _Allocator> & __proxy)
606 return __quoted_input ( __is, __proxy.__string, __proxy.__delim, __proxy.__escape );
610 template <class _CharT>
611 _LIBCPP_INLINE_VISIBILITY
612 __quoted_output_proxy<_CharT, const _CharT *>
613 quoted ( const _CharT *__s, _CharT __delim = _CharT('"'), _CharT __escape =_CharT('\\'))
615 const _CharT *__end = __s;
616 while ( *__end ) ++__end;
617 return __quoted_output_proxy<_CharT, const _CharT *> ( __s, __end, __delim, __escape );
621 template <class _CharT, class _Traits, class _Allocator>
622 _LIBCPP_INLINE_VISIBILITY
623 __quoted_output_proxy<_CharT, typename basic_string <_CharT, _Traits, _Allocator>::const_iterator>
624 __quoted ( const basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
626 return __quoted_output_proxy<_CharT,
627 typename basic_string <_CharT, _Traits, _Allocator>::const_iterator>
628 ( __s.cbegin(), __s.cend (), __delim, __escape );
631 template <class _CharT, class _Traits, class _Allocator>
632 _LIBCPP_INLINE_VISIBILITY
633 __quoted_proxy<_CharT, _Traits, _Allocator>
634 __quoted ( basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
636 return __quoted_proxy<_CharT, _Traits, _Allocator>( __s, __delim, __escape );
640 #if _LIBCPP_STD_VER > 11
642 template <class _CharT, class _Traits, class _Allocator>
643 _LIBCPP_INLINE_VISIBILITY
644 __quoted_output_proxy<_CharT, typename basic_string <_CharT, _Traits, _Allocator>::const_iterator>
645 quoted ( const basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
647 return __quoted(__s, __delim, __escape);
650 template <class _CharT, class _Traits, class _Allocator>
651 _LIBCPP_INLINE_VISIBILITY
652 __quoted_proxy<_CharT, _Traits, _Allocator>
653 quoted ( basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
655 return __quoted(__s, __delim, __escape);
658 template <class _CharT, class _Traits>
659 __quoted_output_proxy<_CharT, const _CharT *, _Traits>
660 quoted (basic_string_view <_CharT, _Traits> __sv,
661 _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
663 return __quoted_output_proxy<_CharT, const _CharT *, _Traits>
664 ( __sv.data(), __sv.data() + __sv.size(), __delim, __escape );
668 _LIBCPP_END_NAMESPACE_STD
670 #endif // _LIBCPP_IOMANIP