9b49617b7a94f861bcc2c850ec6a85743b1e0fa6
[dragonfly.git] / contrib / gcc-5.0 / libstdc++-v3 / include / bits / locale_conv.h
1 // wstring_convert implementation -*- C++ -*-
2
3 // Copyright (C) 2012 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.
10
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.
15
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.
19
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/>.
24
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}
28  */
29
30 #ifndef _LOCALE_CONV_H
31 #define _LOCALE_CONV_H 1
32
33 #if __cplusplus < 201103L
34 # include <bits/c++0x_warning.h>
35 #else
36
37 #include <streambuf>
38 #include "stringfwd.h"
39 #include "allocator.h"
40 #include "codecvt.h"
41 #include "unique_ptr.h"
42
43 namespace std _GLIBCXX_VISIBILITY(default)
44 {
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46
47 #ifdef _GLIBCXX_USE_WCHAR_T
48
49   /**
50    * @addtogroup locales
51    * @{
52    */
53
54   /// String conversions
55   template<typename _Codecvt, typename _Elem = wchar_t,
56            typename _Wide_alloc = allocator<_Elem>,
57            typename _Byte_alloc = allocator<char>>
58     class wstring_convert
59     {
60     public:
61       typedef basic_string<char, char_traits<char>, _Byte_alloc>   byte_string;
62       typedef basic_string<_Elem, char_traits<_Elem>, _Wide_alloc> wide_string;
63       typedef typename _Codecvt::state_type                        state_type;
64       typedef typename wide_string::traits_type::int_type          int_type;
65
66       /** Default constructor.
67        *
68        * @param  __pcvt The facet to use for conversions.
69        *
70        * Takes ownership of @p __pcvt and will delete it in the destructor.
71        */
72       explicit
73       wstring_convert(_Codecvt* __pcvt = new _Codecvt()) : _M_cvt(__pcvt)
74       {
75         if (!_M_cvt)
76           __throw_logic_error("wstring_convert");
77       }
78
79       /** Construct with an initial converstion state.
80        *
81        * @param  __pcvt The facet to use for conversions.
82        * @param  __state Initial conversion state.
83        *
84        * Takes ownership of @p __pcvt and will delete it in the destructor.
85        * The object's conversion state will persist between conversions.
86        */
87       wstring_convert(_Codecvt* __pcvt, state_type __state)
88       : _M_cvt(__pcvt), _M_state(__state), _M_with_cvtstate(true)
89       {
90         if (!_M_cvt)
91           __throw_logic_error("wstring_convert");
92       }
93
94       /** Construct with error strings.
95        *
96        * @param  __byte_err A string to return on failed conversions.
97        * @param  __wide_err A wide string to return on failed conversions.
98        */
99       explicit
100       wstring_convert(const byte_string& __byte_err,
101                       const wide_string& __wide_err = wide_string())
102       : _M_cvt(new _Codecvt),
103         _M_byte_err_string(__byte_err), _M_wide_err_string(__wide_err),
104         _M_with_strings(true)
105       {
106         if (!_M_cvt)
107           __throw_logic_error("wstring_convert");
108       }
109
110       ~wstring_convert() = default;
111
112       // _GLIBCXX_RESOLVE_LIB_DEFECTS
113       // 2176. Special members for wstring_convert and wbuffer_convert
114       wstring_convert(const wstring_convert&) = delete;
115       wstring_convert& operator=(const wstring_convert&) = delete;
116
117       /// @{ Convert from bytes.
118       wide_string
119       from_bytes(char __byte)
120       {
121         char __bytes[2] = { __byte };
122         return from_bytes(__bytes, __bytes+1);
123       }
124
125       wide_string
126       from_bytes(const char* __ptr)
127       { return from_bytes(__ptr, __ptr+char_traits<char>::length(__ptr)); }
128
129       wide_string
130       from_bytes(const byte_string& __str)
131       {
132         auto __ptr = __str.data();
133         return from_bytes(__ptr, __ptr + __str.size());
134       }
135
136       wide_string
137       from_bytes(const char* __first, const char* __last)
138       {
139         auto __errstr = _M_with_strings ? &_M_wide_err_string : nullptr;
140         _ConvFn<char, _Elem> __fn = &_Codecvt::in;
141         return _M_conv(__first, __last, __errstr, __fn);
142       }
143       /// @}
144
145       /// @{ Convert to bytes.
146       byte_string
147       to_bytes(_Elem __wchar)
148       {
149         _Elem __wchars[2] = { __wchar };
150         return to_bytes(__wchars, __wchars+1);
151       }
152
153       byte_string
154       to_bytes(const _Elem* __ptr)
155       {
156         return to_bytes(__ptr, __ptr+wide_string::traits_type::length(__ptr));
157       }
158
159       byte_string
160       to_bytes(const wide_string& __wstr)
161       {
162         auto __ptr = __wstr.data();
163         return to_bytes(__ptr, __ptr + __wstr.size());
164       }
165
166       byte_string
167       to_bytes(const _Elem* __first, const _Elem* __last)
168       {
169         auto __errstr = _M_with_strings ? &_M_byte_err_string : nullptr;
170         _ConvFn<_Elem, char> __fn = &_Codecvt::out;
171         return _M_conv(__first, __last, __errstr, __fn);
172       }
173       /// @}
174
175       // _GLIBCXX_RESOLVE_LIB_DEFECTS
176       // 2174. wstring_convert::converted() should be noexcept
177       /// The number of elements successfully converted in the last conversion.
178       size_t converted() const noexcept { return _M_count; }
179
180       /// The final conversion state of the last conversion.
181       state_type state() const { return _M_state; }
182
183     private:
184       template<typename _InC, typename _OutC>
185         using _ConvFn
186           = codecvt_base::result
187             (_Codecvt::*)(state_type&, const _InC*, const _InC*, const _InC*&,
188                           _OutC*, _OutC*, _OutC*&) const;
189
190       template<typename _InChar, typename _OutStr, typename _MemFn>
191         _OutStr
192         _M_conv(const _InChar* __first, const _InChar* __last,
193                 const _OutStr* __err, _MemFn __memfn)
194         {
195           if (!_M_with_cvtstate)
196             _M_state = state_type();
197
198           auto __outstr = __err ? _OutStr(__err->get_allocator()) : _OutStr();
199           size_t __outchars = 0;
200           auto __next = __first;
201           const auto __maxlen = _M_cvt->max_length();
202
203           codecvt_base::result __result;
204           do
205             {
206               __outstr.resize(__outstr.size() + (__last - __next) + __maxlen);
207               auto __outnext = &__outstr.front() + __outchars;
208               auto const __outlast = &__outstr.back() + 1;
209               __result = ((*_M_cvt).*__memfn)(_M_state, __next, __last, __next,
210                                             __outnext, __outlast, __outnext);
211               __outchars = __outnext - &__outstr.front();
212             }
213           while (__result == codecvt_base::partial && __next != __last
214                  && (__outstr.size() - __outchars) < __maxlen);
215
216           if (__result == codecvt_base::noconv)
217             {
218               __outstr.assign(__first, __last);
219               _M_count = __outstr.size();
220               return __outstr;
221             }
222
223           __outstr.resize(__outchars);
224           _M_count = __next - __first;
225
226           if (__result != codecvt_base::error)
227             return __outstr;
228           else if (__err)
229             return *__err;
230           else
231             __throw_range_error("wstring_convert");
232         }
233
234       unique_ptr<_Codecvt>      _M_cvt;
235       byte_string               _M_byte_err_string;
236       wide_string               _M_wide_err_string;
237       state_type                _M_state = state_type();
238       size_t                    _M_count = 0;
239       bool                      _M_with_cvtstate = false;
240       bool                      _M_with_strings = false;
241     };
242
243   /// Buffer conversions
244   template<typename _Codecvt, typename _Elem = wchar_t,
245            typename _Tr = char_traits<_Elem>>
246     class wbuffer_convert : public basic_streambuf<_Elem, _Tr>
247     {
248       typedef basic_streambuf<_Elem, _Tr> _Wide_streambuf;
249
250     public:
251       typedef typename _Codecvt::state_type state_type;
252
253       /** Default constructor.
254        *
255        * @param  __bytebuf The underlying byte stream buffer.
256        * @param  __pcvt    The facet to use for conversions.
257        * @param  __state   Initial conversion state.
258        *
259        * Takes ownership of @p __pcvt and will delete it in the destructor.
260        */
261       explicit
262       wbuffer_convert(streambuf* __bytebuf = 0, _Codecvt* __pcvt = new _Codecvt,
263                       state_type __state = state_type())
264       : _M_buf(__bytebuf), _M_cvt(__pcvt), _M_state(__state)
265       {
266         if (!_M_cvt)
267           __throw_logic_error("wstring_convert");
268
269         _M_always_noconv = _M_cvt->always_noconv();
270
271         if (_M_buf)
272           {
273             this->setp(_M_put_area, _M_put_area + _S_buffer_length);
274             this->setg(_M_get_area + _S_putback_length,
275                        _M_get_area + _S_putback_length,
276                        _M_get_area + _S_putback_length);
277           }
278       }
279
280       ~wbuffer_convert() = default;
281
282       // _GLIBCXX_RESOLVE_LIB_DEFECTS
283       // 2176. Special members for wstring_convert and wbuffer_convert
284       wbuffer_convert(const wbuffer_convert&) = delete;
285       wbuffer_convert& operator=(const wbuffer_convert&) = delete;
286
287       streambuf* rdbuf() const noexcept { return _M_buf; }
288
289       streambuf*
290       rdbuf(streambuf *__bytebuf) noexcept
291       {
292         auto __prev = _M_buf;
293         _M_buf = __bytebuf;
294         return __prev;
295       }
296
297       /// The conversion state following the last conversion.
298       state_type state() const noexcept { return _M_state; }
299
300     protected:
301       int
302       sync()
303       { return _M_buf && _M_conv_put() && _M_buf->pubsync() ? 0 : -1; }
304
305       typename _Wide_streambuf::int_type
306       overflow(typename _Wide_streambuf::int_type __out)
307       {
308         if (!_M_buf || !_M_conv_put())
309           return _Tr::eof();
310         else if (!_Tr::eq_int_type(__out, _Tr::eof()))
311           return this->sputc(__out);
312         return _Tr::not_eof(__out);
313       }
314
315       typename _Wide_streambuf::int_type
316       underflow()
317       {
318         if (!_M_buf)
319           return _Tr::eof();
320
321         if (this->gptr() < this->egptr() || (_M_buf && _M_conv_get()))
322           return _Tr::to_int_type(*this->gptr());
323         else
324           return _Tr::eof();
325       }
326
327       streamsize
328       xsputn(const typename _Wide_streambuf::char_type* __s, streamsize __n)
329       {
330         if (!_M_buf || __n == 0)
331           return 0;
332         streamsize __done = 0;
333         do
334         {
335           auto __nn = std::min<streamsize>(this->epptr() - this->pptr(),
336                                            __n - __done);
337           _Tr::copy(this->pptr(), __s + __done, __nn);
338           this->pbump(__nn);
339           __done += __nn;
340         } while (__done < __n && _M_conv_put());
341         return __done;
342       }
343
344     private:
345       // fill the get area from converted contents of the byte stream buffer
346       bool
347       _M_conv_get()
348       {
349         const streamsize __pb1 = this->gptr() - this->eback();
350         const streamsize __pb2 = _S_putback_length;
351         const streamsize __npb = std::min(__pb1, __pb2);
352
353         _Tr::move(_M_get_area + _S_putback_length - __npb,
354                   this->gptr() - __npb, __npb);
355
356         streamsize __nbytes = sizeof(_M_get_buf) - _M_unconv;
357         __nbytes = std::min(__nbytes, _M_buf->in_avail());
358         if (__nbytes < 1)
359           __nbytes == 1;
360         __nbytes = _M_buf->sgetn(_M_get_buf + _M_unconv, __nbytes);
361         if (__nbytes < 1)
362           return false;
363         __nbytes += _M_unconv;
364
365         // convert _M_get_buf into _M_get_area
366
367         _Elem* __outbuf = _M_get_area + _S_putback_length;
368         _Elem* __outnext = __outbuf;
369         const char* __bnext = _M_get_buf;
370
371         codecvt_base::result __result;
372         if (_M_always_noconv)
373           __result = codecvt_base::noconv;
374         else
375           {
376             _Elem* __outend = _M_get_area + _S_buffer_length;
377
378             __result = _M_cvt->in(_M_state,
379                                   __bnext, __bnext + __nbytes, __bnext,
380                                   __outbuf, __outend, __outnext);
381           }
382
383         if (__result == codecvt_base::noconv)
384           {
385             // cast is safe because noconv means _Elem is same type as char
386             auto __get_buf = reinterpret_cast<const _Elem*>(_M_get_buf);
387             _Tr::copy(__outbuf, __get_buf, __nbytes);
388             _M_unconv = 0;
389             return true;
390           }
391
392         if ((_M_unconv = _M_get_buf + __nbytes - __bnext))
393           char_traits<char>::move(_M_get_buf, __bnext, _M_unconv);
394
395         this->setg(__outbuf, __outbuf, __outnext);
396
397         return __result != codecvt_base::error;
398       }
399
400       // unused
401       bool
402       _M_put(...)
403       { return false; }
404
405       bool
406       _M_put(const char* __p, streamsize __n)
407       {
408         if (_M_buf->sputn(__p, __n) < __n)
409           return false;
410       }
411
412       // convert the put area and write to the byte stream buffer
413       bool
414       _M_conv_put()
415       {
416         _Elem* const __first = this->pbase();
417         const _Elem* const __last = this->pptr();
418         const streamsize __pending = __last - __first;
419
420         if (_M_always_noconv)
421           return _M_put(__first, __pending);
422
423         char __outbuf[2 * _S_buffer_length];
424
425         const _Elem* __next = __first;
426         const _Elem* __start;
427         do
428           {
429             __start = __next;
430             char* __outnext = __outbuf;
431             char* const __outlast = __outbuf + sizeof(__outbuf);
432             auto __result = _M_cvt->out(_M_state, __next, __last, __next,
433                                         __outnext, __outlast, __outnext);
434             if (__result == codecvt_base::error)
435               return false;
436             else if (__result == codecvt_base::noconv)
437               return _M_put(__next, __pending);
438
439             if (!_M_put(__outbuf, __outnext - __outbuf))
440               return false;
441           }
442         while (__next != __last && __next != __start);
443
444         if (__next != __last)
445           _Tr::move(__first, __next, __last - __next);
446
447         this->pbump(__first - __next);
448         return __next != __first;
449       }
450
451       streambuf*                _M_buf;
452       unique_ptr<_Codecvt>      _M_cvt;
453       state_type                _M_state;
454
455       static const streamsize   _S_buffer_length = 32;
456       static const streamsize   _S_putback_length = 3;
457       _Elem                     _M_put_area[_S_buffer_length];
458       _Elem                     _M_get_area[_S_buffer_length];
459       streamsize                _M_unconv = 0;
460       char                      _M_get_buf[_S_buffer_length-_S_putback_length];
461       bool                      _M_always_noconv;
462     };
463
464   /// @} group locales
465
466 #endif  // _GLIBCXX_USE_WCHAR_T
467
468 _GLIBCXX_END_NAMESPACE_VERSION
469 } // namespace
470
471 #endif // __cplusplus
472
473 #endif /* _LOCALE_CONV_H */