31#define _LOCALE_CONV_H 1
33#if __cplusplus < 201103L
42namespace std _GLIBCXX_VISIBILITY(default)
44_GLIBCXX_BEGIN_NAMESPACE_VERSION
51 template<
typename _OutStr,
typename _InChar,
typename _Codecvt,
52 typename _State,
typename _Fn>
54 __do_str_codecvt(
const _InChar* __first,
const _InChar* __last,
55 _OutStr& __outstr,
const _Codecvt& __cvt, _State& __state,
56 size_t& __count, _Fn __fn)
58 if (__first == __last)
65 size_t __outchars = 0;
66 auto __next = __first;
67 const auto __maxlen = __cvt.max_length() + 1;
69 codecvt_base::result __result;
72 __outstr.resize(__outstr.size() + (__last - __next) * __maxlen);
73 auto __outnext = &__outstr.front() + __outchars;
74 auto const __outlast = &__outstr.back() + 1;
75 __result = (__cvt.*__fn)(__state, __next, __last, __next,
76 __outnext, __outlast, __outnext);
77 __outchars = __outnext - &__outstr.front();
79 while (__result == codecvt_base::partial && __next != __last
80 && ptrdiff_t(__outstr.size() - __outchars) < __maxlen);
82 if (__result == codecvt_base::error)
84 __count = __next - __first;
90 if _GLIBCXX17_CONSTEXPR (is_same<
typename _Codecvt::intern_type,
91 typename _Codecvt::extern_type>())
92 if (__result == codecvt_base::noconv)
94 __outstr.assign(__first, __last);
95 __count = __last - __first;
99 __outstr.resize(__outchars);
100 __count = __next - __first;
105 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
107 __str_codecvt_in(
const char* __first,
const char* __last,
108 basic_string<_CharT, _Traits, _Alloc>& __outstr,
109 const codecvt<_CharT, char, _State>& __cvt,
110 _State& __state,
size_t& __count)
112 using _Codecvt = codecvt<_CharT, char, _State>;
114 = codecvt_base::result
115 (_Codecvt::*)(_State&, const char*, const char*, const char*&,
116 _CharT*, _CharT*, _CharT*&) const;
118 return __do_str_codecvt(__first, __last, __outstr, __cvt, __state,
123 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
125 __str_codecvt_in(
const char* __first,
const char* __last,
126 basic_string<_CharT, _Traits, _Alloc>& __outstr,
127 const codecvt<_CharT, char, _State>& __cvt)
131 return __str_codecvt_in(__first, __last, __outstr, __cvt, __state, __n);
135 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
137 __str_codecvt_in_all(
const char* __first,
const char* __last,
138 basic_string<_CharT, _Traits, _Alloc>& __outstr,
139 const codecvt<_CharT, char, _State>& __cvt)
143 return __str_codecvt_in(__first, __last, __outstr, __cvt, __state, __n)
144 && (__n == size_t(__last - __first));
148 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
150 __str_codecvt_out(
const _CharT* __first,
const _CharT* __last,
151 basic_string<char, _Traits, _Alloc>& __outstr,
152 const codecvt<_CharT, char, _State>& __cvt,
153 _State& __state,
size_t& __count)
155 using _Codecvt = codecvt<_CharT, char, _State>;
157 = codecvt_base::result
158 (_Codecvt::*)(_State&, const _CharT*, const _CharT*, const _CharT*&,
159 char*, char*, char*&) const;
161 return __do_str_codecvt(__first, __last, __outstr, __cvt, __state,
166 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
168 __str_codecvt_out(
const _CharT* __first,
const _CharT* __last,
169 basic_string<char, _Traits, _Alloc>& __outstr,
170 const codecvt<_CharT, char, _State>& __cvt)
174 return __str_codecvt_out(__first, __last, __outstr, __cvt, __state, __n);
178 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
180 __str_codecvt_out_all(
const _CharT* __first,
const _CharT* __last,
181 basic_string<char, _Traits, _Alloc>& __outstr,
182 const codecvt<_CharT, char, _State>& __cvt)
186 return __str_codecvt_out(__first, __last, __outstr, __cvt, __state, __n)
187 && (__n == size_t(__last - __first));
190#ifdef _GLIBCXX_USE_CHAR8_T
193 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
195 __str_codecvt_out(
const _CharT* __first,
const _CharT* __last,
196 basic_string<char8_t, _Traits, _Alloc>& __outstr,
197 const codecvt<_CharT, char8_t, _State>& __cvt,
198 _State& __state,
size_t& __count)
200 using _Codecvt = codecvt<_CharT, char8_t, _State>;
202 = codecvt_base::result
203 (_Codecvt::*)(_State&, const _CharT*, const _CharT*, const _CharT*&,
204 char8_t*, char8_t*, char8_t*&) const;
206 return __do_str_codecvt(__first, __last, __outstr, __cvt, __state,
210 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
212 __str_codecvt_out(
const _CharT* __first,
const _CharT* __last,
213 basic_string<char8_t, _Traits, _Alloc>& __outstr,
214 const codecvt<_CharT, char8_t, _State>& __cvt)
218 return __str_codecvt_out(__first, __last, __outstr, __cvt, __state, __n);
225 template<
typename _Tp>
228 __attribute__((__nonnull__(2)))
230 _Scoped_ptr(_Tp* __ptr) noexcept
234 _Scoped_ptr(_Tp* __ptr,
const char* __msg)
238 __throw_logic_error(__msg);
241 ~_Scoped_ptr() {
delete _M_ptr; }
243 _Scoped_ptr(
const _Scoped_ptr&) =
delete;
244 _Scoped_ptr& operator=(
const _Scoped_ptr&) =
delete;
246 __attribute__((__returns_nonnull__))
247 _Tp* operator->() const noexcept {
return _M_ptr; }
249 _Tp&
operator*() const noexcept {
return *_M_ptr; }
256_GLIBCXX_BEGIN_NAMESPACE_CXX11
259 template<
typename _Codecvt,
typename _Elem = wchar_t,
260 typename _Wide_alloc = allocator<_Elem>,
261 typename _Byte_alloc = allocator<char>>
267 typedef typename _Codecvt::state_type state_type;
268 typedef typename wide_string::traits_type::int_type int_type;
292 : _M_cvt(__pcvt,
"wstring_convert"),
293 _M_state(__state), _M_with_cvtstate(true)
304 : _M_cvt(new _Codecvt),
305 _M_byte_err_string(__byte_err), _M_wide_err_string(__wide_err),
306 _M_with_strings(true)
320 char __bytes[2] = { __byte };
321 return from_bytes(__bytes, __bytes+1);
331 auto __ptr = __str.
data();
332 return from_bytes(__ptr, __ptr + __str.
size());
338 if (!_M_with_cvtstate)
339 _M_state = state_type();
341 if (__str_codecvt_in(__first, __last, __out, *_M_cvt, _M_state,
345 return _M_wide_err_string;
346 __throw_range_error(
"wstring_convert::from_bytes");
354 _Elem __wchars[2] = { __wchar };
355 return to_bytes(__wchars, __wchars+1);
361 return to_bytes(__ptr, __ptr+wide_string::traits_type::length(__ptr));
367 auto __ptr = __wstr.
data();
368 return to_bytes(__ptr, __ptr + __wstr.
size());
372 to_bytes(
const _Elem* __first,
const _Elem* __last)
374 if (!_M_with_cvtstate)
375 _M_state = state_type();
377 if (__str_codecvt_out(__first, __last, __out, *_M_cvt, _M_state,
381 return _M_byte_err_string;
382 __throw_range_error(
"wstring_convert::to_bytes");
392 state_type
state()
const {
return _M_state; }
395 __detail::_Scoped_ptr<_Codecvt> _M_cvt;
396 byte_string _M_byte_err_string;
397 wide_string _M_wide_err_string;
398 state_type _M_state = state_type();
400 bool _M_with_cvtstate =
false;
401 bool _M_with_strings =
false;
404_GLIBCXX_END_NAMESPACE_CXX11
407 template<
typename _Codecvt,
typename _Elem = wchar_t,
408 typename _Tr = char_traits<_Elem>>
415 typedef typename _Codecvt::state_type state_type;
430 state_type __state = state_type())
431 : _M_buf(__bytebuf), _M_cvt(__pcvt,
"wbuffer_convert"),
432 _M_state(__state), _M_always_noconv(_M_cvt->always_noconv())
436 this->setp(_M_put_area, _M_put_area + _S_buffer_length);
437 this->setg(_M_get_area + _S_putback_length,
438 _M_get_area + _S_putback_length,
439 _M_get_area + _S_putback_length);
450 streambuf* rdbuf() const noexcept {
return _M_buf; }
453 rdbuf(streambuf *__bytebuf)
noexcept
455 auto __prev = _M_buf;
461 state_type
state() const noexcept {
return _M_state; }
466 {
return _M_buf && _M_conv_put() && !_M_buf->pubsync() ? 0 : -1; }
468 typename _Wide_streambuf::int_type
471 if (!_M_buf || !_M_conv_put())
473 else if (!_Tr::eq_int_type(__out, _Tr::eof()))
474 return this->sputc(__out);
475 return _Tr::not_eof(__out);
478 typename _Wide_streambuf::int_type
484 if (this->gptr() < this->egptr() || (_M_buf && _M_conv_get()))
485 return _Tr::to_int_type(*this->gptr());
491 xsputn(
const typename _Wide_streambuf::char_type* __s,
streamsize __n)
493 if (!_M_buf || __n == 0)
498 auto __nn = std::min<streamsize>(this->epptr() - this->pptr(),
500 _Tr::copy(this->pptr(), __s + __done, __nn);
503 }
while (__done < __n && _M_conv_put());
512 const streamsize __pb1 = this->gptr() - this->eback();
516 _Tr::move(_M_get_area + _S_putback_length - __npb,
517 this->gptr() - __npb, __npb);
519 streamsize __nbytes =
sizeof(_M_get_buf) - _M_unconv;
520 __nbytes =
std::min(__nbytes, _M_buf->in_avail());
523 __nbytes = _M_buf->sgetn(_M_get_buf + _M_unconv, __nbytes);
526 __nbytes += _M_unconv;
530 _Elem* __outbuf = _M_get_area + _S_putback_length;
531 _Elem* __outnext = __outbuf;
532 const char* __bnext = _M_get_buf;
534 codecvt_base::result __result;
535 if (_M_always_noconv)
536 __result = codecvt_base::noconv;
539 _Elem* __outend = _M_get_area + _S_buffer_length;
541 __result = _M_cvt->in(_M_state,
542 __bnext, __bnext + __nbytes, __bnext,
543 __outbuf, __outend, __outnext);
546 if (__result == codecvt_base::noconv)
549 auto __get_buf =
reinterpret_cast<const _Elem*
>(_M_get_buf);
550 _Tr::copy(__outbuf, __get_buf, __nbytes);
555 if ((_M_unconv = _M_get_buf + __nbytes - __bnext))
556 char_traits<char>::move(_M_get_buf, __bnext, _M_unconv);
558 this->setg(__outbuf, __outbuf, __outnext);
560 return __result != codecvt_base::error;
569 _M_put(
const char* __p, streamsize __n)
571 if (_M_buf->sputn(__p, __n) < __n)
580 _Elem*
const __first = this->pbase();
581 const _Elem*
const __last = this->pptr();
582 const streamsize __pending = __last - __first;
584 if (_M_always_noconv)
585 return _M_put(__first, __pending);
587 char __outbuf[2 * _S_buffer_length];
589 const _Elem* __next = __first;
590 const _Elem* __start;
594 char* __outnext = __outbuf;
595 char*
const __outlast = __outbuf +
sizeof(__outbuf);
596 auto __result = _M_cvt->out(_M_state, __next, __last, __next,
597 __outnext, __outlast, __outnext);
598 if (__result == codecvt_base::error)
600 else if (__result == codecvt_base::noconv)
601 return _M_put(__next, __pending);
603 if (!_M_put(__outbuf, __outnext - __outbuf))
606 while (__next != __last && __next != __start);
608 if (__next != __last)
609 _Tr::move(__first, __next, __last - __next);
611 this->pbump(__first - __next);
612 return __next != __first;
616 __detail::_Scoped_ptr<_Codecvt> _M_cvt;
619 static const streamsize _S_buffer_length = 32;
620 static const streamsize _S_putback_length = 3;
621 _Elem _M_put_area[_S_buffer_length];
622 _Elem _M_get_area[_S_buffer_length];
624 char _M_get_buf[_S_buffer_length-_S_putback_length];
625 bool _M_always_noconv;
630_GLIBCXX_END_NAMESPACE_VERSION
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
basic_streambuf< char > streambuf
Base class for char buffers.
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
ISO C++ entities toplevel namespace is std.
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
The actual work of input and output (interface).
traits_type::int_type int_type
Basis for explicit traits specializations.
result in(state_type &__state, const extern_type *__from, const extern_type *__from_end, const extern_type *&__from_next, intern_type *__to, intern_type *__to_end, intern_type *&__to_next) const
Convert from external to internal character set.
result out(state_type &__state, const intern_type *__from, const intern_type *__from_end, const intern_type *&__from_next, extern_type *__to, extern_type *__to_end, extern_type *&__to_next) const
Convert from internal to external character set.
Managing sequences of characters and character-like objects.
const _CharT * data() const noexcept
Return const pointer to contents.
size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
allocator_type get_allocator() const noexcept
Return copy of allocator used to construct this string.
size_t converted() const noexcept
The number of elements successfully converted in the last conversion.
wstring_convert(_Codecvt *__pcvt)
wstring_convert()
Default constructor.
wide_string from_bytes(char __byte)
Convert from bytes.
byte_string to_bytes(_Elem __wchar)
Convert to bytes.
byte_string to_bytes(const _Elem *__ptr)
Convert to bytes.
byte_string to_bytes(const wide_string &__wstr)
Convert to bytes.
byte_string to_bytes(const _Elem *__first, const _Elem *__last)
Convert to bytes.
wstring_convert(const byte_string &__byte_err, const wide_string &__wide_err=wide_string())
wide_string from_bytes(const char *__first, const char *__last)
Convert from bytes.
state_type state() const
The final conversion state of the last conversion.
wide_string from_bytes(const char *__ptr)
Convert from bytes.
wstring_convert(_Codecvt *__pcvt, state_type __state)
wide_string from_bytes(const byte_string &__str)
Convert from bytes.
_Wide_streambuf::int_type underflow()
Fetches more data from the controlled sequence.
state_type state() const noexcept
The conversion state following the last conversion.
wbuffer_convert(streambuf *__bytebuf, _Codecvt *__pcvt=new _Codecvt, state_type __state=state_type())
int sync()
Synchronizes the buffer arrays with the controlled sequences.
wbuffer_convert()
Default constructor.
_Wide_streambuf::int_type overflow(typename _Wide_streambuf::int_type __out)
Consumes data from the buffer; writes to the controlled sequence.