30 #ifndef _LOCALE_CONV_H
31 #define _LOCALE_CONV_H 1
33 #if __cplusplus < 201103L
42 namespace 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 };
331 auto __ptr = __str.
data();
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();
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>>
414 typedef typename _Codecvt::state_type state_type;
429 state_type __state = state_type())
430 : _M_buf(__bytebuf), _M_cvt(__pcvt,
"wbuffer_convert"),
431 _M_state(__state), _M_always_noconv(_M_cvt->always_noconv())
435 this->
setp(_M_put_area, _M_put_area + _S_buffer_length);
436 this->
setg(_M_get_area + _S_putback_length,
437 _M_get_area + _S_putback_length,
438 _M_get_area + _S_putback_length);
449 streambuf* rdbuf() const noexcept {
return _M_buf; }
454 auto __prev = _M_buf;
460 state_type
state() const noexcept {
return _M_state; }
465 {
return _M_buf && _M_conv_put() && !_M_buf->
pubsync() ? 0 : -1; }
470 if (!_M_buf || !_M_conv_put())
472 else if (!_Tr::eq_int_type(__out, _Tr::eof()))
473 return this->
sputc(__out);
474 return _Tr::not_eof(__out);
483 if (this->
gptr() < this->
egptr() || (_M_buf && _M_conv_get()))
484 return _Tr::to_int_type(*this->
gptr());
492 if (!_M_buf || __n == 0)
497 auto __nn = std::min<streamsize>(this->
epptr() - this->
pptr(),
499 _Tr::copy(this->
pptr(), __s + __done, __nn);
502 }
while (__done < __n && _M_conv_put());
515 _Tr::move(_M_get_area + _S_putback_length - __npb,
516 this->
gptr() - __npb, __npb);
518 streamsize __nbytes =
sizeof(_M_get_buf) - _M_unconv;
522 __nbytes = _M_buf->
sgetn(_M_get_buf + _M_unconv, __nbytes);
525 __nbytes += _M_unconv;
529 _Elem* __outbuf = _M_get_area + _S_putback_length;
530 _Elem* __outnext = __outbuf;
531 const char* __bnext = _M_get_buf;
533 codecvt_base::result __result;
534 if (_M_always_noconv)
535 __result = codecvt_base::noconv;
538 _Elem* __outend = _M_get_area + _S_buffer_length;
540 __result = _M_cvt->in(_M_state,
541 __bnext, __bnext + __nbytes, __bnext,
542 __outbuf, __outend, __outnext);
545 if (__result == codecvt_base::noconv)
548 auto __get_buf =
reinterpret_cast<const _Elem*
>(_M_get_buf);
549 _Tr::copy(__outbuf, __get_buf, __nbytes);
554 if ((_M_unconv = _M_get_buf + __nbytes - __bnext))
555 char_traits<char>::move(_M_get_buf, __bnext, _M_unconv);
557 this->
setg(__outbuf, __outbuf, __outnext);
559 return __result != codecvt_base::error;
570 if (_M_buf->
sputn(__p, __n) < __n)
579 _Elem*
const __first = this->
pbase();
580 const _Elem*
const __last = this->
pptr();
581 const streamsize __pending = __last - __first;
583 if (_M_always_noconv)
584 return _M_put(__first, __pending);
586 char __outbuf[2 * _S_buffer_length];
588 const _Elem* __next = __first;
589 const _Elem* __start;
593 char* __outnext = __outbuf;
594 char*
const __outlast = __outbuf +
sizeof(__outbuf);
595 auto __result = _M_cvt->out(_M_state, __next, __last, __next,
596 __outnext, __outlast, __outnext);
597 if (__result == codecvt_base::error)
599 else if (__result == codecvt_base::noconv)
600 return _M_put(__next, __pending);
602 if (!_M_put(__outbuf, __outnext - __outbuf))
605 while (__next != __last && __next != __start);
607 if (__next != __last)
608 _Tr::move(__first, __next, __last - __next);
610 this->
pbump(__first - __next);
611 return __next != __first;
615 __detail::_Scoped_ptr<_Codecvt> _M_cvt;
618 static const streamsize _S_buffer_length = 32;
619 static const streamsize _S_putback_length = 3;
620 _Elem _M_put_area[_S_buffer_length];
621 _Elem _M_get_area[_S_buffer_length];
623 char _M_get_buf[_S_buffer_length-_S_putback_length];
624 bool _M_always_noconv;
629 _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
void setg(char_type *__gbeg, char_type *__gnext, char_type *__gend)
Setting the three read area pointers.
char_type * pbase() const
Access to the put area.
streamsize sputn(const char_type *__s, streamsize __n)
Entry point for all single-character output functions.
char_type * epptr() const
streamsize sgetn(char_type *__s, streamsize __n)
Entry point for xsgetn.
streamsize in_avail()
Looking ahead into the stream.
char_type * eback() const
Access to the get area.
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 * egptr() const
int_type sputc(char_type __c)
Entry point for all single-character output functions.
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.
size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
const _CharT * data() const noexcept
Return const pointer to contents.
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.