PR libstdc++/87278 restore support for std::make_shared<volatile T>()
[official-gcc.git] / libstdc++-v3 / include / bits / locale_conv.h
blobe9b684b4f98850f0152861a8189b230ef77bea87
1 // wstring_convert implementation -*- C++ -*-
3 // Copyright (C) 2015-2018 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file bits/locale_conv.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{locale}
30 #ifndef _LOCALE_CONV_H
31 #define _LOCALE_CONV_H 1
33 #if __cplusplus < 201103L
34 # include <bits/c++0x_warning.h>
35 #else
37 #include <streambuf>
38 #include "stringfwd.h"
39 #include "allocator.h"
40 #include "codecvt.h"
41 #include "unique_ptr.h"
43 namespace std _GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
47 /**
48 * @addtogroup locales
49 * @{
52 template<typename _OutStr, typename _InChar, typename _Codecvt,
53 typename _State, typename _Fn>
54 bool
55 __do_str_codecvt(const _InChar* __first, const _InChar* __last,
56 _OutStr& __outstr, const _Codecvt& __cvt, _State& __state,
57 size_t& __count, _Fn __fn)
59 if (__first == __last)
61 __outstr.clear();
62 __count = 0;
63 return true;
66 size_t __outchars = 0;
67 auto __next = __first;
68 const auto __maxlen = __cvt.max_length() + 1;
70 codecvt_base::result __result;
73 __outstr.resize(__outstr.size() + (__last - __next) * __maxlen);
74 auto __outnext = &__outstr.front() + __outchars;
75 auto const __outlast = &__outstr.back() + 1;
76 __result = (__cvt.*__fn)(__state, __next, __last, __next,
77 __outnext, __outlast, __outnext);
78 __outchars = __outnext - &__outstr.front();
80 while (__result == codecvt_base::partial && __next != __last
81 && (__outstr.size() - __outchars) < __maxlen);
83 if (__result == codecvt_base::error)
85 __count = __next - __first;
86 return false;
89 if (__result == codecvt_base::noconv)
91 __outstr.assign(__first, __last);
92 __count = __last - __first;
94 else
96 __outstr.resize(__outchars);
97 __count = __next - __first;
100 return true;
103 // Convert narrow character string to wide.
104 template<typename _CharT, typename _Traits, typename _Alloc, typename _State>
105 inline bool
106 __str_codecvt_in(const char* __first, const char* __last,
107 basic_string<_CharT, _Traits, _Alloc>& __outstr,
108 const codecvt<_CharT, char, _State>& __cvt,
109 _State& __state, size_t& __count)
111 using _Codecvt = codecvt<_CharT, char, _State>;
112 using _ConvFn
113 = codecvt_base::result
114 (_Codecvt::*)(_State&, const char*, const char*, const char*&,
115 _CharT*, _CharT*, _CharT*&) const;
116 _ConvFn __fn = &codecvt<_CharT, char, _State>::in;
117 return __do_str_codecvt(__first, __last, __outstr, __cvt, __state,
118 __count, __fn);
121 template<typename _CharT, typename _Traits, typename _Alloc, typename _State>
122 inline bool
123 __str_codecvt_in(const char* __first, const char* __last,
124 basic_string<_CharT, _Traits, _Alloc>& __outstr,
125 const codecvt<_CharT, char, _State>& __cvt)
127 _State __state = {};
128 size_t __n;
129 return __str_codecvt_in(__first, __last, __outstr, __cvt, __state, __n);
132 // Convert wide character string to narrow.
133 template<typename _CharT, typename _Traits, typename _Alloc, typename _State>
134 inline bool
135 __str_codecvt_out(const _CharT* __first, const _CharT* __last,
136 basic_string<char, _Traits, _Alloc>& __outstr,
137 const codecvt<_CharT, char, _State>& __cvt,
138 _State& __state, size_t& __count)
140 using _Codecvt = codecvt<_CharT, char, _State>;
141 using _ConvFn
142 = codecvt_base::result
143 (_Codecvt::*)(_State&, const _CharT*, const _CharT*, const _CharT*&,
144 char*, char*, char*&) const;
145 _ConvFn __fn = &codecvt<_CharT, char, _State>::out;
146 return __do_str_codecvt(__first, __last, __outstr, __cvt, __state,
147 __count, __fn);
150 template<typename _CharT, typename _Traits, typename _Alloc, typename _State>
151 inline bool
152 __str_codecvt_out(const _CharT* __first, const _CharT* __last,
153 basic_string<char, _Traits, _Alloc>& __outstr,
154 const codecvt<_CharT, char, _State>& __cvt)
156 _State __state = {};
157 size_t __n;
158 return __str_codecvt_out(__first, __last, __outstr, __cvt, __state, __n);
161 #ifdef _GLIBCXX_USE_WCHAR_T
163 _GLIBCXX_BEGIN_NAMESPACE_CXX11
165 /// String conversions
166 template<typename _Codecvt, typename _Elem = wchar_t,
167 typename _Wide_alloc = allocator<_Elem>,
168 typename _Byte_alloc = allocator<char>>
169 class wstring_convert
171 public:
172 typedef basic_string<char, char_traits<char>, _Byte_alloc> byte_string;
173 typedef basic_string<_Elem, char_traits<_Elem>, _Wide_alloc> wide_string;
174 typedef typename _Codecvt::state_type state_type;
175 typedef typename wide_string::traits_type::int_type int_type;
177 /// Default constructor.
178 wstring_convert() : _M_cvt(new _Codecvt()) { }
180 /** Constructor.
182 * @param __pcvt The facet to use for conversions.
184 * Takes ownership of @p __pcvt and will delete it in the destructor.
186 explicit
187 wstring_convert(_Codecvt* __pcvt) : _M_cvt(__pcvt)
189 if (!_M_cvt)
190 __throw_logic_error("wstring_convert");
193 /** Construct with an initial converstion state.
195 * @param __pcvt The facet to use for conversions.
196 * @param __state Initial conversion state.
198 * Takes ownership of @p __pcvt and will delete it in the destructor.
199 * The object's conversion state will persist between conversions.
201 wstring_convert(_Codecvt* __pcvt, state_type __state)
202 : _M_cvt(__pcvt), _M_state(__state), _M_with_cvtstate(true)
204 if (!_M_cvt)
205 __throw_logic_error("wstring_convert");
208 /** Construct with error strings.
210 * @param __byte_err A string to return on failed conversions.
211 * @param __wide_err A wide string to return on failed conversions.
213 explicit
214 wstring_convert(const byte_string& __byte_err,
215 const wide_string& __wide_err = wide_string())
216 : _M_cvt(new _Codecvt),
217 _M_byte_err_string(__byte_err), _M_wide_err_string(__wide_err),
218 _M_with_strings(true)
220 if (!_M_cvt)
221 __throw_logic_error("wstring_convert");
224 ~wstring_convert() = default;
226 // _GLIBCXX_RESOLVE_LIB_DEFECTS
227 // 2176. Special members for wstring_convert and wbuffer_convert
228 wstring_convert(const wstring_convert&) = delete;
229 wstring_convert& operator=(const wstring_convert&) = delete;
231 /// @{ Convert from bytes.
232 wide_string
233 from_bytes(char __byte)
235 char __bytes[2] = { __byte };
236 return from_bytes(__bytes, __bytes+1);
239 wide_string
240 from_bytes(const char* __ptr)
241 { return from_bytes(__ptr, __ptr+char_traits<char>::length(__ptr)); }
243 wide_string
244 from_bytes(const byte_string& __str)
246 auto __ptr = __str.data();
247 return from_bytes(__ptr, __ptr + __str.size());
250 wide_string
251 from_bytes(const char* __first, const char* __last)
253 if (!_M_with_cvtstate)
254 _M_state = state_type();
255 wide_string __out{ _M_wide_err_string.get_allocator() };
256 if (__str_codecvt_in(__first, __last, __out, *_M_cvt, _M_state,
257 _M_count))
258 return __out;
259 if (_M_with_strings)
260 return _M_wide_err_string;
261 __throw_range_error("wstring_convert::from_bytes");
263 /// @}
265 /// @{ Convert to bytes.
266 byte_string
267 to_bytes(_Elem __wchar)
269 _Elem __wchars[2] = { __wchar };
270 return to_bytes(__wchars, __wchars+1);
273 byte_string
274 to_bytes(const _Elem* __ptr)
276 return to_bytes(__ptr, __ptr+wide_string::traits_type::length(__ptr));
279 byte_string
280 to_bytes(const wide_string& __wstr)
282 auto __ptr = __wstr.data();
283 return to_bytes(__ptr, __ptr + __wstr.size());
286 byte_string
287 to_bytes(const _Elem* __first, const _Elem* __last)
289 if (!_M_with_cvtstate)
290 _M_state = state_type();
291 byte_string __out{ _M_byte_err_string.get_allocator() };
292 if (__str_codecvt_out(__first, __last, __out, *_M_cvt, _M_state,
293 _M_count))
294 return __out;
295 if (_M_with_strings)
296 return _M_byte_err_string;
297 __throw_range_error("wstring_convert::to_bytes");
299 /// @}
301 // _GLIBCXX_RESOLVE_LIB_DEFECTS
302 // 2174. wstring_convert::converted() should be noexcept
303 /// The number of elements successfully converted in the last conversion.
304 size_t converted() const noexcept { return _M_count; }
306 /// The final conversion state of the last conversion.
307 state_type state() const { return _M_state; }
309 private:
310 unique_ptr<_Codecvt> _M_cvt;
311 byte_string _M_byte_err_string;
312 wide_string _M_wide_err_string;
313 state_type _M_state = state_type();
314 size_t _M_count = 0;
315 bool _M_with_cvtstate = false;
316 bool _M_with_strings = false;
319 _GLIBCXX_END_NAMESPACE_CXX11
321 /// Buffer conversions
322 template<typename _Codecvt, typename _Elem = wchar_t,
323 typename _Tr = char_traits<_Elem>>
324 class wbuffer_convert : public basic_streambuf<_Elem, _Tr>
326 typedef basic_streambuf<_Elem, _Tr> _Wide_streambuf;
328 public:
329 typedef typename _Codecvt::state_type state_type;
331 /// Default constructor.
332 wbuffer_convert() : wbuffer_convert(nullptr) { }
334 /** Constructor.
336 * @param __bytebuf The underlying byte stream buffer.
337 * @param __pcvt The facet to use for conversions.
338 * @param __state Initial conversion state.
340 * Takes ownership of @p __pcvt and will delete it in the destructor.
342 explicit
343 wbuffer_convert(streambuf* __bytebuf, _Codecvt* __pcvt = new _Codecvt,
344 state_type __state = state_type())
345 : _M_buf(__bytebuf), _M_cvt(__pcvt), _M_state(__state)
347 if (!_M_cvt)
348 __throw_logic_error("wbuffer_convert");
350 _M_always_noconv = _M_cvt->always_noconv();
352 if (_M_buf)
354 this->setp(_M_put_area, _M_put_area + _S_buffer_length);
355 this->setg(_M_get_area + _S_putback_length,
356 _M_get_area + _S_putback_length,
357 _M_get_area + _S_putback_length);
361 ~wbuffer_convert() = default;
363 // _GLIBCXX_RESOLVE_LIB_DEFECTS
364 // 2176. Special members for wstring_convert and wbuffer_convert
365 wbuffer_convert(const wbuffer_convert&) = delete;
366 wbuffer_convert& operator=(const wbuffer_convert&) = delete;
368 streambuf* rdbuf() const noexcept { return _M_buf; }
370 streambuf*
371 rdbuf(streambuf *__bytebuf) noexcept
373 auto __prev = _M_buf;
374 _M_buf = __bytebuf;
375 return __prev;
378 /// The conversion state following the last conversion.
379 state_type state() const noexcept { return _M_state; }
381 protected:
383 sync()
384 { return _M_buf && _M_conv_put() && !_M_buf->pubsync() ? 0 : -1; }
386 typename _Wide_streambuf::int_type
387 overflow(typename _Wide_streambuf::int_type __out)
389 if (!_M_buf || !_M_conv_put())
390 return _Tr::eof();
391 else if (!_Tr::eq_int_type(__out, _Tr::eof()))
392 return this->sputc(__out);
393 return _Tr::not_eof(__out);
396 typename _Wide_streambuf::int_type
397 underflow()
399 if (!_M_buf)
400 return _Tr::eof();
402 if (this->gptr() < this->egptr() || (_M_buf && _M_conv_get()))
403 return _Tr::to_int_type(*this->gptr());
404 else
405 return _Tr::eof();
408 streamsize
409 xsputn(const typename _Wide_streambuf::char_type* __s, streamsize __n)
411 if (!_M_buf || __n == 0)
412 return 0;
413 streamsize __done = 0;
416 auto __nn = std::min<streamsize>(this->epptr() - this->pptr(),
417 __n - __done);
418 _Tr::copy(this->pptr(), __s + __done, __nn);
419 this->pbump(__nn);
420 __done += __nn;
421 } while (__done < __n && _M_conv_put());
422 return __done;
425 private:
426 // fill the get area from converted contents of the byte stream buffer
427 bool
428 _M_conv_get()
430 const streamsize __pb1 = this->gptr() - this->eback();
431 const streamsize __pb2 = _S_putback_length;
432 const streamsize __npb = std::min(__pb1, __pb2);
434 _Tr::move(_M_get_area + _S_putback_length - __npb,
435 this->gptr() - __npb, __npb);
437 streamsize __nbytes = sizeof(_M_get_buf) - _M_unconv;
438 __nbytes = std::min(__nbytes, _M_buf->in_avail());
439 if (__nbytes < 1)
440 __nbytes = 1;
441 __nbytes = _M_buf->sgetn(_M_get_buf + _M_unconv, __nbytes);
442 if (__nbytes < 1)
443 return false;
444 __nbytes += _M_unconv;
446 // convert _M_get_buf into _M_get_area
448 _Elem* __outbuf = _M_get_area + _S_putback_length;
449 _Elem* __outnext = __outbuf;
450 const char* __bnext = _M_get_buf;
452 codecvt_base::result __result;
453 if (_M_always_noconv)
454 __result = codecvt_base::noconv;
455 else
457 _Elem* __outend = _M_get_area + _S_buffer_length;
459 __result = _M_cvt->in(_M_state,
460 __bnext, __bnext + __nbytes, __bnext,
461 __outbuf, __outend, __outnext);
464 if (__result == codecvt_base::noconv)
466 // cast is safe because noconv means _Elem is same type as char
467 auto __get_buf = reinterpret_cast<const _Elem*>(_M_get_buf);
468 _Tr::copy(__outbuf, __get_buf, __nbytes);
469 _M_unconv = 0;
470 return true;
473 if ((_M_unconv = _M_get_buf + __nbytes - __bnext))
474 char_traits<char>::move(_M_get_buf, __bnext, _M_unconv);
476 this->setg(__outbuf, __outbuf, __outnext);
478 return __result != codecvt_base::error;
481 // unused
482 bool
483 _M_put(...)
484 { return false; }
486 bool
487 _M_put(const char* __p, streamsize __n)
489 if (_M_buf->sputn(__p, __n) < __n)
490 return false;
491 return true;
494 // convert the put area and write to the byte stream buffer
495 bool
496 _M_conv_put()
498 _Elem* const __first = this->pbase();
499 const _Elem* const __last = this->pptr();
500 const streamsize __pending = __last - __first;
502 if (_M_always_noconv)
503 return _M_put(__first, __pending);
505 char __outbuf[2 * _S_buffer_length];
507 const _Elem* __next = __first;
508 const _Elem* __start;
511 __start = __next;
512 char* __outnext = __outbuf;
513 char* const __outlast = __outbuf + sizeof(__outbuf);
514 auto __result = _M_cvt->out(_M_state, __next, __last, __next,
515 __outnext, __outlast, __outnext);
516 if (__result == codecvt_base::error)
517 return false;
518 else if (__result == codecvt_base::noconv)
519 return _M_put(__next, __pending);
521 if (!_M_put(__outbuf, __outnext - __outbuf))
522 return false;
524 while (__next != __last && __next != __start);
526 if (__next != __last)
527 _Tr::move(__first, __next, __last - __next);
529 this->pbump(__first - __next);
530 return __next != __first;
533 streambuf* _M_buf;
534 unique_ptr<_Codecvt> _M_cvt;
535 state_type _M_state;
537 static const streamsize _S_buffer_length = 32;
538 static const streamsize _S_putback_length = 3;
539 _Elem _M_put_area[_S_buffer_length];
540 _Elem _M_get_area[_S_buffer_length];
541 streamsize _M_unconv = 0;
542 char _M_get_buf[_S_buffer_length-_S_putback_length];
543 bool _M_always_noconv;
546 #endif // _GLIBCXX_USE_WCHAR_T
548 /// @} group locales
550 _GLIBCXX_END_NAMESPACE_VERSION
551 } // namespace
553 #endif // __cplusplus
555 #endif /* _LOCALE_CONV_H */