📄 xlocale
字号:
protected:
virtual __CLR_OR_THIS_CALL ~codecvt()
{ // destroy the object
}
void __CLR_OR_THIS_CALL _Init(const _Locinfo&)
{ // initialize
}
virtual result __CLR_OR_THIS_CALL do_in(_Statype& _State,
const _Byte *_First1, const _Byte *_Last1, const _Byte *& _Mid1,
_Elem *_First2, _Elem *_Last2, _Elem *& _Mid2) const
{ // convert bytes [_First1, _Last1) to [_First2, _Last)
char *_Pstate = (char *)&_State;
_Mid1 = _First1;
_Mid2 = _First2;
for (; _Mid1 != _Last1 && _Mid2 != _Last2; )
{ // convert a multibyte sequence
unsigned char _By = (unsigned char)*_Mid1;
unsigned long _Ch;
int _Nextra;
if (_By < 0x80)
_Ch = _By, _Nextra = 0;
else if (_By < 0xc0)
{ // 0x80-0xdf not first byte
++_Mid1;
return (_Mybase::error);
}
else if (_By < 0xe0)
_Ch = _By & 0x1f, _Nextra = 1;
else if (_By < 0xf0)
_Ch = _By & 0x0f, _Nextra = 2;
else if (_By < 0xf8)
_Ch = _By & 0x07, _Nextra = 3;
else
_Ch = _By & 0x03, _Nextra = _By < 0xfc ? 4 : 5;
if (_Nextra == 0)
++_Mid1;
else if (_Last1 - _Mid1 < _Nextra + 1)
break; // not enough input
else
for (++_Mid1; 0 < _Nextra; --_Nextra, ++_Mid1)
if ((_By = (unsigned char)*_Mid1) < 0x80 || 0xc0 <= _By)
return (_Mybase::error); // not continuation byte
else
_Ch = _Ch << 6 | _By & 0x3f;
if (*_Pstate == 0)
{ // first time, maybe look for and consume header
*_Pstate = 1;
if ((_Mode & _Consume_header) != 0 && _Ch == 0xfeff)
{ // drop header and retry
result _Ans = do_in(_State, _Mid1, _Last1, _Mid1,
_First2, _Last2, _Mid2);
if (_Ans == _Mybase::partial)
{ // roll back header determination
*_Pstate = 0;
_Mid1 = _First1;
}
return (_Ans);
}
}
if (_Maxcode < _Ch)
return (_Mybase::error); // code too large
*_Mid2++ = (_Elem)_Ch;
}
return (_First1 == _Mid1 ? _Mybase::partial : _Mybase::ok);
}
virtual result __CLR_OR_THIS_CALL do_out(_Statype& _State,
const _Elem *_First1, const _Elem *_Last1, const _Elem *& _Mid1,
_Byte *_First2, _Byte *_Last2, _Byte *& _Mid2) const
{ // convert [_First1, _Last1) to bytes [_First2, _Last)
char *_Pstate = (char *)&_State;
_Mid1 = _First1;
_Mid2 = _First2;
for (; _Mid1 != _Last1 && _Mid2 != _Last2; )
{ // convert and put a wide char
_Byte _By;
int _Nextra;
unsigned long _Ch = (unsigned long)*_Mid1;
if (_Maxcode < _Ch)
return (_Mybase::error);
if (_Ch < 0x0080)
_By = (_Byte)_Ch, _Nextra = 0;
else if (_Ch < 0x0800)
_By = (_Byte)(0xc0 | _Ch >> 6), _Nextra = 1;
else if (_Ch < 0x00010000)
_By = (_Byte)(0xe0 | _Ch >> 12), _Nextra = 2;
else if (_Ch < 0x00200000)
_By = (_Byte)(0xf0 | _Ch >> 18), _Nextra = 3;
else if (_Ch < 0x04000000)
_By = (_Byte)(0xf8 | _Ch >> 24), _Nextra = 4;
else
_By = (_Byte)(0xfc | _Ch >> 30 & 0x03), _Nextra = 5;
if (*_Pstate == 0)
{ // first time, maybe generate header
*_Pstate = 1;
if ((_Mode & _Generate_header) == 0)
;
else if (_Last2 - _Mid2 < 3 + 1 + _Nextra)
return (_Mybase::partial); // not enough room for both
else
{ // prepend header
*_Mid2++ = (_Byte)(unsigned char)0xef;
*_Mid2++ = (_Byte)(unsigned char)0xbb;
*_Mid2++ = (_Byte)(unsigned char)0xbf;
}
}
if (_Last2 - _Mid2 < 1 + _Nextra)
break; // not enough room for output
++_Mid1;
for (*_Mid2++ = _By; 0 < _Nextra; )
*_Mid2++ = (_Byte)(_Ch >> 6 * --_Nextra & 0x3f | 0x80);
}
return (_First1 == _Mid1 ? _Mybase::partial : _Mybase::ok);
}
virtual result __CLR_OR_THIS_CALL do_unshift(_Statype&,
_Byte *_First2, _Byte *, _Byte *& _Mid2) const
{ // generate bytes to return to default shift state
_Mid2 = _First2;
return (_Mybase::ok);
}
virtual int __CLR_OR_THIS_CALL do_length(const _Statype& _State, const _Byte *_First1,
const _Byte *_Last1, size_t _Count) const _THROW0()
{ // return min(_Count, converted length of bytes [_First1, _Last1))
int _Wchars = 0;
_Statype _Mystate = _State;
for (; (size_t)_Wchars < _Count && _First1 != _Last1; )
{ // convert another wide character
const _Byte *_Mid1;
_Elem *_Mid2;
_Elem _Ch;
switch (do_in(_Mystate, _First1, _Last1, _Mid1,
&_Ch, &_Ch + 1, _Mid2))
{ // test result of single wide-char conversion
case _Mybase::noconv:
return ((int)(_Wchars + (int)(_Last1 - _First1)));
case _Mybase::ok:
if (_Mid2 == &_Ch + 1)
++_Wchars; // replacement do_in might not convert one
_First1 = _Mid1;
break;
default:
return ((int)_Wchars); // error or partial
}
}
return ((int)_Wchars);
}
virtual bool __CLR_OR_THIS_CALL do_always_noconv() const _THROW0()
{ // return true if conversions never change input
return (false);
}
virtual int __CLR_OR_THIS_CALL do_max_length() const _THROW0()
{ // return maximum length required for a conversion
return ((_Mode & (_Consume_header | _Generate_header)) != 0
? 9 : 6);
}
virtual int __CLR_OR_THIS_CALL do_encoding() const _THROW0()
{ // return length of code sequence (from codecvt)
return ((_Mode & (_Consume_header | _Generate_header)) != 0
? -1 : 0); // -1 => state dependent, 0 => varying length
}
private:
unsigned long _Maxcode; // default: 0xffffffff
_Codecvt_mode _Mode; // default: _Consume_header
};
#endif /* _HAS_CHAR16_T_LANGUAGE_SUPPORT */
// CLASS codecvt<wchar_t, char, _Mbstatet>
template<>
class _CRTIMP2_PURE codecvt<wchar_t, char, _Mbstatet>
: public codecvt_base
{ // facet for converting between wchar_t and char (_Byte) sequences
public:
typedef wchar_t _Elem;
typedef char _Byte;
typedef _Mbstatet _Statype;
typedef _Elem intern_type;
typedef _Byte extern_type;
typedef _Statype state_type;
result __CLR_OR_THIS_CALL in(_Statype& _State,
const _Byte *_First1, const _Byte *_Last1, const _Byte *& _Mid1,
_Elem *_First2, _Elem *_Last2, _Elem *& _Mid2) const
{ // convert bytes [_First1, _Last1) to [_First2, _Last)
return (do_in(_State,
_First1, _Last1, _Mid1, _First2, _Last2, _Mid2));
}
result __CLR_OR_THIS_CALL out(_Statype& _State,
const _Elem *_First1, const _Elem *_Last1, const _Elem *& _Mid1,
_Byte *_First2, _Byte *_Last2, _Byte *& _Mid2) const
{ // convert [_First1, _Last1) to bytes [_First2, _Last)
return (do_out(_State,
_First1, _Last1, _Mid1, _First2, _Last2, _Mid2));
}
result __CLR_OR_THIS_CALL unshift(_Statype& _State,
_Byte *_First2, _Byte *_Last2, _Byte *& _Mid2) const
{ // generate bytes to return to default shift state
return (do_unshift(_State,
_First2, _Last2, _Mid2));
}
int __CLR_OR_THIS_CALL length(const _Statype& _State, const _Byte *_First1,
const _Byte *_Last1, size_t _Count) const
{ // return min(_Count, converted length of bytes [_First1, _Last1))
return (do_length(_State, _First1, _Last1, _Count));
}
__PURE_APPDOMAIN_GLOBAL static locale::id id;
explicit __CLR_OR_THIS_CALL codecvt(size_t _Refs = 0)
: codecvt_base(_Refs)
{ // construct from current locale
_BEGIN_LOCINFO(_Lobj)
_Init(_Lobj);
_END_LOCINFO()
}
__CLR_OR_THIS_CALL codecvt(const _Locinfo& _Lobj, size_t _Refs = 0)
: codecvt_base(_Refs)
{ // construct from specified locale
_Init(_Lobj);
}
static size_t __CLRCALL_OR_CDECL _Getcat(const locale::facet **_Ppf = 0,
const locale *_Ploc = 0)
{ // return locale category mask and construct standard facet
if (_Ppf != 0 && *_Ppf == 0)
*_Ppf = _NEW_CRT codecvt<_Elem, _Byte, _Statype>(
_Locinfo(_Ploc->c_str()));
return (_X_CTYPE);
}
protected:
virtual __CLR_OR_THIS_CALL ~codecvt()
{ // destroy the object
}
void __CLR_OR_THIS_CALL _Init(const _Locinfo& _Lobj)
{ // initialize from _Lobj
_Cvt = _Lobj._Getcvt();
}
virtual result __CLR_OR_THIS_CALL do_in(_Statype& _State,
const _Byte *_First1, const _Byte *_Last1, const _Byte *& _Mid1,
_Elem *_First2, _Elem *_Last2, _Elem *& _Mid2) const
{ // convert bytes [_First1, _Last1) to [_First2, _Last)
_DEBUG_RANGE(_First1, _Last1);
_DEBUG_RANGE(_First2, _Last2);
_Mid1 = _First1, _Mid2 = _First2;
result _Ans = _Mid1 == _Last1 ? ok : partial;
int _Bytes;
while (_Mid1 != _Last1 && _Mid2 != _Last2)
switch (_Bytes = _Mbrtowc(_Mid2, _Mid1, _Last1 - _Mid1,
&_State, &_Cvt))
{ // test result of locale-specific mbrtowc call
case -2: // partial conversion
_Mid1 = _Last1;
return (_Ans);
case -1: // failed conversion
return (error);
case 0: // may have converted null character
if (*_Mid2 == (_Elem)0)
_Bytes = (int)_CSTD strlen(_Mid1) + 1;
// fall through
default: // converted _Bytes bytes to a wchar_t
if (_Bytes == -3)
_Bytes = 0; // wchar_t generated from state info
_Mid1 += _Bytes;
++_Mid2;
_Ans = ok;
}
return (_Ans);
}
virtual result __CLR_OR_THIS_CALL do_out(_Statype& _State,
const _Elem *_First1, const _Elem *_Last1, const _Elem *& _Mid1,
_Byte *_First2, _Byte *_Last2, _Byte *& _Mid2) const
{ // convert [_First1, _Last1) to bytes [_First2, _Last)
_DEBUG_RANGE(_First1, _Last1);
_DEBUG_RANGE(_First2, _Last2);
_Mid1 = _First1, _Mid2 = _First2;
result _Ans = _Mid1 == _Last1 ? ok : partial;
int _Bytes;
while (_Mid1 != _Last1 && _Mid2 != _Last2)
if ((int)MB_CUR_MAX <= _Last2 - _Mid2)
if ((_Bytes = _Wcrtomb(_Mid2, *_Mid1,
&_State, &_Cvt)) < 0)
return (error); // locale-specific wcrtomb failed
else
++_Mid1, _Mid2 += _Bytes, _Ans = ok;
else
{ // destination too small, convert into buffer
_Byte _Buf[MB_LEN_MAX];
_Statype _Stsave = _State;
if ((_Bytes = _Wcrtomb(_Buf, *_Mid1,
&_State, &_Cvt)) < 0)
return (error); // locale-specific wcrtomb failed
else if (_Last2 - _Mid2 < _Bytes)
{ // converted too many, roll back and return previous
_State = _Stsave;
return (_Ans);
}
else
{ // copy converted bytes from buffer
_CSTD memcpy(_Mid2, _Buf, _Bytes);
++_Mid1, _Mid2 += _Bytes, _Ans = ok;
}
}
return (_Ans);
}
virtual result __CLR_OR_THIS_CALL do_unshift(_Statype& _State,
_Byte *_First2, _Byte *_Last2, _Byte *& _Mid2) const
{ // generate bytes to return to default shift state
_DEBUG_RANGE(_First2, _Last2);
_Mid2 = _First2;
result _Ans = ok;
int _Bytes;
_Byte _Buf[MB_LEN_MAX];
_Statype _Stsave = _State;
if ((_Bytes = _Wcrtomb(_Buf, L'\0', &_State, &_Cvt)) <= 0)
_Ans = error; // locale-specific wcrtomb failed
else if (_Last2 - _Mid2 < --_Bytes)
{ // converted too many, roll back and return
_State = _Stsave;
_Ans = partial;
}
else if (0 < _Bytes)
{ // copy converted bytes from buffer
_CSTD memcpy(_Mid2, _Buf, _Bytes);
_Mid2 += _Bytes;
}
return (_Ans);
}
virtual int __CLR_OR_THIS_CALL do_length(const _Statype& _State, const _Byte *_First1,
const _Byte *_Last1, size_t _Count) const
{ // return min(_Count, converted length of bytes [_First1, _Last1))
#if _HAS_STRICT_CONFORMANCE
return ((int)(_Count < (size_t)(_Last1 - _First1)
? _Count : _Last1 - _First1)); // assume 1-to-1 conversion
#else /* _HAS_STRICT_CONFORMANCE */
_DEBUG_RANGE(_First1, _Last1);
int _Wchars;
const _Byte *_Mid1;
_Statype _Mystate = _State;
for (_Wchars = 0, _Mid1 = _First1;
(size_t)_Wchars < _Count && _Mid1 != _Last1; )
{ // convert another wchar_t
int _Bytes;
_Elem _Ch;
switch (_Bytes = _Mbrtowc(&_Ch, _Mid1, _Last1 - _Mid1,
&_Mystate, &_Cvt))
{ // test result of locale-specific mbrtowc call
case -2: // partial conversion
return (_Wchars);
case -1: // failed conversion
return (_Wchars);
case 0: // may have converted null character
if (_Ch == (_Elem)0)
_Bytes = (int)_CSTD strlen(_Mid1) + 1;
// fall through
default: // converted _Bytes bytes to a wchar_t
if (_Bytes == -3)
_Bytes = 0; // wchar_t generated from state info
_Mid1 += _Bytes;
++_Wchars;
}
}
return (_Wchars);
#endif /* _HAS_STRICT_CONFORMANCE */
}
virtual bool __CLR_OR_THIS_CALL do_always_noconv() const _THROW0()
{ // return true if conversions never change input
return (false);
}
virtual int __CLR_OR_THIS_CALL do_max_length() const _THROW0()
{ // return maximum length required for a conversion (from codecvt)
return (MB_LEN_MAX);
}
private:
_Locinfo::_Cvtvec _Cvt; // locale info passed to _Mbrtowc, _Wcrtomb
};
#ifdef _NATIVE_WCHAR_T_DEFINED
// CLASS codecvt<unsigned short, char, _Mbstatet>
template<>
class _CRTIMP2_PURE codecvt<unsigned short, char, _Mbstatet>
: public codecvt_base
{ // facet for converting between unsigned short and char sequences
public:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -