xref: /dflybsd-src/contrib/gcc-8.0/libstdc++-v3/include/bits/locale_conv.h (revision 95059079af47f9a66a175f374f2da1a5020e3255)
138fd1498Szrj // wstring_convert implementation -*- C++ -*-
238fd1498Szrj 
338fd1498Szrj // Copyright (C) 2015-2018 Free Software Foundation, Inc.
438fd1498Szrj //
538fd1498Szrj // This file is part of the GNU ISO C++ Library.  This library is free
638fd1498Szrj // software; you can redistribute it and/or modify it under the
738fd1498Szrj // terms of the GNU General Public License as published by the
838fd1498Szrj // Free Software Foundation; either version 3, or (at your option)
938fd1498Szrj // any later version.
1038fd1498Szrj 
1138fd1498Szrj // This library is distributed in the hope that it will be useful,
1238fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
1338fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1438fd1498Szrj // GNU General Public License for more details.
1538fd1498Szrj 
1638fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional
1738fd1498Szrj // permissions described in the GCC Runtime Library Exception, version
1838fd1498Szrj // 3.1, as published by the Free Software Foundation.
1938fd1498Szrj 
2038fd1498Szrj // You should have received a copy of the GNU General Public License and
2138fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program;
2238fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
2338fd1498Szrj // <http://www.gnu.org/licenses/>.
2438fd1498Szrj 
2538fd1498Szrj /** @file bits/locale_conv.h
2638fd1498Szrj  *  This is an internal header file, included by other library headers.
2738fd1498Szrj  *  Do not attempt to use it directly. @headername{locale}
2838fd1498Szrj  */
2938fd1498Szrj 
3038fd1498Szrj #ifndef _LOCALE_CONV_H
3138fd1498Szrj #define _LOCALE_CONV_H 1
3238fd1498Szrj 
3338fd1498Szrj #if __cplusplus < 201103L
3438fd1498Szrj # include <bits/c++0x_warning.h>
3538fd1498Szrj #else
3638fd1498Szrj 
3738fd1498Szrj #include <streambuf>
38*58e805e6Szrj #include <bits/stringfwd.h>
39*58e805e6Szrj #include <bits/allocator.h>
40*58e805e6Szrj #include <bits/codecvt.h>
41*58e805e6Szrj #include <bits/unique_ptr.h>
4238fd1498Szrj 
_GLIBCXX_VISIBILITY(default)4338fd1498Szrj namespace std _GLIBCXX_VISIBILITY(default)
4438fd1498Szrj {
4538fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION
4638fd1498Szrj 
4738fd1498Szrj   /**
4838fd1498Szrj    * @addtogroup locales
4938fd1498Szrj    * @{
5038fd1498Szrj    */
5138fd1498Szrj 
5238fd1498Szrj   template<typename _OutStr, typename _InChar, typename _Codecvt,
5338fd1498Szrj 	   typename _State, typename _Fn>
5438fd1498Szrj     bool
5538fd1498Szrj     __do_str_codecvt(const _InChar* __first, const _InChar* __last,
5638fd1498Szrj 		     _OutStr& __outstr, const _Codecvt& __cvt, _State& __state,
5738fd1498Szrj 		     size_t& __count, _Fn __fn)
5838fd1498Szrj     {
5938fd1498Szrj       if (__first == __last)
6038fd1498Szrj 	{
6138fd1498Szrj 	  __outstr.clear();
6238fd1498Szrj 	  __count = 0;
6338fd1498Szrj 	  return true;
6438fd1498Szrj 	}
6538fd1498Szrj 
6638fd1498Szrj       size_t __outchars = 0;
6738fd1498Szrj       auto __next = __first;
6838fd1498Szrj       const auto __maxlen = __cvt.max_length() + 1;
6938fd1498Szrj 
7038fd1498Szrj       codecvt_base::result __result;
7138fd1498Szrj       do
7238fd1498Szrj 	{
7338fd1498Szrj 	  __outstr.resize(__outstr.size() + (__last - __next) * __maxlen);
7438fd1498Szrj 	  auto __outnext = &__outstr.front() + __outchars;
7538fd1498Szrj 	  auto const __outlast = &__outstr.back() + 1;
7638fd1498Szrj 	  __result = (__cvt.*__fn)(__state, __next, __last, __next,
7738fd1498Szrj 					__outnext, __outlast, __outnext);
7838fd1498Szrj 	  __outchars = __outnext - &__outstr.front();
7938fd1498Szrj 	}
8038fd1498Szrj       while (__result == codecvt_base::partial && __next != __last
8138fd1498Szrj 	     && (__outstr.size() - __outchars) < __maxlen);
8238fd1498Szrj 
8338fd1498Szrj       if (__result == codecvt_base::error)
8438fd1498Szrj 	{
8538fd1498Szrj 	  __count = __next - __first;
8638fd1498Szrj 	  return false;
8738fd1498Szrj 	}
8838fd1498Szrj 
8938fd1498Szrj       if (__result == codecvt_base::noconv)
9038fd1498Szrj 	{
9138fd1498Szrj 	  __outstr.assign(__first, __last);
9238fd1498Szrj 	  __count = __last - __first;
9338fd1498Szrj 	}
9438fd1498Szrj       else
9538fd1498Szrj 	{
9638fd1498Szrj 	  __outstr.resize(__outchars);
9738fd1498Szrj 	  __count = __next - __first;
9838fd1498Szrj 	}
9938fd1498Szrj 
10038fd1498Szrj       return true;
10138fd1498Szrj     }
10238fd1498Szrj 
10338fd1498Szrj   // Convert narrow character string to wide.
10438fd1498Szrj   template<typename _CharT, typename _Traits, typename _Alloc, typename _State>
10538fd1498Szrj     inline bool
10638fd1498Szrj     __str_codecvt_in(const char* __first, const char* __last,
10738fd1498Szrj 		     basic_string<_CharT, _Traits, _Alloc>& __outstr,
10838fd1498Szrj 		     const codecvt<_CharT, char, _State>& __cvt,
10938fd1498Szrj 		     _State& __state, size_t& __count)
11038fd1498Szrj     {
11138fd1498Szrj       using _Codecvt = codecvt<_CharT, char, _State>;
11238fd1498Szrj       using _ConvFn
11338fd1498Szrj 	= codecvt_base::result
11438fd1498Szrj 	  (_Codecvt::*)(_State&, const char*, const char*, const char*&,
11538fd1498Szrj 			_CharT*, _CharT*, _CharT*&) const;
11638fd1498Szrj       _ConvFn __fn = &codecvt<_CharT, char, _State>::in;
11738fd1498Szrj       return __do_str_codecvt(__first, __last, __outstr, __cvt, __state,
11838fd1498Szrj 			      __count, __fn);
11938fd1498Szrj     }
12038fd1498Szrj 
12138fd1498Szrj   template<typename _CharT, typename _Traits, typename _Alloc, typename _State>
12238fd1498Szrj     inline bool
12338fd1498Szrj     __str_codecvt_in(const char* __first, const char* __last,
12438fd1498Szrj 		     basic_string<_CharT, _Traits, _Alloc>& __outstr,
12538fd1498Szrj 		     const codecvt<_CharT, char, _State>& __cvt)
12638fd1498Szrj     {
12738fd1498Szrj       _State __state = {};
12838fd1498Szrj       size_t __n;
12938fd1498Szrj       return __str_codecvt_in(__first, __last, __outstr, __cvt, __state, __n);
13038fd1498Szrj     }
13138fd1498Szrj 
13238fd1498Szrj   // Convert wide character string to narrow.
13338fd1498Szrj   template<typename _CharT, typename _Traits, typename _Alloc, typename _State>
13438fd1498Szrj     inline bool
13538fd1498Szrj     __str_codecvt_out(const _CharT* __first, const _CharT* __last,
13638fd1498Szrj 		      basic_string<char, _Traits, _Alloc>& __outstr,
13738fd1498Szrj 		      const codecvt<_CharT, char, _State>& __cvt,
13838fd1498Szrj 		      _State& __state, size_t& __count)
13938fd1498Szrj     {
14038fd1498Szrj       using _Codecvt = codecvt<_CharT, char, _State>;
14138fd1498Szrj       using _ConvFn
14238fd1498Szrj 	= codecvt_base::result
14338fd1498Szrj 	  (_Codecvt::*)(_State&, const _CharT*, const _CharT*, const _CharT*&,
14438fd1498Szrj 			char*, char*, char*&) const;
14538fd1498Szrj       _ConvFn __fn = &codecvt<_CharT, char, _State>::out;
14638fd1498Szrj       return __do_str_codecvt(__first, __last, __outstr, __cvt, __state,
14738fd1498Szrj 			      __count, __fn);
14838fd1498Szrj     }
14938fd1498Szrj 
15038fd1498Szrj   template<typename _CharT, typename _Traits, typename _Alloc, typename _State>
15138fd1498Szrj     inline bool
15238fd1498Szrj     __str_codecvt_out(const _CharT* __first, const _CharT* __last,
15338fd1498Szrj 		      basic_string<char, _Traits, _Alloc>& __outstr,
15438fd1498Szrj 		      const codecvt<_CharT, char, _State>& __cvt)
15538fd1498Szrj     {
15638fd1498Szrj       _State __state = {};
15738fd1498Szrj       size_t __n;
15838fd1498Szrj       return __str_codecvt_out(__first, __last, __outstr, __cvt, __state, __n);
15938fd1498Szrj     }
16038fd1498Szrj 
16138fd1498Szrj #ifdef _GLIBCXX_USE_WCHAR_T
16238fd1498Szrj 
16338fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_CXX11
16438fd1498Szrj 
16538fd1498Szrj   /// String conversions
16638fd1498Szrj   template<typename _Codecvt, typename _Elem = wchar_t,
16738fd1498Szrj 	   typename _Wide_alloc = allocator<_Elem>,
16838fd1498Szrj 	   typename _Byte_alloc = allocator<char>>
16938fd1498Szrj     class wstring_convert
17038fd1498Szrj     {
17138fd1498Szrj     public:
17238fd1498Szrj       typedef basic_string<char, char_traits<char>, _Byte_alloc>   byte_string;
17338fd1498Szrj       typedef basic_string<_Elem, char_traits<_Elem>, _Wide_alloc> wide_string;
17438fd1498Szrj       typedef typename _Codecvt::state_type 			   state_type;
17538fd1498Szrj       typedef typename wide_string::traits_type::int_type	   int_type;
17638fd1498Szrj 
17738fd1498Szrj       /** Default constructor.
17838fd1498Szrj        *
17938fd1498Szrj        * @param  __pcvt The facet to use for conversions.
18038fd1498Szrj        *
18138fd1498Szrj        * Takes ownership of @p __pcvt and will delete it in the destructor.
18238fd1498Szrj        */
18338fd1498Szrj       explicit
18438fd1498Szrj       wstring_convert(_Codecvt* __pcvt = new _Codecvt()) : _M_cvt(__pcvt)
18538fd1498Szrj       {
18638fd1498Szrj 	if (!_M_cvt)
18738fd1498Szrj 	  __throw_logic_error("wstring_convert");
18838fd1498Szrj       }
18938fd1498Szrj 
19038fd1498Szrj       /** Construct with an initial converstion state.
19138fd1498Szrj        *
19238fd1498Szrj        * @param  __pcvt The facet to use for conversions.
19338fd1498Szrj        * @param  __state Initial conversion state.
19438fd1498Szrj        *
19538fd1498Szrj        * Takes ownership of @p __pcvt and will delete it in the destructor.
19638fd1498Szrj        * The object's conversion state will persist between conversions.
19738fd1498Szrj        */
19838fd1498Szrj       wstring_convert(_Codecvt* __pcvt, state_type __state)
19938fd1498Szrj       : _M_cvt(__pcvt), _M_state(__state), _M_with_cvtstate(true)
20038fd1498Szrj       {
20138fd1498Szrj 	if (!_M_cvt)
20238fd1498Szrj 	  __throw_logic_error("wstring_convert");
20338fd1498Szrj       }
20438fd1498Szrj 
20538fd1498Szrj       /** Construct with error strings.
20638fd1498Szrj        *
20738fd1498Szrj        * @param  __byte_err A string to return on failed conversions.
20838fd1498Szrj        * @param  __wide_err A wide string to return on failed conversions.
20938fd1498Szrj        */
21038fd1498Szrj       explicit
21138fd1498Szrj       wstring_convert(const byte_string& __byte_err,
21238fd1498Szrj 		      const wide_string& __wide_err = wide_string())
21338fd1498Szrj       : _M_cvt(new _Codecvt),
21438fd1498Szrj 	_M_byte_err_string(__byte_err), _M_wide_err_string(__wide_err),
21538fd1498Szrj 	_M_with_strings(true)
21638fd1498Szrj       {
21738fd1498Szrj 	if (!_M_cvt)
21838fd1498Szrj 	  __throw_logic_error("wstring_convert");
21938fd1498Szrj       }
22038fd1498Szrj 
22138fd1498Szrj       ~wstring_convert() = default;
22238fd1498Szrj 
22338fd1498Szrj       // _GLIBCXX_RESOLVE_LIB_DEFECTS
22438fd1498Szrj       // 2176. Special members for wstring_convert and wbuffer_convert
22538fd1498Szrj       wstring_convert(const wstring_convert&) = delete;
22638fd1498Szrj       wstring_convert& operator=(const wstring_convert&) = delete;
22738fd1498Szrj 
22838fd1498Szrj       /// @{ Convert from bytes.
22938fd1498Szrj       wide_string
23038fd1498Szrj       from_bytes(char __byte)
23138fd1498Szrj       {
23238fd1498Szrj 	char __bytes[2] = { __byte };
23338fd1498Szrj 	return from_bytes(__bytes, __bytes+1);
23438fd1498Szrj       }
23538fd1498Szrj 
23638fd1498Szrj       wide_string
23738fd1498Szrj       from_bytes(const char* __ptr)
23838fd1498Szrj       { return from_bytes(__ptr, __ptr+char_traits<char>::length(__ptr)); }
23938fd1498Szrj 
24038fd1498Szrj       wide_string
24138fd1498Szrj       from_bytes(const byte_string& __str)
24238fd1498Szrj       {
24338fd1498Szrj 	auto __ptr = __str.data();
24438fd1498Szrj 	return from_bytes(__ptr, __ptr + __str.size());
24538fd1498Szrj       }
24638fd1498Szrj 
24738fd1498Szrj       wide_string
24838fd1498Szrj       from_bytes(const char* __first, const char* __last)
24938fd1498Szrj       {
25038fd1498Szrj 	if (!_M_with_cvtstate)
25138fd1498Szrj 	  _M_state = state_type();
25238fd1498Szrj 	wide_string __out{ _M_wide_err_string.get_allocator() };
25338fd1498Szrj 	if (__str_codecvt_in(__first, __last, __out, *_M_cvt, _M_state,
25438fd1498Szrj 			     _M_count))
25538fd1498Szrj 	  return __out;
25638fd1498Szrj 	if (_M_with_strings)
25738fd1498Szrj 	  return _M_wide_err_string;
25838fd1498Szrj 	__throw_range_error("wstring_convert::from_bytes");
25938fd1498Szrj       }
26038fd1498Szrj       /// @}
26138fd1498Szrj 
26238fd1498Szrj       /// @{ Convert to bytes.
26338fd1498Szrj       byte_string
26438fd1498Szrj       to_bytes(_Elem __wchar)
26538fd1498Szrj       {
26638fd1498Szrj 	_Elem __wchars[2] = { __wchar };
26738fd1498Szrj 	return to_bytes(__wchars, __wchars+1);
26838fd1498Szrj       }
26938fd1498Szrj 
27038fd1498Szrj       byte_string
27138fd1498Szrj       to_bytes(const _Elem* __ptr)
27238fd1498Szrj       {
27338fd1498Szrj 	return to_bytes(__ptr, __ptr+wide_string::traits_type::length(__ptr));
27438fd1498Szrj       }
27538fd1498Szrj 
27638fd1498Szrj       byte_string
27738fd1498Szrj       to_bytes(const wide_string& __wstr)
27838fd1498Szrj       {
27938fd1498Szrj 	auto __ptr = __wstr.data();
28038fd1498Szrj 	return to_bytes(__ptr, __ptr + __wstr.size());
28138fd1498Szrj       }
28238fd1498Szrj 
28338fd1498Szrj       byte_string
28438fd1498Szrj       to_bytes(const _Elem* __first, const _Elem* __last)
28538fd1498Szrj       {
28638fd1498Szrj 	if (!_M_with_cvtstate)
28738fd1498Szrj 	  _M_state = state_type();
28838fd1498Szrj 	byte_string __out{ _M_byte_err_string.get_allocator() };
28938fd1498Szrj 	if (__str_codecvt_out(__first, __last, __out, *_M_cvt, _M_state,
29038fd1498Szrj 			      _M_count))
29138fd1498Szrj 	  return __out;
29238fd1498Szrj 	if (_M_with_strings)
29338fd1498Szrj 	  return _M_byte_err_string;
29438fd1498Szrj 	__throw_range_error("wstring_convert::to_bytes");
29538fd1498Szrj       }
29638fd1498Szrj       /// @}
29738fd1498Szrj 
29838fd1498Szrj       // _GLIBCXX_RESOLVE_LIB_DEFECTS
29938fd1498Szrj       // 2174. wstring_convert::converted() should be noexcept
30038fd1498Szrj       /// The number of elements successfully converted in the last conversion.
30138fd1498Szrj       size_t converted() const noexcept { return _M_count; }
30238fd1498Szrj 
30338fd1498Szrj       /// The final conversion state of the last conversion.
30438fd1498Szrj       state_type state() const { return _M_state; }
30538fd1498Szrj 
30638fd1498Szrj     private:
30738fd1498Szrj       unique_ptr<_Codecvt>	_M_cvt;
30838fd1498Szrj       byte_string		_M_byte_err_string;
30938fd1498Szrj       wide_string		_M_wide_err_string;
31038fd1498Szrj       state_type		_M_state = state_type();
31138fd1498Szrj       size_t			_M_count = 0;
31238fd1498Szrj       bool			_M_with_cvtstate = false;
31338fd1498Szrj       bool			_M_with_strings = false;
31438fd1498Szrj     };
31538fd1498Szrj 
31638fd1498Szrj _GLIBCXX_END_NAMESPACE_CXX11
31738fd1498Szrj 
31838fd1498Szrj   /// Buffer conversions
31938fd1498Szrj   template<typename _Codecvt, typename _Elem = wchar_t,
32038fd1498Szrj 	   typename _Tr = char_traits<_Elem>>
32138fd1498Szrj     class wbuffer_convert : public basic_streambuf<_Elem, _Tr>
32238fd1498Szrj     {
32338fd1498Szrj       typedef basic_streambuf<_Elem, _Tr> _Wide_streambuf;
32438fd1498Szrj 
32538fd1498Szrj     public:
32638fd1498Szrj       typedef typename _Codecvt::state_type state_type;
32738fd1498Szrj 
32838fd1498Szrj       /** Default constructor.
32938fd1498Szrj        *
33038fd1498Szrj        * @param  __bytebuf The underlying byte stream buffer.
33138fd1498Szrj        * @param  __pcvt    The facet to use for conversions.
33238fd1498Szrj        * @param  __state   Initial conversion state.
33338fd1498Szrj        *
33438fd1498Szrj        * Takes ownership of @p __pcvt and will delete it in the destructor.
33538fd1498Szrj        */
33638fd1498Szrj       explicit
33738fd1498Szrj       wbuffer_convert(streambuf* __bytebuf = 0, _Codecvt* __pcvt = new _Codecvt,
33838fd1498Szrj 		      state_type __state = state_type())
33938fd1498Szrj       : _M_buf(__bytebuf), _M_cvt(__pcvt), _M_state(__state)
34038fd1498Szrj       {
34138fd1498Szrj 	if (!_M_cvt)
34238fd1498Szrj 	  __throw_logic_error("wbuffer_convert");
34338fd1498Szrj 
34438fd1498Szrj 	_M_always_noconv = _M_cvt->always_noconv();
34538fd1498Szrj 
34638fd1498Szrj 	if (_M_buf)
34738fd1498Szrj 	  {
34838fd1498Szrj 	    this->setp(_M_put_area, _M_put_area + _S_buffer_length);
34938fd1498Szrj 	    this->setg(_M_get_area + _S_putback_length,
35038fd1498Szrj 		       _M_get_area + _S_putback_length,
35138fd1498Szrj 		       _M_get_area + _S_putback_length);
35238fd1498Szrj 	  }
35338fd1498Szrj       }
35438fd1498Szrj 
35538fd1498Szrj       ~wbuffer_convert() = default;
35638fd1498Szrj 
35738fd1498Szrj       // _GLIBCXX_RESOLVE_LIB_DEFECTS
35838fd1498Szrj       // 2176. Special members for wstring_convert and wbuffer_convert
35938fd1498Szrj       wbuffer_convert(const wbuffer_convert&) = delete;
36038fd1498Szrj       wbuffer_convert& operator=(const wbuffer_convert&) = delete;
36138fd1498Szrj 
36238fd1498Szrj       streambuf* rdbuf() const noexcept { return _M_buf; }
36338fd1498Szrj 
36438fd1498Szrj       streambuf*
36538fd1498Szrj       rdbuf(streambuf *__bytebuf) noexcept
36638fd1498Szrj       {
36738fd1498Szrj 	auto __prev = _M_buf;
36838fd1498Szrj 	_M_buf = __bytebuf;
36938fd1498Szrj 	return __prev;
37038fd1498Szrj       }
37138fd1498Szrj 
37238fd1498Szrj       /// The conversion state following the last conversion.
37338fd1498Szrj       state_type state() const noexcept { return _M_state; }
37438fd1498Szrj 
37538fd1498Szrj     protected:
37638fd1498Szrj       int
37738fd1498Szrj       sync()
37838fd1498Szrj       { return _M_buf && _M_conv_put() && !_M_buf->pubsync() ? 0 : -1; }
37938fd1498Szrj 
38038fd1498Szrj       typename _Wide_streambuf::int_type
38138fd1498Szrj       overflow(typename _Wide_streambuf::int_type __out)
38238fd1498Szrj       {
38338fd1498Szrj 	if (!_M_buf || !_M_conv_put())
38438fd1498Szrj 	  return _Tr::eof();
38538fd1498Szrj 	else if (!_Tr::eq_int_type(__out, _Tr::eof()))
38638fd1498Szrj 	  return this->sputc(__out);
38738fd1498Szrj 	return _Tr::not_eof(__out);
38838fd1498Szrj       }
38938fd1498Szrj 
39038fd1498Szrj       typename _Wide_streambuf::int_type
39138fd1498Szrj       underflow()
39238fd1498Szrj       {
39338fd1498Szrj 	if (!_M_buf)
39438fd1498Szrj 	  return _Tr::eof();
39538fd1498Szrj 
39638fd1498Szrj 	if (this->gptr() < this->egptr() || (_M_buf && _M_conv_get()))
39738fd1498Szrj 	  return _Tr::to_int_type(*this->gptr());
39838fd1498Szrj 	else
39938fd1498Szrj 	  return _Tr::eof();
40038fd1498Szrj       }
40138fd1498Szrj 
40238fd1498Szrj       streamsize
40338fd1498Szrj       xsputn(const typename _Wide_streambuf::char_type* __s, streamsize __n)
40438fd1498Szrj       {
40538fd1498Szrj 	if (!_M_buf || __n == 0)
40638fd1498Szrj 	  return 0;
40738fd1498Szrj 	streamsize __done = 0;
40838fd1498Szrj 	do
40938fd1498Szrj 	{
41038fd1498Szrj 	  auto __nn = std::min<streamsize>(this->epptr() - this->pptr(),
41138fd1498Szrj 					   __n - __done);
41238fd1498Szrj 	  _Tr::copy(this->pptr(), __s + __done, __nn);
41338fd1498Szrj 	  this->pbump(__nn);
41438fd1498Szrj 	  __done += __nn;
41538fd1498Szrj 	} while (__done < __n && _M_conv_put());
41638fd1498Szrj 	return __done;
41738fd1498Szrj       }
41838fd1498Szrj 
41938fd1498Szrj     private:
42038fd1498Szrj       // fill the get area from converted contents of the byte stream buffer
42138fd1498Szrj       bool
42238fd1498Szrj       _M_conv_get()
42338fd1498Szrj       {
42438fd1498Szrj 	const streamsize __pb1 = this->gptr() - this->eback();
42538fd1498Szrj 	const streamsize __pb2 = _S_putback_length;
42638fd1498Szrj 	const streamsize __npb = std::min(__pb1, __pb2);
42738fd1498Szrj 
42838fd1498Szrj 	_Tr::move(_M_get_area + _S_putback_length - __npb,
42938fd1498Szrj 		  this->gptr() - __npb, __npb);
43038fd1498Szrj 
43138fd1498Szrj 	streamsize __nbytes = sizeof(_M_get_buf) - _M_unconv;
43238fd1498Szrj 	__nbytes = std::min(__nbytes, _M_buf->in_avail());
43338fd1498Szrj 	if (__nbytes < 1)
43438fd1498Szrj 	  __nbytes = 1;
43538fd1498Szrj 	__nbytes = _M_buf->sgetn(_M_get_buf + _M_unconv, __nbytes);
43638fd1498Szrj 	if (__nbytes < 1)
43738fd1498Szrj 	  return false;
43838fd1498Szrj 	__nbytes += _M_unconv;
43938fd1498Szrj 
44038fd1498Szrj 	// convert _M_get_buf into _M_get_area
44138fd1498Szrj 
44238fd1498Szrj 	_Elem* __outbuf = _M_get_area + _S_putback_length;
44338fd1498Szrj 	_Elem* __outnext = __outbuf;
44438fd1498Szrj 	const char* __bnext = _M_get_buf;
44538fd1498Szrj 
44638fd1498Szrj 	codecvt_base::result __result;
44738fd1498Szrj 	if (_M_always_noconv)
44838fd1498Szrj 	  __result = codecvt_base::noconv;
44938fd1498Szrj 	else
45038fd1498Szrj 	  {
45138fd1498Szrj 	    _Elem* __outend = _M_get_area + _S_buffer_length;
45238fd1498Szrj 
45338fd1498Szrj 	    __result = _M_cvt->in(_M_state,
45438fd1498Szrj 				  __bnext, __bnext + __nbytes, __bnext,
45538fd1498Szrj 				  __outbuf, __outend, __outnext);
45638fd1498Szrj 	  }
45738fd1498Szrj 
45838fd1498Szrj 	if (__result == codecvt_base::noconv)
45938fd1498Szrj 	  {
46038fd1498Szrj 	    // cast is safe because noconv means _Elem is same type as char
46138fd1498Szrj 	    auto __get_buf = reinterpret_cast<const _Elem*>(_M_get_buf);
46238fd1498Szrj 	    _Tr::copy(__outbuf, __get_buf, __nbytes);
46338fd1498Szrj 	    _M_unconv = 0;
46438fd1498Szrj 	    return true;
46538fd1498Szrj 	  }
46638fd1498Szrj 
46738fd1498Szrj 	if ((_M_unconv = _M_get_buf + __nbytes - __bnext))
46838fd1498Szrj 	  char_traits<char>::move(_M_get_buf, __bnext, _M_unconv);
46938fd1498Szrj 
47038fd1498Szrj 	this->setg(__outbuf, __outbuf, __outnext);
47138fd1498Szrj 
47238fd1498Szrj 	return __result != codecvt_base::error;
47338fd1498Szrj       }
47438fd1498Szrj 
47538fd1498Szrj       // unused
47638fd1498Szrj       bool
47738fd1498Szrj       _M_put(...)
47838fd1498Szrj       { return false; }
47938fd1498Szrj 
48038fd1498Szrj       bool
48138fd1498Szrj       _M_put(const char* __p, streamsize __n)
48238fd1498Szrj       {
48338fd1498Szrj 	if (_M_buf->sputn(__p, __n) < __n)
48438fd1498Szrj 	  return false;
48538fd1498Szrj 	return true;
48638fd1498Szrj       }
48738fd1498Szrj 
48838fd1498Szrj       // convert the put area and write to the byte stream buffer
48938fd1498Szrj       bool
49038fd1498Szrj       _M_conv_put()
49138fd1498Szrj       {
49238fd1498Szrj 	_Elem* const __first = this->pbase();
49338fd1498Szrj 	const _Elem* const __last = this->pptr();
49438fd1498Szrj 	const streamsize __pending = __last - __first;
49538fd1498Szrj 
49638fd1498Szrj 	if (_M_always_noconv)
49738fd1498Szrj 	  return _M_put(__first, __pending);
49838fd1498Szrj 
49938fd1498Szrj 	char __outbuf[2 * _S_buffer_length];
50038fd1498Szrj 
50138fd1498Szrj 	const _Elem* __next = __first;
50238fd1498Szrj 	const _Elem* __start;
50338fd1498Szrj 	do
50438fd1498Szrj 	  {
50538fd1498Szrj 	    __start = __next;
50638fd1498Szrj 	    char* __outnext = __outbuf;
50738fd1498Szrj 	    char* const __outlast = __outbuf + sizeof(__outbuf);
50838fd1498Szrj 	    auto __result = _M_cvt->out(_M_state, __next, __last, __next,
50938fd1498Szrj 					__outnext, __outlast, __outnext);
51038fd1498Szrj 	    if (__result == codecvt_base::error)
51138fd1498Szrj 	      return false;
51238fd1498Szrj 	    else if (__result == codecvt_base::noconv)
51338fd1498Szrj 	      return _M_put(__next, __pending);
51438fd1498Szrj 
51538fd1498Szrj 	    if (!_M_put(__outbuf, __outnext - __outbuf))
51638fd1498Szrj 	      return false;
51738fd1498Szrj 	  }
51838fd1498Szrj 	while (__next != __last && __next != __start);
51938fd1498Szrj 
52038fd1498Szrj 	if (__next != __last)
52138fd1498Szrj 	  _Tr::move(__first, __next, __last - __next);
52238fd1498Szrj 
52338fd1498Szrj 	this->pbump(__first - __next);
52438fd1498Szrj 	return __next != __first;
52538fd1498Szrj       }
52638fd1498Szrj 
52738fd1498Szrj       streambuf*		_M_buf;
52838fd1498Szrj       unique_ptr<_Codecvt>	_M_cvt;
52938fd1498Szrj       state_type		_M_state;
53038fd1498Szrj 
53138fd1498Szrj       static const streamsize	_S_buffer_length = 32;
53238fd1498Szrj       static const streamsize	_S_putback_length = 3;
53338fd1498Szrj       _Elem                     _M_put_area[_S_buffer_length];
53438fd1498Szrj       _Elem                     _M_get_area[_S_buffer_length];
53538fd1498Szrj       streamsize		_M_unconv = 0;
53638fd1498Szrj       char			_M_get_buf[_S_buffer_length-_S_putback_length];
53738fd1498Szrj       bool			_M_always_noconv;
53838fd1498Szrj     };
53938fd1498Szrj 
54038fd1498Szrj #endif  // _GLIBCXX_USE_WCHAR_T
54138fd1498Szrj 
54238fd1498Szrj   /// @} group locales
54338fd1498Szrj 
54438fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION
54538fd1498Szrj } // namespace
54638fd1498Szrj 
54738fd1498Szrj #endif // __cplusplus
54838fd1498Szrj 
54938fd1498Szrj #endif /* _LOCALE_CONV_H */
550