30 #ifndef _LOCALE_CONV_H
31 #define _LOCALE_CONV_H 1
33 #if __cplusplus < 201103L
43 namespace std _GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
47 #ifdef _GLIBCXX_USE_WCHAR_T
54 _GLIBCXX_BEGIN_NAMESPACE_CXX11
56 template<
typename _Codecvt,
typename _Elem = wchar_t,
57 typename _Wide_alloc = allocator<_Elem>,
58 typename _Byte_alloc = allocator<char>>
64 typedef typename _Codecvt::state_type state_type;
65 typedef typename wide_string::traits_type::int_type int_type;
77 __throw_logic_error(
"wstring_convert");
89 : _M_cvt(__pcvt), _M_state(__state), _M_with_cvtstate(true)
92 __throw_logic_error(
"wstring_convert");
102 const wide_string& __wide_err = wide_string())
103 : _M_cvt(new _Codecvt),
104 _M_byte_err_string(__byte_err), _M_wide_err_string(__wide_err),
105 _M_with_strings(true)
108 __throw_logic_error(
"wstring_convert");
122 char __bytes[2] = { __byte };
133 auto __ptr = __str.
data();
140 auto __errstr = _M_with_strings ? &_M_wide_err_string :
nullptr;
141 _ConvFn<char, _Elem> __fn = &_Codecvt::in;
142 return _M_conv(__first, __last, __errstr, __fn);
150 _Elem __wchars[2] = { __wchar };
151 return to_bytes(__wchars, __wchars+1);
157 return to_bytes(__ptr, __ptr+wide_string::traits_type::length(__ptr));
163 auto __ptr = __wstr.
data();
168 to_bytes(
const _Elem* __first,
const _Elem* __last)
170 auto __errstr = _M_with_strings ? &_M_byte_err_string :
nullptr;
171 _ConvFn<_Elem, char> __fn = &_Codecvt::out;
172 return _M_conv(__first, __last, __errstr, __fn);
182 state_type
state()
const {
return _M_state; }
185 template<
typename _InC,
typename _OutC>
187 = codecvt_base::result
188 (_Codecvt::*)(state_type&,
const _InC*,
const _InC*,
const _InC*&,
189 _OutC*, _OutC*, _OutC*&)
const;
191 template<
typename _InChar,
typename _OutStr,
typename _MemFn>
193 _M_conv(
const _InChar* __first,
const _InChar* __last,
194 const _OutStr* __err, _MemFn __memfn)
196 auto __outstr = __err ? _OutStr(__err->get_allocator()) : _OutStr();
198 if (__first == __last)
204 if (!_M_with_cvtstate)
205 _M_state = state_type();
207 size_t __outchars = 0;
208 auto __next = __first;
209 const auto __maxlen = _M_cvt->max_length() + 1;
211 codecvt_base::result __result;
214 __outstr.resize(__outstr.size() + (__last - __next) * __maxlen);
215 auto __outnext = &__outstr.front() + __outchars;
216 auto const __outlast = &__outstr.back() + 1;
217 __result = ((*_M_cvt).*__memfn)(_M_state, __next, __last, __next,
218 __outnext, __outlast, __outnext);
219 __outchars = __outnext - &__outstr.front();
221 while (__result == codecvt_base::partial && __next != __last
222 && (__outstr.size() - __outchars) < __maxlen);
224 if (__result == codecvt_base::noconv)
226 __outstr.assign(__first, __last);
227 _M_count = __outstr.size();
231 __outstr.resize(__outchars);
232 _M_count = __next - __first;
234 if (__result != codecvt_base::error)
239 __throw_range_error(
"wstring_convert");
242 unique_ptr<_Codecvt> _M_cvt;
243 byte_string _M_byte_err_string;
244 wide_string _M_wide_err_string;
245 state_type _M_state = state_type();
247 bool _M_with_cvtstate =
false;
248 bool _M_with_strings =
false;
250 _GLIBCXX_END_NAMESPACE_CXX11
253 template<
typename _Codecvt,
typename _Elem = wchar_t,
254 typename _Tr = char_traits<_Elem>>
257 typedef basic_streambuf<_Elem, _Tr> _Wide_streambuf;
260 typedef typename _Codecvt::state_type state_type;
272 state_type __state = state_type())
273 : _M_buf(__bytebuf), _M_cvt(__pcvt), _M_state(__state)
276 __throw_logic_error(
"wbuffer_convert");
278 _M_always_noconv = _M_cvt->always_noconv();
282 this->setp(_M_put_area, _M_put_area + _S_buffer_length);
283 this->setg(_M_get_area + _S_putback_length,
284 _M_get_area + _S_putback_length,
285 _M_get_area + _S_putback_length);
296 streambuf* rdbuf() const noexcept {
return _M_buf; }
299 rdbuf(streambuf *__bytebuf) noexcept
301 auto __prev = _M_buf;
307 state_type
state() const noexcept {
return _M_state; }
312 {
return _M_buf && _M_conv_put() && _M_buf->pubsync() ? 0 : -1; }
314 typename _Wide_streambuf::int_type
315 overflow(
typename _Wide_streambuf::int_type __out)
317 if (!_M_buf || !_M_conv_put())
319 else if (!_Tr::eq_int_type(__out, _Tr::eof()))
320 return this->sputc(__out);
321 return _Tr::not_eof(__out);
324 typename _Wide_streambuf::int_type
330 if (this->gptr() < this->egptr() || (_M_buf && _M_conv_get()))
331 return _Tr::to_int_type(*this->gptr());
337 xsputn(
const typename _Wide_streambuf::char_type* __s,
streamsize __n)
339 if (!_M_buf || __n == 0)
344 auto __nn = std::min<streamsize>(this->epptr() - this->pptr(),
346 _Tr::copy(this->pptr(), __s + __done, __nn);
349 }
while (__done < __n && _M_conv_put());
358 const streamsize __pb1 = this->gptr() - this->eback();
362 _Tr::move(_M_get_area + _S_putback_length - __npb,
363 this->gptr() - __npb, __npb);
365 streamsize __nbytes =
sizeof(_M_get_buf) - _M_unconv;
366 __nbytes =
std::min(__nbytes, _M_buf->in_avail());
369 __nbytes = _M_buf->sgetn(_M_get_buf + _M_unconv, __nbytes);
372 __nbytes += _M_unconv;
376 _Elem* __outbuf = _M_get_area + _S_putback_length;
377 _Elem* __outnext = __outbuf;
378 const char* __bnext = _M_get_buf;
380 codecvt_base::result __result;
381 if (_M_always_noconv)
382 __result = codecvt_base::noconv;
385 _Elem* __outend = _M_get_area + _S_buffer_length;
387 __result = _M_cvt->in(_M_state,
388 __bnext, __bnext + __nbytes, __bnext,
389 __outbuf, __outend, __outnext);
392 if (__result == codecvt_base::noconv)
395 auto __get_buf =
reinterpret_cast<const _Elem*
>(_M_get_buf);
396 _Tr::copy(__outbuf, __get_buf, __nbytes);
401 if ((_M_unconv = _M_get_buf + __nbytes - __bnext))
402 char_traits<char>::move(_M_get_buf, __bnext, _M_unconv);
404 this->setg(__outbuf, __outbuf, __outnext);
406 return __result != codecvt_base::error;
417 if (_M_buf->sputn(__p, __n) < __n)
425 _Elem*
const __first = this->pbase();
426 const _Elem*
const __last = this->pptr();
427 const streamsize __pending = __last - __first;
429 if (_M_always_noconv)
430 return _M_put(__first, __pending);
432 char __outbuf[2 * _S_buffer_length];
434 const _Elem* __next = __first;
435 const _Elem* __start;
439 char* __outnext = __outbuf;
440 char*
const __outlast = __outbuf +
sizeof(__outbuf);
441 auto __result = _M_cvt->out(_M_state, __next, __last, __next,
442 __outnext, __outlast, __outnext);
443 if (__result == codecvt_base::error)
445 else if (__result == codecvt_base::noconv)
446 return _M_put(__next, __pending);
448 if (!_M_put(__outbuf, __outnext - __outbuf))
451 while (__next != __last && __next != __start);
453 if (__next != __last)
454 _Tr::move(__first, __next, __last - __next);
456 this->pbump(__first - __next);
457 return __next != __first;
461 unique_ptr<_Codecvt> _M_cvt;
464 static const streamsize _S_buffer_length = 32;
465 static const streamsize _S_putback_length = 3;
466 _Elem _M_put_area[_S_buffer_length];
467 _Elem _M_get_area[_S_buffer_length];
469 char _M_get_buf[_S_buffer_length-_S_putback_length];
470 bool _M_always_noconv;
475 #endif // _GLIBCXX_USE_WCHAR_T
477 _GLIBCXX_END_NAMESPACE_VERSION
480 #endif // __cplusplus
Basis for explicit traits specializations.
wbuffer_convert(streambuf *__bytebuf=0, _Codecvt *__pcvt=new _Codecvt, state_type __state=state_type())
Managing sequences of characters and character-like objects.
wide_string from_bytes(char __byte)
Convert from bytes.
size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
wide_string from_bytes(const byte_string &__str)
Convert from bytes.
byte_string to_bytes(const _Elem *__ptr)
Convert to bytes.
state_type state() const noexcept
The conversion state following the last conversion.
const _CharT * data() const noexcept
Return const pointer to contents.
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
wide_string from_bytes(const char *__first, const char *__last)
Convert from bytes.
wstring_convert(_Codecvt *__pcvt=new _Codecvt())
byte_string to_bytes(_Elem __wchar)
Convert to bytes.
byte_string to_bytes(const wide_string &__wstr)
Convert to bytes.
wstring_convert(const byte_string &__byte_err, const wide_string &__wide_err=wide_string())
_GLIBCXX14_CONSTEXPR const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
state_type state() const
The final conversion state of the last conversion.
byte_string to_bytes(const _Elem *__first, const _Elem *__last)
Convert to bytes.
wstring_convert(_Codecvt *__pcvt, state_type __state)
wide_string from_bytes(const char *__ptr)
Convert from bytes.
ISO C++ entities toplevel namespace is std.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
size_t converted() const noexcept
The number of elements successfully converted in the last conversion.