31#define _LOCALE_CONV_H 1
33#if __cplusplus < 201103L
43namespace std _GLIBCXX_VISIBILITY(default)
45_GLIBCXX_BEGIN_NAMESPACE_VERSION
52 template<
typename _OutStr,
typename _InChar,
typename _Codecvt,
53 typename _State,
typename _Fn>
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)
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 && ptrdiff_t(__outstr.size() - __outchars) < __maxlen);
83 if (__result == codecvt_base::error)
85 __count = __next - __first;
91 if _GLIBCXX17_CONSTEXPR (is_same<
typename _Codecvt::intern_type,
92 typename _Codecvt::extern_type>())
93 if (__result == codecvt_base::noconv)
95 __outstr.assign(__first, __last);
96 __count = __last - __first;
100 __outstr.resize(__outchars);
101 __count = __next - __first;
106 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
108 __str_codecvt_in(
const char* __first,
const char* __last,
109 basic_string<_CharT, _Traits, _Alloc>& __outstr,
110 const codecvt<_CharT, char, _State>& __cvt,
111 _State& __state,
size_t& __count)
113 using _Codecvt = codecvt<_CharT, char, _State>;
115 = codecvt_base::result
116 (_Codecvt::*)(_State&, const char*, const char*, const char*&,
117 _CharT*, _CharT*, _CharT*&) const;
119 return __do_str_codecvt(__first, __last, __outstr, __cvt, __state,
124 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
126 __str_codecvt_in(
const char* __first,
const char* __last,
127 basic_string<_CharT, _Traits, _Alloc>& __outstr,
128 const codecvt<_CharT, char, _State>& __cvt)
132 return __str_codecvt_in(__first, __last, __outstr, __cvt, __state, __n);
136 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
138 __str_codecvt_in_all(
const char* __first,
const char* __last,
139 basic_string<_CharT, _Traits, _Alloc>& __outstr,
140 const codecvt<_CharT, char, _State>& __cvt)
144 return __str_codecvt_in(__first, __last, __outstr, __cvt, __state, __n)
145 && (__n == size_t(__last - __first));
149 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
151 __str_codecvt_out(
const _CharT* __first,
const _CharT* __last,
152 basic_string<char, _Traits, _Alloc>& __outstr,
153 const codecvt<_CharT, char, _State>& __cvt,
154 _State& __state,
size_t& __count)
156 using _Codecvt = codecvt<_CharT, char, _State>;
158 = codecvt_base::result
159 (_Codecvt::*)(_State&, const _CharT*, const _CharT*, const _CharT*&,
160 char*, char*, char*&) const;
162 return __do_str_codecvt(__first, __last, __outstr, __cvt, __state,
167 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
169 __str_codecvt_out(
const _CharT* __first,
const _CharT* __last,
170 basic_string<char, _Traits, _Alloc>& __outstr,
171 const codecvt<_CharT, char, _State>& __cvt)
175 return __str_codecvt_out(__first, __last, __outstr, __cvt, __state, __n);
179 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
181 __str_codecvt_out_all(
const _CharT* __first,
const _CharT* __last,
182 basic_string<char, _Traits, _Alloc>& __outstr,
183 const codecvt<_CharT, char, _State>& __cvt)
187 return __str_codecvt_out(__first, __last, __outstr, __cvt, __state, __n)
188 && (__n == size_t(__last - __first));
191#ifdef _GLIBCXX_USE_CHAR8_T
194 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
196 __str_codecvt_out(
const _CharT* __first,
const _CharT* __last,
197 basic_string<char8_t, _Traits, _Alloc>& __outstr,
198 const codecvt<_CharT, char8_t, _State>& __cvt,
199 _State& __state,
size_t& __count)
201 using _Codecvt = codecvt<_CharT, char8_t, _State>;
203 = codecvt_base::result
204 (_Codecvt::*)(_State&, const _CharT*, const _CharT*, const _CharT*&,
205 char8_t*, char8_t*, char8_t*&) const;
207 return __do_str_codecvt(__first, __last, __outstr, __cvt, __state,
211 template<
typename _CharT,
typename _Traits,
typename _Alloc,
typename _State>
213 __str_codecvt_out(
const _CharT* __first,
const _CharT* __last,
214 basic_string<char8_t, _Traits, _Alloc>& __outstr,
215 const codecvt<_CharT, char8_t, _State>& __cvt)
219 return __str_codecvt_out(__first, __last, __outstr, __cvt, __state, __n);
224#ifdef _GLIBCXX_USE_WCHAR_T
226_GLIBCXX_BEGIN_NAMESPACE_CXX11
229 template<
typename _Codecvt,
typename _Elem = wchar_t,
230 typename _Wide_alloc = allocator<_Elem>,
231 typename _Byte_alloc = allocator<char>>
237 typedef typename _Codecvt::state_type state_type;
238 typedef typename wide_string::traits_type::int_type int_type;
253 __throw_logic_error(
"wstring_convert");
265 : _M_cvt(__pcvt), _M_state(__state), _M_with_cvtstate(true)
268 __throw_logic_error(
"wstring_convert");
279 : _M_cvt(new _Codecvt),
280 _M_byte_err_string(__byte_err), _M_wide_err_string(__wide_err),
281 _M_with_strings(true)
284 __throw_logic_error(
"wstring_convert");
298 char __bytes[2] = { __byte };
309 auto __ptr = __str.
data();
316 if (!_M_with_cvtstate)
317 _M_state = state_type();
319 if (__str_codecvt_in(__first, __last, __out, *_M_cvt, _M_state,
323 return _M_wide_err_string;
324 __throw_range_error(
"wstring_convert::from_bytes");
332 _Elem __wchars[2] = { __wchar };
333 return to_bytes(__wchars, __wchars+1);
345 auto __ptr = __wstr.
data();
350 to_bytes(
const _Elem* __first,
const _Elem* __last)
352 if (!_M_with_cvtstate)
353 _M_state = state_type();
355 if (__str_codecvt_out(__first, __last, __out, *_M_cvt, _M_state,
359 return _M_byte_err_string;
360 __throw_range_error(
"wstring_convert::to_bytes");
370 state_type
state()
const {
return _M_state; }
374 byte_string _M_byte_err_string;
375 wide_string _M_wide_err_string;
376 state_type _M_state = state_type();
378 bool _M_with_cvtstate =
false;
379 bool _M_with_strings =
false;
382_GLIBCXX_END_NAMESPACE_CXX11
385 template<
typename _Codecvt,
typename _Elem = wchar_t,
386 typename _Tr = char_traits<_Elem>>
392 typedef typename _Codecvt::state_type state_type;
407 state_type __state = state_type())
408 : _M_buf(__bytebuf), _M_cvt(__pcvt), _M_state(__state)
411 __throw_logic_error(
"wbuffer_convert");
413 _M_always_noconv = _M_cvt->always_noconv();
417 this->
setp(_M_put_area, _M_put_area + _S_buffer_length);
418 this->
setg(_M_get_area + _S_putback_length,
419 _M_get_area + _S_putback_length,
420 _M_get_area + _S_putback_length);
431 streambuf* rdbuf() const noexcept {
return _M_buf; }
436 auto __prev = _M_buf;
442 state_type
state() const noexcept {
return _M_state; }
447 {
return _M_buf && _M_conv_put() && !_M_buf->
pubsync() ? 0 : -1; }
452 if (!_M_buf || !_M_conv_put())
454 else if (!_Tr::eq_int_type(__out, _Tr::eof()))
455 return this->
sputc(__out);
456 return _Tr::not_eof(__out);
465 if (this->
gptr() < this->
egptr() || (_M_buf && _M_conv_get()))
466 return _Tr::to_int_type(*this->
gptr());
474 if (!_M_buf || __n == 0)
479 auto __nn = std::min<streamsize>(this->
epptr() - this->
pptr(),
481 _Tr::copy(this->
pptr(), __s + __done, __nn);
484 }
while (__done < __n && _M_conv_put());
497 _Tr::move(_M_get_area + _S_putback_length - __npb,
498 this->
gptr() - __npb, __npb);
500 streamsize __nbytes =
sizeof(_M_get_buf) - _M_unconv;
504 __nbytes = _M_buf->
sgetn(_M_get_buf + _M_unconv, __nbytes);
507 __nbytes += _M_unconv;
511 _Elem* __outbuf = _M_get_area + _S_putback_length;
512 _Elem* __outnext = __outbuf;
513 const char* __bnext = _M_get_buf;
515 codecvt_base::result __result;
516 if (_M_always_noconv)
517 __result = codecvt_base::noconv;
520 _Elem* __outend = _M_get_area + _S_buffer_length;
522 __result = _M_cvt->in(_M_state,
523 __bnext, __bnext + __nbytes, __bnext,
524 __outbuf, __outend, __outnext);
527 if (__result == codecvt_base::noconv)
530 auto __get_buf =
reinterpret_cast<const _Elem*
>(_M_get_buf);
531 _Tr::copy(__outbuf, __get_buf, __nbytes);
536 if ((_M_unconv = _M_get_buf + __nbytes - __bnext))
537 char_traits<char>::move(_M_get_buf, __bnext, _M_unconv);
539 this->
setg(__outbuf, __outbuf, __outnext);
541 return __result != codecvt_base::error;
552 if (_M_buf->
sputn(__p, __n) < __n)
561 _Elem*
const __first = this->
pbase();
562 const _Elem*
const __last = this->
pptr();
563 const streamsize __pending = __last - __first;
565 if (_M_always_noconv)
566 return _M_put(__first, __pending);
568 char __outbuf[2 * _S_buffer_length];
570 const _Elem* __next = __first;
571 const _Elem* __start;
575 char* __outnext = __outbuf;
576 char*
const __outlast = __outbuf +
sizeof(__outbuf);
577 auto __result = _M_cvt->out(_M_state, __next, __last, __next,
578 __outnext, __outlast, __outnext);
579 if (__result == codecvt_base::error)
581 else if (__result == codecvt_base::noconv)
582 return _M_put(__next, __pending);
584 if (!_M_put(__outbuf, __outnext - __outbuf))
587 while (__next != __last && __next != __start);
589 if (__next != __last)
590 _Tr::move(__first, __next, __last - __next);
592 this->
pbump(__first - __next);
593 return __next != __first;
597 unique_ptr<_Codecvt> _M_cvt;
600 static const streamsize _S_buffer_length = 32;
601 static const streamsize _S_putback_length = 3;
602 _Elem _M_put_area[_S_buffer_length];
603 _Elem _M_get_area[_S_buffer_length];
605 char _M_get_buf[_S_buffer_length-_S_putback_length];
606 bool _M_always_noconv;
613_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.
The actual work of input and output (interface).
traits_type::int_type int_type
char_type * epptr() const
Access to the put area.
char_type * pptr() const
Access to the put area.
void setg(char_type *__gbeg, char_type *__gnext, char_type *__gend)
Setting the three read area pointers.
char_type * eback() const
Access to the get area.
char_type * egptr() const
Access to the get area.
char_type * gptr() const
Access to the get area.
streamsize sputn(const char_type *__s, streamsize __n)
Entry point for all single-character output functions.
streamsize sgetn(char_type *__s, streamsize __n)
Entry point for xsgetn.
streamsize in_avail()
Looking ahead into the stream.
void setp(char_type *__pbeg, char_type *__pend)
Setting the three write area pointers.
void pbump(int __n)
Moving the write position.
int pubsync()
Calls virtual sync function.
char_type * pbase() const
Access to the put area.
int_type sputc(char_type __c)
Entry point for all single-character output functions.
Managing sequences of characters and character-like objects.
const _CharT * data() const noexcept
Return const pointer to contents.
size_type length() const noexcept
Returns the number of characters in the string, not including any null-termination.
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.
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.
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.
20.7.1.2 unique_ptr for single objects.