31#define _LOCALE_CONV_H 1
33#if __cplusplus < 201103L
42namespace std _GLIBCXX_VISIBILITY(default)
44_GLIBCXX_BEGIN_NAMESPACE_VERSION
53 template<
typename _OutStr,
typename _InChar,
typename _Codecvt,
54 typename _State,
typename _Fn>
56 __do_str_codecvt(
const _InChar* __first,
const _InChar* __last,
57 _OutStr& __outstr,
const _Codecvt& __cvt, _State& __state,
58 size_t& __count, _Fn __fn)
60 if (__first == __last)
67 size_t __outchars = 0;
68 auto __next = __first;
69 const auto __maxlen = __cvt.max_length() + 1;
71 codecvt_base::result __result;
74 __outstr.resize(__outstr.size() + (__last - __next) * __maxlen);
75 auto __outnext = &__outstr.front() + __outchars;
76 auto const __outlast = &__outstr.back() + 1;
77 __result = (__cvt.*__fn)(__state, __next, __last, __next,
78 __outnext, __outlast, __outnext);
79 __outchars = __outnext - &__outstr.front();
81 while (__result == codecvt_base::partial && __next != __last
82 && ptrdiff_t(__outstr.size() - __outchars) < __maxlen);
84 if (__result == codecvt_base::error)
86 __count = __next - __first;
90#pragma GCC diagnostic push
91#pragma GCC diagnostic ignored "-Wc++17-extensions"
94 if constexpr (
is_same<
typename _Codecvt::intern_type,
95 typename _Codecvt::extern_type>::value)
96 if (__result == codecvt_base::noconv)
98 __outstr.assign(__first, __last);
99 __count = __last - __first;
102#pragma GCC diagnostic pop
104 __outstr.resize(__outchars);
105 __count = __next - __first;
110 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
112 __str_codecvt_in(
const char* __first,
const char* __last,
115 _State& __state,
size_t& __count)
119 = codecvt_base::result
120 (_Codecvt::*)(_State&,
const char*,
const char*,
const char*&,
121 _CharT*, _CharT*, _CharT*&)
const;
123 return __do_str_codecvt(__first, __last, __outstr, __cvt, __state,
128 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
130 __str_codecvt_in(
const char* __first,
const char* __last,
136 return __str_codecvt_in(__first, __last, __outstr, __cvt, __state, __n);
140 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
142 __str_codecvt_in_all(
const char* __first,
const char* __last,
148 return __str_codecvt_in(__first, __last, __outstr, __cvt, __state, __n)
149 && (__n == size_t(__last - __first));
153 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
155 __str_codecvt_out(
const _CharT* __first,
const _CharT* __last,
158 _State& __state,
size_t& __count)
162 = codecvt_base::result
163 (_Codecvt::*)(_State&,
const _CharT*,
const _CharT*,
const _CharT*&,
164 char*,
char*,
char*&)
const;
166 return __do_str_codecvt(__first, __last, __outstr, __cvt, __state,
171 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
173 __str_codecvt_out(
const _CharT* __first,
const _CharT* __last,
179 return __str_codecvt_out(__first, __last, __outstr, __cvt, __state, __n);
183 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
185 __str_codecvt_out_all(
const _CharT* __first,
const _CharT* __last,
191 return __str_codecvt_out(__first, __last, __outstr, __cvt, __state, __n)
192 && (__n == size_t(__last - __first));
195#ifdef _GLIBCXX_USE_CHAR8_T
198 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
200 __str_codecvt_out(
const _CharT* __first,
const _CharT* __last,
203 _State& __state,
size_t& __count)
207 = codecvt_base::result
208 (_Codecvt::*)(_State&,
const _CharT*,
const _CharT*,
const _CharT*&,
209 char8_t*,
char8_t*,
char8_t*&)
const;
211 return __do_str_codecvt(__first, __last, __outstr, __cvt, __state,
215 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
217 __str_codecvt_out(
const _CharT* __first,
const _CharT* __last,
223 return __str_codecvt_out(__first, __last, __outstr, __cvt, __state, __n);
230 template<
typename _Tp>
233 __attribute__((__nonnull__(2)))
235 _Scoped_ptr(_Tp* __ptr) noexcept
239 _Scoped_ptr(_Tp* __ptr,
const char* __msg)
243 __throw_logic_error(__msg);
246 ~_Scoped_ptr() {
delete _M_ptr; }
248 _Scoped_ptr(
const _Scoped_ptr&) =
delete;
249 _Scoped_ptr& operator=(
const _Scoped_ptr&) =
delete;
251 __attribute__((__returns_nonnull__))
252 _Tp* operator->() const noexcept {
return _M_ptr; }
254 _Tp&
operator*() const noexcept {
return *_M_ptr; }
262_GLIBCXX_BEGIN_NAMESPACE_CXX11
265 template<
typename _Codecvt,
typename _Elem = wchar_t,
273 typedef typename _Codecvt::state_type state_type;
274 typedef typename wide_string::traits_type::int_type int_type;
298 : _M_cvt(__pcvt,
"std::wstring_convert"),
299 _M_state(__state), _M_with_cvtstate(true)
309 const wide_string& __wide_err = wide_string())
310 : _M_cvt(new _Codecvt),
311 _M_byte_err_string(__byte_err), _M_wide_err_string(__wide_err),
312 _M_with_strings(true)
326 char __bytes[2] = { __byte };
332 {
return from_bytes(__ptr, __ptr+char_traits<char>::length(__ptr)); }
337 auto __ptr = __str.
data();
344 if (!_M_with_cvtstate)
345 _M_state = state_type();
346 wide_string __out{ _M_wide_err_string.get_allocator() };
347 if (__str_codecvt_in(__first, __last, __out, *_M_cvt, _M_state,
351 return _M_wide_err_string;
352 __throw_range_error(
"wstring_convert::from_bytes");
360 _Elem __wchars[2] = { __wchar };
361 return to_bytes(__wchars, __wchars+1);
373 auto __ptr = __wstr.
data();
378 to_bytes(
const _Elem* __first,
const _Elem* __last)
380 if (!_M_with_cvtstate)
381 _M_state = state_type();
382 byte_string __out{ _M_byte_err_string.get_allocator() };
383 if (__str_codecvt_out(__first, __last, __out, *_M_cvt, _M_state,
387 return _M_byte_err_string;
388 __throw_range_error(
"wstring_convert::to_bytes");
398 state_type
state()
const {
return _M_state; }
401 __detail::_Scoped_ptr<_Codecvt> _M_cvt;
402 byte_string _M_byte_err_string;
403 wide_string _M_wide_err_string;
404 state_type _M_state = state_type();
406 bool _M_with_cvtstate =
false;
407 bool _M_with_strings =
false;
410_GLIBCXX_END_NAMESPACE_CXX11
413 template<
typename _Codecvt,
typename _Elem = wchar_t,
414 typename _Tr = char_traits<_Elem>>
421 typedef typename _Codecvt::state_type state_type;
436 state_type __state = state_type())
437 : _M_buf(__bytebuf), _M_cvt(__pcvt,
"std::wbuffer_convert"),
438 _M_state(__state), _M_always_noconv(_M_cvt->always_noconv())
442 this->
setp(_M_put_area, _M_put_area + _S_buffer_length);
443 this->
setg(_M_get_area + _S_putback_length,
444 _M_get_area + _S_putback_length,
445 _M_get_area + _S_putback_length);
456 streambuf* rdbuf() const noexcept {
return _M_buf; }
459 rdbuf(streambuf *__bytebuf)
noexcept
461 auto __prev = _M_buf;
467 state_type
state() const noexcept {
return _M_state; }
472 {
return _M_buf && _M_conv_put() && !_M_buf->pubsync() ? 0 : -1; }
474 typename _Wide_streambuf::int_type
477 if (!_M_buf || !_M_conv_put())
479 else if (!_Tr::eq_int_type(__out, _Tr::eof()))
480 return this->
sputc(__out);
481 return _Tr::not_eof(__out);
484 typename _Wide_streambuf::int_type
490 if (this->
gptr() < this->
egptr() || (_M_buf && _M_conv_get()))
491 return _Tr::to_int_type(*this->
gptr());
497 xsputn(
const typename _Wide_streambuf::char_type* __s,
streamsize __n)
499 if (!_M_buf || __n == 0)
506 _Tr::copy(this->pptr(), __s + __done, __nn);
509 }
while (__done < __n && _M_conv_put());
518 const streamsize __pb1 = this->gptr() - this->eback();
522 _Tr::move(_M_get_area + _S_putback_length - __npb,
523 this->gptr() - __npb, __npb);
525 streamsize __nbytes =
sizeof(_M_get_buf) - _M_unconv;
526 __nbytes =
std::min(__nbytes, _M_buf->in_avail());
529 __nbytes = _M_buf->sgetn(_M_get_buf + _M_unconv, __nbytes);
532 __nbytes += _M_unconv;
536 _Elem* __outbuf = _M_get_area + _S_putback_length;
537 _Elem* __outnext = __outbuf;
538 const char* __bnext = _M_get_buf;
540 codecvt_base::result __result;
541 if (_M_always_noconv)
542 __result = codecvt_base::noconv;
545 _Elem* __outend = _M_get_area + _S_buffer_length;
547 __result = _M_cvt->in(_M_state,
548 __bnext, __bnext + __nbytes, __bnext,
549 __outbuf, __outend, __outnext);
552 if (__result == codecvt_base::noconv)
555 auto __get_buf =
reinterpret_cast<const _Elem*
>(_M_get_buf);
556 _Tr::copy(__outbuf, __get_buf, __nbytes);
561 if ((_M_unconv = _M_get_buf + __nbytes - __bnext))
562 char_traits<char>::move(_M_get_buf, __bnext, _M_unconv);
564 this->setg(__outbuf, __outbuf, __outnext);
566 return __result != codecvt_base::error;
575 _M_put(
const char* __p, streamsize __n)
577 if (_M_buf->sputn(__p, __n) < __n)
586 _Elem*
const __first = this->pbase();
587 const _Elem*
const __last = this->pptr();
588 const streamsize __pending = __last - __first;
590 if (_M_always_noconv)
591 return _M_put(__first, __pending);
593 char __outbuf[2 * _S_buffer_length];
595 const _Elem* __next = __first;
596 const _Elem* __start;
600 char* __outnext = __outbuf;
601 char*
const __outlast = __outbuf +
sizeof(__outbuf);
602 auto __result = _M_cvt->out(_M_state, __next, __last, __next,
603 __outnext, __outlast, __outnext);
604 if (__result == codecvt_base::error)
606 else if (__result == codecvt_base::noconv)
607 return _M_put(__next, __pending);
609 if (!_M_put(__outbuf, __outnext - __outbuf))
612 while (__next != __last && __next != __start);
614 if (__next != __last)
615 _Tr::move(__first, __next, __last - __next);
617 this->pbump(__first - __next);
618 return __next != __first;
622 __detail::_Scoped_ptr<_Codecvt> _M_cvt;
625 static const streamsize _S_buffer_length = 32;
626 static const streamsize _S_putback_length = 3;
627 _Elem _M_put_area[_S_buffer_length];
628 _Elem _M_get_area[_S_buffer_length];
630 char _M_get_buf[_S_buffer_length-_S_putback_length];
631 bool _M_always_noconv;
636_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.
Implementation details not part of the namespace std interface.
traits_type::int_type int_type
void setg(char_type *__gbeg, char_type *__gnext, char_type *__gend)
Setting the three read area pointers.
char_type * egptr() const
Access to the get area.
char_type * gptr() const
Access to the get area.
void setp(char_type *__pbeg, char_type *__pend)
Setting the three write area pointers.
int_type sputc(char_type __c)
Entry point for all single-character output functions.
basic_streambuf()
Base constructor.
The standard allocator, as per C++03 [20.4.1].
Managing sequences of characters and character-like objects.
constexpr size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
constexpr const _CharT * data() const noexcept
Return const pointer to contents.
constexpr size_type length() const noexcept
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.
Primary class template codecvt.
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.