Commit a5834d1b by Benjamin Kosnik Committed by Benjamin Kosnik

codecvt.h: Implement codecvt<wchar_t, char, mbstate_t>.


2000-08-21  Benjamin Kosnik  <bkoz@purist.soma.redhat.com>

	* bits/codecvt.h: Implement codecvt<wchar_t, char, mbstate_t>. Fix
	up __enc_traits template so as to be marginally useful.
	* src/codecvt.cc: And here.
	* bits/char_traits: Tweak.
	* bits/locale_facets.h: Tweak.
	* bits/locale_facets.tcc: Tweak.
	* bits/localefwd.h: Tweak.
	* src/locale-inst.cc: Add use_facet/has_facet instantiations here.
	* testsuite/22_locale/codecvt_wchar_t_cc.cc: New file.
	* testsuite/22_locale/codecvt_char_char.cc: New file.
	* testsuite/22_locale/codecvt_unicode_char.cc: New file.

From-SVN: r35870
parent 199acebb
2000-08-21 Benjamin Kosnik <bkoz@purist.soma.redhat.com>
* bits/codecvt.h: Implement codecvt<wchar_t, char, mbstate_t>. Fix
up __enc_traits template so as to be marginally useful.
* src/codecvt.cc: And here.
* bits/char_traits: Tweak.
* bits/locale_facets.h: Tweak.
* bits/locale_facets.tcc: Tweak.
* bits/localefwd.h: Tweak.
* src/locale-inst.cc: Add use_facet/has_facet instantiations here.
* testsuite/22_locale/codecvt_wchar_t_cc.cc: New file.
* testsuite/22_locale/codecvt_char_char.cc: New file.
* testsuite/22_locale/codecvt_unicode_char.cc: New file.
2000-08-21 Gabriel Dos Reis <gdr@merlin.codesourcery.com>
* bits/std_cmath.h (std::abs): Overload for int and long.
......
......@@ -82,8 +82,8 @@ namespace std {
compare(const char_type* __s1, const char_type* __s2, size_t __n)
{
for (size_t __i = 0; __i < __n; ++__i)
if (!eq(__s1[__i],__s2[__i]))
return lt(__s1[__i],__s2[__i]) ? -1 : 1;
if (!eq(__s1[__i], __s2[__i]))
return lt(__s1[__i], __s2[__i]) ? -1 : 1;
return 0;
}
......
......@@ -389,7 +389,7 @@ namespace std
template<>
const ctype<char>&
use_facet<const ctype<char> > (const locale& __loc);
use_facet<const ctype<char> >(const locale& __loc);
#ifdef _GLIBCPP_USE_WCHAR_T
// ctype<wchar_t> specialization
......@@ -455,7 +455,7 @@ namespace std
template<>
const ctype<wchar_t>&
use_facet< const ctype<wchar_t> > (const locale& __loc);
use_facet< const ctype<wchar_t> >(const locale& __loc);
#endif //_GLIBCPP_USE_WCHAR_T
// Include host-specific ctype specializations.
......@@ -1615,66 +1615,66 @@ namespace std
template<typename _CharT>
inline bool
isspace(_CharT __c, const locale& __loc)
{ return use_facet<ctype<_CharT> > (__loc).is(ctype_base::space, __c); }
{ return use_facet<ctype<_CharT> >(__loc).is(ctype_base::space, __c); }
template<typename _CharT>
inline bool
isprint(_CharT __c, const locale& __loc)
{ return use_facet<ctype<_CharT> > (__loc).is(ctype_base::print, __c); }
{ return use_facet<ctype<_CharT> >(__loc).is(ctype_base::print, __c); }
template<typename _CharT>
inline bool
iscntrl(_CharT __c, const locale& __loc)
{ return use_facet<ctype<_CharT> > (__loc).is(ctype_base::cntrl, __c); }
{ return use_facet<ctype<_CharT> >(__loc).is(ctype_base::cntrl, __c); }
template<typename _CharT>
inline bool
isupper(_CharT __c, const locale& __loc)
{ return use_facet<ctype<_CharT> > (__loc).is(ctype_base::upper, __c); }
{ return use_facet<ctype<_CharT> >(__loc).is(ctype_base::upper, __c); }
template<typename _CharT>
inline bool islower(_CharT __c, const locale& __loc)
{ return use_facet<ctype<_CharT> > (__loc).is(ctype_base::lower, __c); }
{ return use_facet<ctype<_CharT> >(__loc).is(ctype_base::lower, __c); }
template<typename _CharT>
inline bool
isalpha(_CharT __c, const locale& __loc)
{ return use_facet<ctype<_CharT> > (__loc).is(ctype_base::alpha, __c); }
{ return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alpha, __c); }
template<typename _CharT>
inline bool
isdigit(_CharT __c, const locale& __loc)
{ return use_facet<ctype<_CharT> > (__loc).is(ctype_base::digit, __c); }
{ return use_facet<ctype<_CharT> >(__loc).is(ctype_base::digit, __c); }
template<typename _CharT>
inline bool
ispunct(_CharT __c, const locale& __loc)
{ return use_facet<ctype<_CharT> > (__loc).is(ctype_base::punct, __c); }
{ return use_facet<ctype<_CharT> >(__loc).is(ctype_base::punct, __c); }
template<typename _CharT>
inline bool
isxdigit(_CharT __c, const locale& __loc)
{ return use_facet<ctype<_CharT> > (__loc).is(ctype_base::xdigit, __c); }
{ return use_facet<ctype<_CharT> >(__loc).is(ctype_base::xdigit, __c); }
template<typename _CharT>
inline bool
isalnum(_CharT __c, const locale& __loc)
{ return use_facet<ctype<_CharT> > (__loc).is(ctype_base::alnum, __c); }
{ return use_facet<ctype<_CharT> >(__loc).is(ctype_base::alnum, __c); }
template<typename _CharT>
inline bool
isgraph(_CharT __c, const locale& __loc)
{ return use_facet<ctype<_CharT> > (__loc).is(ctype_base::graph, __c); }
{ return use_facet<ctype<_CharT> >(__loc).is(ctype_base::graph, __c); }
template<typename _CharT>
inline _CharT
toupper(_CharT __c, const locale& __loc)
{ return use_facet<ctype<_CharT> > (__loc).toupper(__c); }
{ return use_facet<ctype<_CharT> >(__loc).toupper(__c); }
template<typename _CharT>
inline _CharT
tolower(_CharT __c, const locale& __loc)
{ return use_facet<ctype<_CharT> > (__loc).tolower(__c); }
{ return use_facet<ctype<_CharT> >(__loc).tolower(__c); }
} // namespace std
......
......@@ -35,10 +35,10 @@
#include <bits/std_cerrno.h>
#include <bits/std_cstdlib.h> // For strof, strtold
#include <bits/std_limits.h> // For numeric_limits
#include <bits/std_vector.h>
#include <bits/std_memory.h> // For auto_ptr
#include <bits/sbuf_iter.h> // For streambuf_iterators
#include <bits/std_cctype.h> // For isspace
#include <bits/std_vector.h>
namespace std
{
......@@ -69,12 +69,12 @@ namespace std
const _Facet&
use_facet(const locale& __loc)
{
typedef locale::_Impl::__vec_facet __vec_facet;
const locale::facet* __fp = (const _Facet*)0; // check derivation
locale::id& __id = _Facet::id; // check member id
size_t __i = __id._M_index;
const locale::_Impl* __tmp = __loc._M_impl;
if (__id._M_index >= __loc._M_impl->_M_facets->size()
|| (__fp = (*(__tmp->_M_facets))[__i]) == 0)
__vec_facet* __facet = __loc._M_impl->_M_facets;
if (__i >= __facet->size() || (__fp = (*(__facet))[__i]) == 0)
return _Use_facet_failure_handler<_Facet>(__loc);
return static_cast<const _Facet&>(*__fp);
}
......@@ -86,8 +86,8 @@ namespace std
typedef locale::_Impl::__vec_facet __vec_facet;
locale::id& __id = _Facet::id; // check member id
size_t __i = __id._M_index;
__vec_facet* __tmpv = __loc._M_impl->_M_facets;
return (__i < __tmpv->size() && (*__tmpv)[__i] != 0);
__vec_facet* __facet = __loc._M_impl->_M_facets;
return (__i < __facet->size() && (*__facet)[__i] != 0);
}
// __match_parallel
......@@ -405,7 +405,6 @@ namespace std
// We now seek "units", i.e. digits and thousands separators.
// We may need to know if anything is found here. A leading zero
// (removed by now) would count.
bool __testunits = __testzero;
while (__valid && __beg != __end)
{
......
......@@ -67,14 +67,6 @@ namespace std
template<typename _Tp, typename _Alloc> class vector;
class locale;
template<typename _Facet>
const _Facet&
use_facet(const locale&);
template<typename _Facet>
bool
has_facet(const locale&) throw();
// 22.1.3 Convenience interfaces
template<typename _CharT>
inline bool
......@@ -289,10 +281,14 @@ namespace std
classic();
private:
_Impl* _M_impl; // The (shared) implementation
// The (shared) implementation
_Impl* _M_impl;
static _Impl* _S_classic; // The one true C reference locale
static _Impl* _S_global; // Current global reference locale
// The one true C reference locale
static _Impl* _S_classic;
// Current global reference locale
static _Impl* _S_global;
explicit
locale(_Impl*) throw();
......@@ -312,10 +308,11 @@ namespace std
// locale implementation object
class locale::_Impl
{
// Types.
typedef vector<facet*, allocator<facet*> > __vec_facet;
typedef vector<string, allocator<string> > __vec_string;
// Friends:
// Friends.
friend class locale;
friend class facet;
......@@ -327,6 +324,7 @@ namespace std
friend bool
has_facet(const locale&) throw();
// Data Members.
size_t _M_num_references;
__vec_facet* _M_facets;
__vec_string* _M_category_names;
......@@ -467,11 +465,13 @@ namespace std
public:
id() {};
private:
// NB: there is no accessor for _M_index because it may be used
// NB: There is no accessor for _M_index because it may be used
// before the constructor is run; the effect of calling a member
// function (even an inline) would be undefined.
mutable size_t _M_index;
static size_t _S_highwater; // last id number assigned
// Last id number assigned
static size_t _S_highwater;
void
operator=(const id&); // not defined
......
......@@ -31,6 +31,7 @@
namespace std {
// codecvt<char, char, mbstate_t> required specialization
locale::id codecvt<char, char, mbstate_t>::id;
codecvt<char, char, mbstate_t>::
......@@ -48,10 +49,8 @@ namespace std {
extern_type* __to, extern_type* __to_end,
extern_type*& __to_next) const
{
size_t __sizefrom = __from_end - __from;
size_t __sizeto = __to_end - __to;
size_t __length = __sizefrom <= __sizeto ? __sizefrom : __sizeto;
memcpy(__to, __from, __length);
size_t __len = min(__from_end - __from, __to_end - __to);
memcpy(__to, __from, __len);
__from_next = __from;
__to_next = __to;
return noconv;
......@@ -60,7 +59,7 @@ namespace std {
codecvt_base::result
codecvt<char, char, mbstate_t>::
do_unshift(state_type& /*__state*/, extern_type* __to,
extern_type* /*__to_limit*/, extern_type*& __to_next) const
extern_type* /*__to_end*/, extern_type*& __to_next) const
{
__to_next = __to;
return noconv;
......@@ -73,10 +72,8 @@ namespace std {
intern_type* __to, intern_type* __to_end,
intern_type*& __to_next) const
{
size_t __sizefrom = __from_end - __from;
size_t __sizeto = __to_end - __to;
size_t __length = __sizefrom <= __sizeto ? __sizefrom : __sizeto;
memcpy(__to, __from, __length);
size_t __len = min(__from_end - __from, __to_end - __to);
memcpy(__to, __from, __len);
__from_next = __from;
__to_next = __to;
return noconv;
......@@ -84,21 +81,24 @@ namespace std {
int
codecvt<char, char, mbstate_t>::
do_encoding() const throw() { return 1; }
do_encoding() const throw()
{ return 1; }
bool
codecvt<char, char, mbstate_t>::
do_always_noconv() const throw() { return true; }
do_always_noconv() const throw()
{ return true; }
int
codecvt<char, char, mbstate_t>::
do_length (const state_type& /*__state*/, const extern_type* __from,
const extern_type* __end, size_t __max) const
{ return (__max < size_t(__end - __from)) ? __max : __end - __from; }
{ return min(__max, static_cast<size_t>(__end - __from)); }
int
codecvt<char, char, mbstate_t>::
do_max_length() const throw() { return 1; }
do_max_length() const throw()
{ return 1; }
codecvt_byname<char, char, mbstate_t>::
codecvt_byname(const char* /*__s*/, size_t __refs)
......@@ -108,6 +108,7 @@ namespace std {
~codecvt_byname() { }
#ifdef _GLIBCPP_USE_WCHAR_T
// codecvt<wchar_t, char, mbstate_t> required specialization
locale::id codecvt<wchar_t, char, mbstate_t>::id;
codecvt<wchar_t, char, mbstate_t>::
......@@ -119,21 +120,37 @@ namespace std {
codecvt_base::result
codecvt<wchar_t, char, mbstate_t>::
do_out(state_type& /*__state*/, const intern_type* __from,
do_out(state_type& __state, const intern_type* __from,
const intern_type* __from_end, const intern_type*& __from_next,
extern_type* __to, extern_type* __to_limit,
extern_type* __to, extern_type* __to_end,
extern_type*& __to_next) const
{
for (; __from < __from_end && __to < __to_limit; ++__from, ++__to)
*__to = static_cast<char>(*__from);
__from_next = __from; __to_next = __to;
return __from == __from_end ? ok : partial;
result __ret = error;
size_t __len = min(__from_end - __from, __to_end - __to);
size_t __conv = wcsrtombs(__to, &__from, __len, &__state);
if (__conv == __len)
{
__from_next = __from;
__to_next = __to + __conv;
__ret = ok;
}
else if (__conv > 0 && __conv < __len)
{
__from_next = __from;
__to_next = __to + __conv;
__ret = partial;
}
else
__ret = error;
return __ret;
}
codecvt_base::result
codecvt<wchar_t, char, mbstate_t>::
do_unshift (state_type& /*__state*/, extern_type* __to,
extern_type* /*__to_limit*/, extern_type*& __to_next) const
do_unshift(state_type& /*__state*/, extern_type* __to,
extern_type* /*__to_end*/, extern_type*& __to_next) const
{
__to_next = __to;
return noconv;
......@@ -141,22 +158,37 @@ namespace std {
codecvt_base::result
codecvt<wchar_t, char, mbstate_t>::
do_in(state_type& /*__state*/, const extern_type* __from,
do_in(state_type& __state, const extern_type* __from,
const extern_type* __from_end, const extern_type*& __from_next,
intern_type* __to, intern_type* __to_limit,
intern_type* __to, intern_type* __to_end,
intern_type*& __to_next) const
{
for (; __from < __from_end && __to < __to_limit; ++__from, ++__to)
*__to = static_cast<wchar_t>(*__from);
result __ret = error;
size_t __len = min(__from_end - __from, __to_end - __to);
size_t __conv = mbsrtowcs(__to, &__from, __len, &__state);
if (__conv == __len)
{
__from_next = __from;
__to_next = __to;
return __from == __from_end ? ok : partial;
__to_next = __to + __conv;
__ret = ok;
}
else if (__conv > 0 && __conv < __len)
{
__from_next = __from;
__to_next = __to + __conv;
__ret = partial;
}
else
__ret = error;
return __ret;
}
int
codecvt<wchar_t, char, mbstate_t>::
do_encoding() const throw()
{ return 1; }
{ return 0; }
bool
codecvt<wchar_t, char, mbstate_t>::
......@@ -167,10 +199,11 @@ namespace std {
codecvt<wchar_t, char, mbstate_t>::
do_length(const state_type& /*__state*/, const extern_type* __from,
const extern_type* __end, size_t __max) const
{ return (__max < size_t(__end - __from)) ? __max : __end - __from; }
{ return min(__max, static_cast<size_t>(__end - __from)); }
int
codecvt<wchar_t, char, mbstate_t>::do_max_length() const throw()
codecvt<wchar_t, char, mbstate_t>::
do_max_length() const throw()
{ return 1; }
codecvt_byname<wchar_t, char, mbstate_t>::
......
......@@ -135,6 +135,15 @@ namespace std {
// codecvt
template class __codecvt_abstract_base<char, char, mbstate_t>;
template class __codecvt_abstract_base<wchar_t, char, mbstate_t>;
#ifdef _GLIBCPP_USE_WCHAR_T
typedef unsigned short unicode_t;
template
const codecvt<unicode_t, char, __enc_traits>&
use_facet<codecvt<unicode_t, char, __enc_traits> >(const locale&);
template
bool
has_facet<codecvt<unicode_t, char, __enc_traits> >(const locale &);
#endif
// collate
template class _Collate<char>;
......@@ -160,7 +169,7 @@ namespace std {
use_facet<ctype<char> >(const locale& __loc);
template
const codecvt<char, char, mbstate_t>&
use_facet<codecvt<char, char, mbstate_t> >(locale const &);
use_facet<codecvt<char, char, mbstate_t> >(const locale&);
template
const num_put<char, obuf_iterator>&
_Use_facet_failure_handler<num_put<char, obuf_iterator> >
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment