blob: 2057a5d1f009674de4588fc2569068d8cbdaeffc [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- iomanip ----------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_IOMANIP
12#define _LIBCPP_IOMANIP
13
14/*
15 iomanip synopsis
16
Marshall Clow096d5792013-11-14 20:01:38 +000017namespace std {
18
Howard Hinnantc51e1022010-05-11 19:42:16 +000019// types T1, T2, ... are unspecified implementation types
20T1 resetiosflags(ios_base::fmtflags mask);
21T2 setiosflags (ios_base::fmtflags mask);
22T3 setbase(int base);
23template<charT> T4 setfill(charT c);
24T5 setprecision(int n);
25T6 setw(int n);
26template <class moneyT> T7 get_money(moneyT& mon, bool intl = false);
27template <class charT, class moneyT> T8 put_money(const moneyT& mon, bool intl = false);
28template <class charT> T9 get_time(struct tm* tmb, const charT* fmt);
29template <class charT> T10 put_time(const struct tm* tmb, const charT* fmt);
30
Marshall Clowf38a2982013-09-05 04:48:45 +000031template <class charT>
32 T11 quoted(const charT* s, charT delim=charT('"'), charT escape=charT('\\')); // C++14
33
34template <class charT, class traits, class Allocator>
35 T12 quoted(const basic_string<charT, traits, Allocator>& s,
36 charT delim=charT('"'), charT escape=charT('\\')); // C++14
37
38template <class charT, class traits, class Allocator>
39 T13 quoted(basic_string<charT, traits, Allocator>& s,
40 charT delim=charT('"'), charT escape=charT('\\')); // C++14
41
Howard Hinnantc51e1022010-05-11 19:42:16 +000042} // std
43
44*/
45
46#include <__config>
47#include <istream>
48
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000049#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +000050#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000051#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000052
53_LIBCPP_BEGIN_NAMESPACE_STD
54
55// resetiosflags
56
57class __iom_t1
58{
59 ios_base::fmtflags __mask_;
60public:
Howard Hinnant64da2602010-09-22 15:29:08 +000061 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +000062 explicit __iom_t1(ios_base::fmtflags __m) : __mask_(__m) {}
63
64 template <class _CharT, class _Traits>
65 friend
Howard Hinnant64da2602010-09-22 15:29:08 +000066 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +000067 basic_istream<_CharT, _Traits>&
68 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t1& __x)
69 {
70 __is.unsetf(__x.__mask_);
71 return __is;
72 }
73
74 template <class _CharT, class _Traits>
75 friend
Howard Hinnant64da2602010-09-22 15:29:08 +000076 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +000077 basic_ostream<_CharT, _Traits>&
78 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t1& __x)
79 {
80 __os.unsetf(__x.__mask_);
81 return __os;
82 }
83};
84
85inline _LIBCPP_INLINE_VISIBILITY
86__iom_t1
87resetiosflags(ios_base::fmtflags __mask)
88{
89 return __iom_t1(__mask);
90}
91
92// setiosflags
93
94class __iom_t2
95{
96 ios_base::fmtflags __mask_;
97public:
Howard Hinnant64da2602010-09-22 15:29:08 +000098 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +000099 explicit __iom_t2(ios_base::fmtflags __m) : __mask_(__m) {}
100
101 template <class _CharT, class _Traits>
102 friend
Howard Hinnant64da2602010-09-22 15:29:08 +0000103 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000104 basic_istream<_CharT, _Traits>&
105 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t2& __x)
106 {
107 __is.setf(__x.__mask_);
108 return __is;
109 }
110
111 template <class _CharT, class _Traits>
112 friend
Howard Hinnant64da2602010-09-22 15:29:08 +0000113 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000114 basic_ostream<_CharT, _Traits>&
115 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t2& __x)
116 {
117 __os.setf(__x.__mask_);
118 return __os;
119 }
120};
121
122inline _LIBCPP_INLINE_VISIBILITY
123__iom_t2
124setiosflags(ios_base::fmtflags __mask)
125{
126 return __iom_t2(__mask);
127}
128
129// setbase
130
131class __iom_t3
132{
133 int __base_;
134public:
Howard Hinnant64da2602010-09-22 15:29:08 +0000135 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000136 explicit __iom_t3(int __b) : __base_(__b) {}
137
138 template <class _CharT, class _Traits>
139 friend
Howard Hinnant64da2602010-09-22 15:29:08 +0000140 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000141 basic_istream<_CharT, _Traits>&
142 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t3& __x)
143 {
144 __is.setf(__x.__base_ == 8 ? ios_base::oct :
145 __x.__base_ == 10 ? ios_base::dec :
146 __x.__base_ == 16 ? ios_base::hex :
147 ios_base::fmtflags(0), ios_base::basefield);
148 return __is;
149 }
150
151 template <class _CharT, class _Traits>
152 friend
Howard Hinnant64da2602010-09-22 15:29:08 +0000153 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154 basic_ostream<_CharT, _Traits>&
155 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t3& __x)
156 {
157 __os.setf(__x.__base_ == 8 ? ios_base::oct :
158 __x.__base_ == 10 ? ios_base::dec :
159 __x.__base_ == 16 ? ios_base::hex :
160 ios_base::fmtflags(0), ios_base::basefield);
161 return __os;
162 }
163};
164
165inline _LIBCPP_INLINE_VISIBILITY
166__iom_t3
167setbase(int __base)
168{
169 return __iom_t3(__base);
170}
171
172// setfill
173
174template<class _CharT>
175class __iom_t4
176{
177 _CharT __fill_;
178public:
Howard Hinnant64da2602010-09-22 15:29:08 +0000179 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180 explicit __iom_t4(_CharT __c) : __fill_(__c) {}
181
182 template <class _Traits>
183 friend
Howard Hinnant64da2602010-09-22 15:29:08 +0000184 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185 basic_ostream<_CharT, _Traits>&
186 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t4& __x)
187 {
188 __os.fill(__x.__fill_);
189 return __os;
190 }
191};
192
193template<class _CharT>
194inline _LIBCPP_INLINE_VISIBILITY
195__iom_t4<_CharT>
196setfill(_CharT __c)
197{
198 return __iom_t4<_CharT>(__c);
199}
200
201// setprecision
202
203class __iom_t5
204{
205 int __n_;
206public:
Howard Hinnant64da2602010-09-22 15:29:08 +0000207 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000208 explicit __iom_t5(int __n) : __n_(__n) {}
209
210 template <class _CharT, class _Traits>
211 friend
Howard Hinnant64da2602010-09-22 15:29:08 +0000212 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000213 basic_istream<_CharT, _Traits>&
214 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t5& __x)
215 {
216 __is.precision(__x.__n_);
217 return __is;
218 }
219
220 template <class _CharT, class _Traits>
221 friend
Howard Hinnant64da2602010-09-22 15:29:08 +0000222 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000223 basic_ostream<_CharT, _Traits>&
224 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t5& __x)
225 {
226 __os.precision(__x.__n_);
227 return __os;
228 }
229};
230
231inline _LIBCPP_INLINE_VISIBILITY
232__iom_t5
233setprecision(int __n)
234{
235 return __iom_t5(__n);
236}
237
238// setw
239
240class __iom_t6
241{
242 int __n_;
243public:
Howard Hinnant64da2602010-09-22 15:29:08 +0000244 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000245 explicit __iom_t6(int __n) : __n_(__n) {}
246
247 template <class _CharT, class _Traits>
248 friend
Howard Hinnant64da2602010-09-22 15:29:08 +0000249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000250 basic_istream<_CharT, _Traits>&
251 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t6& __x)
252 {
253 __is.width(__x.__n_);
254 return __is;
255 }
256
257 template <class _CharT, class _Traits>
258 friend
Howard Hinnant64da2602010-09-22 15:29:08 +0000259 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000260 basic_ostream<_CharT, _Traits>&
261 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t6& __x)
262 {
263 __os.width(__x.__n_);
264 return __os;
265 }
266};
267
268inline _LIBCPP_INLINE_VISIBILITY
269__iom_t6
270setw(int __n)
271{
272 return __iom_t6(__n);
273}
274
275// get_money
276
277template <class _MoneyT> class __iom_t7;
278
279template <class _CharT, class _Traits, class _MoneyT>
280basic_istream<_CharT, _Traits>&
281operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x);
282
283template <class _MoneyT>
284class __iom_t7
285{
286 _MoneyT& __mon_;
287 bool __intl_;
288public:
Howard Hinnant64da2602010-09-22 15:29:08 +0000289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000290 __iom_t7(_MoneyT& __mon, bool __intl)
291 : __mon_(__mon), __intl_(__intl) {}
292
Howard Hinnantc834c512011-11-29 18:15:50 +0000293 template <class _CharT, class _Traits, class _Mp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000294 friend
295 basic_istream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +0000296 operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_Mp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000297};
298
299template <class _CharT, class _Traits, class _MoneyT>
300basic_istream<_CharT, _Traits>&
301operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t7<_MoneyT>& __x)
302{
303#ifndef _LIBCPP_NO_EXCEPTIONS
304 try
305 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000306#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000307 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
308 if (__s)
309 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000310 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
311 typedef money_get<_CharT, _Ip> _Fp;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000312 ios_base::iostate __err = ios_base::goodbit;
Howard Hinnantc834c512011-11-29 18:15:50 +0000313 const _Fp& __mf = use_facet<_Fp>(__is.getloc());
314 __mf.get(_Ip(__is), _Ip(), __x.__intl_, __is, __err, __x.__mon_);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000315 __is.setstate(__err);
316 }
317#ifndef _LIBCPP_NO_EXCEPTIONS
318 }
319 catch (...)
320 {
321 __is.__set_badbit_and_consider_rethrow();
322 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000323#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000324 return __is;
325}
326
327template <class _MoneyT>
328inline _LIBCPP_INLINE_VISIBILITY
329__iom_t7<_MoneyT>
330get_money(_MoneyT& __mon, bool __intl = false)
331{
332 return __iom_t7<_MoneyT>(__mon, __intl);
333}
334
335// put_money
336
337template <class _MoneyT> class __iom_t8;
338
339template <class _CharT, class _Traits, class _MoneyT>
340basic_ostream<_CharT, _Traits>&
341operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x);
342
343template <class _MoneyT>
344class __iom_t8
345{
346 const _MoneyT& __mon_;
347 bool __intl_;
348public:
Howard Hinnant64da2602010-09-22 15:29:08 +0000349 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000350 __iom_t8(const _MoneyT& __mon, bool __intl)
351 : __mon_(__mon), __intl_(__intl) {}
352
Howard Hinnantc834c512011-11-29 18:15:50 +0000353 template <class _CharT, class _Traits, class _Mp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000354 friend
355 basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +0000356 operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_Mp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000357};
358
359template <class _CharT, class _Traits, class _MoneyT>
360basic_ostream<_CharT, _Traits>&
361operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t8<_MoneyT>& __x)
362{
363#ifndef _LIBCPP_NO_EXCEPTIONS
364 try
365 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000366#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000367 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
368 if (__s)
369 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000370 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
371 typedef money_put<_CharT, _Op> _Fp;
372 const _Fp& __mf = use_facet<_Fp>(__os.getloc());
373 if (__mf.put(_Op(__os), __x.__intl_, __os, __os.fill(), __x.__mon_).failed())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000374 __os.setstate(ios_base::badbit);
375 }
376#ifndef _LIBCPP_NO_EXCEPTIONS
377 }
378 catch (...)
379 {
380 __os.__set_badbit_and_consider_rethrow();
381 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000382#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383 return __os;
384}
385
386template <class _MoneyT>
387inline _LIBCPP_INLINE_VISIBILITY
388__iom_t8<_MoneyT>
389put_money(const _MoneyT& __mon, bool __intl = false)
390{
391 return __iom_t8<_MoneyT>(__mon, __intl);
392}
393
394// get_time
395
396template <class _CharT> class __iom_t9;
397
398template <class _CharT, class _Traits>
399basic_istream<_CharT, _Traits>&
400operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x);
401
402template <class _CharT>
403class __iom_t9
404{
405 tm* __tm_;
406 const _CharT* __fmt_;
407public:
Howard Hinnant64da2602010-09-22 15:29:08 +0000408 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409 __iom_t9(tm* __tm, const _CharT* __fmt)
410 : __tm_(__tm), __fmt_(__fmt) {}
411
Howard Hinnantc834c512011-11-29 18:15:50 +0000412 template <class _Cp, class _Traits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000413 friend
Howard Hinnantc834c512011-11-29 18:15:50 +0000414 basic_istream<_Cp, _Traits>&
415 operator>>(basic_istream<_Cp, _Traits>& __is, const __iom_t9<_Cp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416};
417
418template <class _CharT, class _Traits>
419basic_istream<_CharT, _Traits>&
420operator>>(basic_istream<_CharT, _Traits>& __is, const __iom_t9<_CharT>& __x)
421{
422#ifndef _LIBCPP_NO_EXCEPTIONS
423 try
424 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000425#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
427 if (__s)
428 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000429 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
430 typedef time_get<_CharT, _Ip> _Fp;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000431 ios_base::iostate __err = ios_base::goodbit;
Howard Hinnantc834c512011-11-29 18:15:50 +0000432 const _Fp& __tf = use_facet<_Fp>(__is.getloc());
433 __tf.get(_Ip(__is), _Ip(), __is, __err, __x.__tm_,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434 __x.__fmt_, __x.__fmt_ + _Traits::length(__x.__fmt_));
435 __is.setstate(__err);
436 }
437#ifndef _LIBCPP_NO_EXCEPTIONS
438 }
439 catch (...)
440 {
441 __is.__set_badbit_and_consider_rethrow();
442 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000443#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000444 return __is;
445}
446
447template <class _CharT>
448inline _LIBCPP_INLINE_VISIBILITY
449__iom_t9<_CharT>
450get_time(tm* __tm, const _CharT* __fmt)
451{
452 return __iom_t9<_CharT>(__tm, __fmt);
453}
454
455// put_time
456
457template <class _CharT> class __iom_t10;
458
459template <class _CharT, class _Traits>
460basic_ostream<_CharT, _Traits>&
461operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x);
462
463template <class _CharT>
464class __iom_t10
465{
466 const tm* __tm_;
467 const _CharT* __fmt_;
468public:
Howard Hinnant64da2602010-09-22 15:29:08 +0000469 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000470 __iom_t10(const tm* __tm, const _CharT* __fmt)
471 : __tm_(__tm), __fmt_(__fmt) {}
472
Howard Hinnantc834c512011-11-29 18:15:50 +0000473 template <class _Cp, class _Traits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000474 friend
Howard Hinnantc834c512011-11-29 18:15:50 +0000475 basic_ostream<_Cp, _Traits>&
476 operator<<(basic_ostream<_Cp, _Traits>& __os, const __iom_t10<_Cp>& __x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000477};
478
479template <class _CharT, class _Traits>
480basic_ostream<_CharT, _Traits>&
481operator<<(basic_ostream<_CharT, _Traits>& __os, const __iom_t10<_CharT>& __x)
482{
483#ifndef _LIBCPP_NO_EXCEPTIONS
484 try
485 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000486#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000487 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
488 if (__s)
489 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000490 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
491 typedef time_put<_CharT, _Op> _Fp;
492 const _Fp& __tf = use_facet<_Fp>(__os.getloc());
493 if (__tf.put(_Op(__os), __os, __os.fill(), __x.__tm_,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000494 __x.__fmt_, __x.__fmt_ + _Traits::length(__x.__fmt_)).failed())
495 __os.setstate(ios_base::badbit);
496 }
497#ifndef _LIBCPP_NO_EXCEPTIONS
498 }
499 catch (...)
500 {
501 __os.__set_badbit_and_consider_rethrow();
502 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000503#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000504 return __os;
505}
506
507template <class _CharT>
508inline _LIBCPP_INLINE_VISIBILITY
509__iom_t10<_CharT>
510put_time(const tm* __tm, const _CharT* __fmt)
511{
512 return __iom_t10<_CharT>(__tm, __fmt);
513}
514
Marshall Clowf38a2982013-09-05 04:48:45 +0000515template <class _CharT, class _Traits, class _ForwardIterator>
516std::basic_ostream<_CharT, _Traits> &
517__quoted_output ( basic_ostream<_CharT, _Traits> &__os,
518 _ForwardIterator __first, _ForwardIterator __last, _CharT __delim, _CharT __escape )
519{
Marshall Clow3d036762014-03-07 21:45:32 +0000520 _VSTD::basic_string<_CharT, _Traits> __str;
521 __str.push_back(__delim);
Marshall Clowf38a2982013-09-05 04:48:45 +0000522 for ( ; __first != __last; ++ __first )
523 {
524 if (_Traits::eq (*__first, __escape) || _Traits::eq (*__first, __delim))
Marshall Clow3d036762014-03-07 21:45:32 +0000525 __str.push_back(__escape);
526 __str.push_back(*__first);
Marshall Clowf38a2982013-09-05 04:48:45 +0000527 }
Marshall Clow3d036762014-03-07 21:45:32 +0000528 __str.push_back(__delim);
529 return __put_character_sequence(__os, __str.data(), __str.size());
Marshall Clowf38a2982013-09-05 04:48:45 +0000530}
531
532template <class _CharT, class _Traits, class _String>
533basic_istream<_CharT, _Traits> &
534__quoted_input ( basic_istream<_CharT, _Traits> &__is, _String & __string, _CharT __delim, _CharT __escape )
535{
536 __string.clear ();
537 _CharT __c;
538 __is >> __c;
539 if ( __is.fail ())
540 return __is;
541
542 if (!_Traits::eq (__c, __delim)) // no delimiter, read the whole string
543 {
544 __is.unget ();
545 __is >> __string;
546 return __is;
547 }
548
549 __save_flags<_CharT, _Traits> sf(__is);
550 noskipws (__is);
551 while (true)
552 {
553 __is >> __c;
554 if ( __is.fail ())
555 break;
556 if (_Traits::eq (__c, __escape))
557 {
558 __is >> __c;
559 if ( __is.fail ())
560 break;
561 }
562 else if (_Traits::eq (__c, __delim))
563 break;
564 __string.push_back ( __c );
565 }
566 return __is;
567}
568
569
570template <class _CharT, class _Iter, class _Traits=char_traits<_CharT>>
571struct __quoted_output_proxy
572{
573 _Iter __first;
574 _Iter __last;
575 _CharT __delim;
576 _CharT __escape;
577
578 __quoted_output_proxy(_Iter __f, _Iter __l, _CharT __d, _CharT __e)
579 : __first(__f), __last(__l), __delim(__d), __escape(__e) {}
580 // This would be a nice place for a string_ref
581};
582
583template <class _CharT, class _Traits, class _Iter>
584basic_ostream<_CharT, _Traits>& operator<<(
585 basic_ostream<_CharT, _Traits>& __os,
586 const __quoted_output_proxy<_CharT, _Iter, _Traits> & __proxy)
587{
588 return __quoted_output (__os, __proxy.__first, __proxy.__last, __proxy.__delim, __proxy.__escape);
589}
590
591template <class _CharT, class _Traits, class _Allocator>
592struct __quoted_proxy
593{
594 basic_string<_CharT, _Traits, _Allocator> &__string;
595 _CharT __delim;
596 _CharT __escape;
597
598 __quoted_proxy(basic_string<_CharT, _Traits, _Allocator> &__s, _CharT __d, _CharT __e)
599 : __string(__s), __delim(__d), __escape(__e) {}
600};
601
602template <class _CharT, class _Traits, class _Allocator>
603_LIBCPP_INLINE_VISIBILITY
604basic_ostream<_CharT, _Traits>& operator<<(
605 basic_ostream<_CharT, _Traits>& __os,
606 const __quoted_proxy<_CharT, _Traits, _Allocator> & __proxy)
607{
Marshall Clow096d5792013-11-14 20:01:38 +0000608 return __quoted_output (__os, __proxy.__string.cbegin (), __proxy.__string.cend (), __proxy.__delim, __proxy.__escape);
Marshall Clowf38a2982013-09-05 04:48:45 +0000609}
610
611// extractor for non-const basic_string& proxies
612template <class _CharT, class _Traits, class _Allocator>
613_LIBCPP_INLINE_VISIBILITY
614basic_istream<_CharT, _Traits>& operator>>(
615 basic_istream<_CharT, _Traits>& __is,
616 const __quoted_proxy<_CharT, _Traits, _Allocator> & __proxy)
617{
618 return __quoted_input ( __is, __proxy.__string, __proxy.__delim, __proxy.__escape );
619}
620
621
622template <class _CharT>
623_LIBCPP_INLINE_VISIBILITY
624__quoted_output_proxy<_CharT, const _CharT *>
625quoted ( const _CharT *__s, _CharT __delim = _CharT('"'), _CharT __escape =_CharT('\\'))
626{
627 const _CharT *__end = __s;
628 while ( *__end ) ++__end;
629 return __quoted_output_proxy<_CharT, const _CharT *> ( __s, __end, __delim, __escape );
630}
631
Eric Fiselier435db152016-06-17 19:46:40 +0000632
633template <class _CharT, class _Traits, class _Allocator>
634_LIBCPP_INLINE_VISIBILITY
635__quoted_output_proxy<_CharT, typename basic_string <_CharT, _Traits, _Allocator>::const_iterator>
636__quoted ( const basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
637{
638 return __quoted_output_proxy<_CharT,
639 typename basic_string <_CharT, _Traits, _Allocator>::const_iterator>
640 ( __s.cbegin(), __s.cend (), __delim, __escape );
641}
642
643template <class _CharT, class _Traits, class _Allocator>
644_LIBCPP_INLINE_VISIBILITY
645__quoted_proxy<_CharT, _Traits, _Allocator>
646__quoted ( basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
647{
648 return __quoted_proxy<_CharT, _Traits, _Allocator>( __s, __delim, __escape );
649}
650
651
652#if _LIBCPP_STD_VER > 11
653
Marshall Clowf38a2982013-09-05 04:48:45 +0000654template <class _CharT, class _Traits, class _Allocator>
655_LIBCPP_INLINE_VISIBILITY
656__quoted_output_proxy<_CharT, typename basic_string <_CharT, _Traits, _Allocator>::const_iterator>
657quoted ( const basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
658{
Eric Fiselier435db152016-06-17 19:46:40 +0000659 return __quoted(__s, __delim, __escape);
Marshall Clowf38a2982013-09-05 04:48:45 +0000660}
661
662template <class _CharT, class _Traits, class _Allocator>
Eric Fiselier435db152016-06-17 19:46:40 +0000663_LIBCPP_INLINE_VISIBILITY
Marshall Clowf38a2982013-09-05 04:48:45 +0000664__quoted_proxy<_CharT, _Traits, _Allocator>
665quoted ( basic_string <_CharT, _Traits, _Allocator> &__s, _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
666{
Eric Fiselier435db152016-06-17 19:46:40 +0000667 return __quoted(__s, __delim, __escape);
Marshall Clowf38a2982013-09-05 04:48:45 +0000668}
669#endif
670
Howard Hinnantc51e1022010-05-11 19:42:16 +0000671_LIBCPP_END_NAMESPACE_STD
672
673#endif // _LIBCPP_IOMANIP