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;
88#pragma GCC diagnostic push
89#pragma GCC diagnostic ignored "-Wc++17-extensions"
92 if constexpr (
is_same<
typename _Codecvt::intern_type,
93 typename _Codecvt::extern_type>::value)
94 if (__result == codecvt_base::noconv)
96 __outstr.assign(__first, __last);
97 __count = __last - __first;
100#pragma GCC diagnostic pop
102 __outstr.resize(__outchars);
103 __count = __next - __first;
108 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
110 __str_codecvt_in(
const char* __first,
const char* __last,
113 _State& __state,
size_t& __count)
117 = codecvt_base::result
118 (_Codecvt::*)(_State&,
const char*,
const char*,
const char*&,
119 _CharT*, _CharT*, _CharT*&)
const;
121 return __do_str_codecvt(__first, __last, __outstr, __cvt, __state,
126 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
128 __str_codecvt_in(
const char* __first,
const char* __last,
134 return __str_codecvt_in(__first, __last, __outstr, __cvt, __state, __n);
138 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
140 __str_codecvt_in_all(
const char* __first,
const char* __last,
146 return __str_codecvt_in(__first, __last, __outstr, __cvt, __state, __n)
147 && (__n == size_t(__last - __first));
151 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
153 __str_codecvt_out(
const _CharT* __first,
const _CharT* __last,
156 _State& __state,
size_t& __count)
160 = codecvt_base::result
161 (_Codecvt::*)(_State&,
const _CharT*,
const _CharT*,
const _CharT*&,
162 char*,
char*,
char*&)
const;
164 return __do_str_codecvt(__first, __last, __outstr, __cvt, __state,
169 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
171 __str_codecvt_out(
const _CharT* __first,
const _CharT* __last,
177 return __str_codecvt_out(__first, __last, __outstr, __cvt, __state, __n);
181 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
183 __str_codecvt_out_all(
const _CharT* __first,
const _CharT* __last,
189 return __str_codecvt_out(__first, __last, __outstr, __cvt, __state, __n)
190 && (__n == size_t(__last - __first));
193#ifdef _GLIBCXX_USE_CHAR8_T
196 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
198 __str_codecvt_out(
const _CharT* __first,
const _CharT* __last,
201 _State& __state,
size_t& __count)
205 = codecvt_base::result
206 (_Codecvt::*)(_State&,
const _CharT*,
const _CharT*,
const _CharT*&,
207 char8_t*,
char8_t*,
char8_t*&)
const;
209 return __do_str_codecvt(__first, __last, __outstr, __cvt, __state,
213 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
215 __str_codecvt_out(
const _CharT* __first,
const _CharT* __last,
221 return __str_codecvt_out(__first, __last, __outstr, __cvt, __state, __n);
228 template<
typename _Tp>
231 __attribute__((__nonnull__(2)))
233 _Scoped_ptr(_Tp* __ptr) noexcept
237 _Scoped_ptr(_Tp* __ptr,
const char* __msg)
241 __throw_logic_error(__msg);
244 ~_Scoped_ptr() {
delete _M_ptr; }
246 _Scoped_ptr(
const _Scoped_ptr&) =
delete;
247 _Scoped_ptr& operator=(
const _Scoped_ptr&) =
delete;
249 __attribute__((__returns_nonnull__))
250 _Tp* operator->() const noexcept {
return _M_ptr; }
252 _Tp& operator*() const noexcept {
return *_M_ptr; }
259_GLIBCXX_BEGIN_NAMESPACE_CXX11
262 template<
typename _Codecvt,
typename _Elem = wchar_t,
270 typedef typename _Codecvt::state_type state_type;
271 typedef typename wide_string::traits_type::int_type int_type;
295 : _M_cvt(__pcvt,
"std::wstring_convert"),
296 _M_state(__state), _M_with_cvtstate(true)
306 const wide_string& __wide_err = wide_string())
307 : _M_cvt(new _Codecvt),
308 _M_byte_err_string(__byte_err), _M_wide_err_string(__wide_err),
309 _M_with_strings(true)
323 char __bytes[2] = { __byte };
329 {
return from_bytes(__ptr, __ptr+char_traits<char>::length(__ptr)); }
334 auto __ptr = __str.
data();
341 if (!_M_with_cvtstate)
342 _M_state = state_type();
343 wide_string __out{ _M_wide_err_string.get_allocator() };
344 if (__str_codecvt_in(__first, __last, __out, *_M_cvt, _M_state,
348 return _M_wide_err_string;
349 __throw_range_error(
"wstring_convert::from_bytes");
357 _Elem __wchars[2] = { __wchar };
358 return to_bytes(__wchars, __wchars+1);
370 auto __ptr = __wstr.
data();
375 to_bytes(
const _Elem* __first,
const _Elem* __last)
377 if (!_M_with_cvtstate)
378 _M_state = state_type();
379 byte_string __out{ _M_byte_err_string.get_allocator() };
380 if (__str_codecvt_out(__first, __last, __out, *_M_cvt, _M_state,
384 return _M_byte_err_string;
385 __throw_range_error(
"wstring_convert::to_bytes");
395 state_type
state()
const {
return _M_state; }
398 __detail::_Scoped_ptr<_Codecvt> _M_cvt;
399 byte_string _M_byte_err_string;
400 wide_string _M_wide_err_string;
401 state_type _M_state = state_type();
403 bool _M_with_cvtstate =
false;
404 bool _M_with_strings =
false;
407_GLIBCXX_END_NAMESPACE_CXX11
410 template<
typename _Codecvt,
typename _Elem = wchar_t,
411 typename _Tr = char_traits<_Elem>>
418 typedef typename _Codecvt::state_type state_type;
433 state_type __state = state_type())
434 : _M_buf(__bytebuf), _M_cvt(__pcvt,
"std::wbuffer_convert"),
435 _M_state(__state), _M_always_noconv(_M_cvt->always_noconv())
439 this->
setp(_M_put_area, _M_put_area + _S_buffer_length);
440 this->
setg(_M_get_area + _S_putback_length,
441 _M_get_area + _S_putback_length,
442 _M_get_area + _S_putback_length);
453 streambuf* rdbuf() const noexcept {
return _M_buf; }
456 rdbuf(streambuf *__bytebuf)
noexcept
458 auto __prev = _M_buf;
464 state_type
state() const noexcept {
return _M_state; }
469 {
return _M_buf && _M_conv_put() && !_M_buf->pubsync() ? 0 : -1; }
471 typename _Wide_streambuf::int_type
474 if (!_M_buf || !_M_conv_put())
476 else if (!_Tr::eq_int_type(__out, _Tr::eof()))
477 return this->
sputc(__out);
478 return _Tr::not_eof(__out);
481 typename _Wide_streambuf::int_type
487 if (this->
gptr() < this->
egptr() || (_M_buf && _M_conv_get()))
488 return _Tr::to_int_type(*this->
gptr());
494 xsputn(
const typename _Wide_streambuf::char_type* __s,
streamsize __n)
496 if (!_M_buf || __n == 0)
503 _Tr::copy(this->pptr(), __s + __done, __nn);
506 }
while (__done < __n && _M_conv_put());
515 const streamsize __pb1 = this->gptr() - this->eback();
519 _Tr::move(_M_get_area + _S_putback_length - __npb,
520 this->gptr() - __npb, __npb);
522 streamsize __nbytes =
sizeof(_M_get_buf) - _M_unconv;
523 __nbytes =
std::min(__nbytes, _M_buf->in_avail());
526 __nbytes = _M_buf->sgetn(_M_get_buf + _M_unconv, __nbytes);
529 __nbytes += _M_unconv;
533 _Elem* __outbuf = _M_get_area + _S_putback_length;
534 _Elem* __outnext = __outbuf;
535 const char* __bnext = _M_get_buf;
537 codecvt_base::result __result;
538 if (_M_always_noconv)
539 __result = codecvt_base::noconv;
542 _Elem* __outend = _M_get_area + _S_buffer_length;
544 __result = _M_cvt->in(_M_state,
545 __bnext, __bnext + __nbytes, __bnext,
546 __outbuf, __outend, __outnext);
549 if (__result == codecvt_base::noconv)
552 auto __get_buf =
reinterpret_cast<const _Elem*
>(_M_get_buf);
553 _Tr::copy(__outbuf, __get_buf, __nbytes);
558 if ((_M_unconv = _M_get_buf + __nbytes - __bnext))
559 char_traits<char>::move(_M_get_buf, __bnext, _M_unconv);
561 this->setg(__outbuf, __outbuf, __outnext);
563 return __result != codecvt_base::error;
572 _M_put(
const char* __p, streamsize __n)
574 if (_M_buf->sputn(__p, __n) < __n)
583 _Elem*
const __first = this->pbase();
584 const _Elem*
const __last = this->pptr();
585 const streamsize __pending = __last - __first;
587 if (_M_always_noconv)
588 return _M_put(__first, __pending);
590 char __outbuf[2 * _S_buffer_length];
592 const _Elem* __next = __first;
593 const _Elem* __start;
597 char* __outnext = __outbuf;
598 char*
const __outlast = __outbuf +
sizeof(__outbuf);
599 auto __result = _M_cvt->out(_M_state, __next, __last, __next,
600 __outnext, __outlast, __outnext);
601 if (__result == codecvt_base::error)
603 else if (__result == codecvt_base::noconv)
604 return _M_put(__next, __pending);
606 if (!_M_put(__outbuf, __outnext - __outbuf))
609 while (__next != __last && __next != __start);
611 if (__next != __last)
612 _Tr::move(__first, __next, __last - __next);
614 this->pbump(__first - __next);
615 return __next != __first;
619 __detail::_Scoped_ptr<_Codecvt> _M_cvt;
622 static const streamsize _S_buffer_length = 32;
623 static const streamsize _S_putback_length = 3;
624 _Elem _M_put_area[_S_buffer_length];
625 _Elem _M_get_area[_S_buffer_length];
627 char _M_get_buf[_S_buffer_length-_S_putback_length];
628 bool _M_always_noconv;
633_GLIBCXX_END_NAMESPACE_VERSION
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.