strconv.cpp

来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 2,176 行 · 第 1/5 页

CPP
2,176
字号
        }
        len += sizeof(wxUint16);
        psz++;
    }
    if (buf && len<=n-sizeof(wxUint16))   *(wxUint16*)buf=0;

    return len;
}


// swap 16bit MB to 16bit String
size_t wxMBConvUTF16swap::MB2WC(wchar_t *buf, const char *psz, size_t n) const
{
    size_t len=0;

    while (*(wxUint16*)psz && (!buf || len < n))
    {
        if (buf)
        {
            ((char *)buf)[0] = psz[1];
            ((char *)buf)[1] = psz[0];
            buf++;
        }
        len++;
        psz += sizeof(wxUint16);
    }
    if (buf && len<n)   *buf=0;

    return len;
}


// swap 16bit MB to 16bit String
size_t wxMBConvUTF16swap::WC2MB(char *buf, const wchar_t *psz, size_t n) const
{
    size_t len=0;

    while (*psz && (!buf || len < n))
    {
        if (buf)
        {
            *buf++ = ((char*)psz)[1];
            *buf++ = ((char*)psz)[0];
        }
        len += sizeof(wxUint16);
        psz++;
    }
    if (buf && len<=n-sizeof(wxUint16))   *(wxUint16*)buf=0;

    return len;
}


#else // WC_UTF16


// copy 16bit MB to 32bit String
size_t wxMBConvUTF16straight::MB2WC(wchar_t *buf, const char *psz, size_t n) const
{
    size_t len=0;

    while (*(wxUint16*)psz && (!buf || len < n))
    {
        wxUint32 cc;
        size_t pa=decode_utf16((wxUint16*)psz, cc);
        if (pa == (size_t)-1)
            return pa;

        if (buf)
            *buf++ = cc;
        len++;
        psz += pa * sizeof(wxUint16);
    }
    if (buf && len<n)   *buf=0;

    return len;
}


// copy 32bit String to 16bit MB
size_t wxMBConvUTF16straight::WC2MB(char *buf, const wchar_t *psz, size_t n) const
{
    size_t len=0;

    while (*psz && (!buf || len < n))
    {
        wxUint16 cc[2];
        size_t pa=encode_utf16(*psz, cc);

        if (pa == (size_t)-1)
            return pa;

        if (buf)
        {
            *(wxUint16*)buf = cc[0];
            buf += sizeof(wxUint16);
            if (pa > 1)
            {
                *(wxUint16*)buf = cc[1];
                buf += sizeof(wxUint16);
            }
        }

        len += pa*sizeof(wxUint16);
        psz++;
    }
    if (buf && len<=n-sizeof(wxUint16))   *(wxUint16*)buf=0;

    return len;
}


// swap 16bit MB to 32bit String
size_t wxMBConvUTF16swap::MB2WC(wchar_t *buf, const char *psz, size_t n) const
{
    size_t len=0;

    while (*(wxUint16*)psz && (!buf || len < n))
    {
        wxUint32 cc;
        char tmp[4];
        tmp[0]=psz[1];  tmp[1]=psz[0];
        tmp[2]=psz[3];  tmp[3]=psz[2];

        size_t pa=decode_utf16((wxUint16*)tmp, cc);
        if (pa == (size_t)-1)
            return pa;

        if (buf)
            *buf++ = cc;

        len++;
        psz += pa * sizeof(wxUint16);
    }
    if (buf && len<n)   *buf=0;

    return len;
}


// swap 32bit String to 16bit MB
size_t wxMBConvUTF16swap::WC2MB(char *buf, const wchar_t *psz, size_t n) const
{
    size_t len=0;

    while (*psz && (!buf || len < n))
    {
        wxUint16 cc[2];
        size_t pa=encode_utf16(*psz, cc);

        if (pa == (size_t)-1)
            return pa;

        if (buf)
        {
            *buf++ = ((char*)cc)[1];
            *buf++ = ((char*)cc)[0];
            if (pa > 1)
            {
                *buf++ = ((char*)cc)[3];
                *buf++ = ((char*)cc)[2];
            }
        }

        len += pa*sizeof(wxUint16);
        psz++;
    }
    if (buf && len<=n-sizeof(wxUint16))   *(wxUint16*)buf=0;

    return len;
}

#endif // WC_UTF16


// ----------------------------------------------------------------------------
// UTF-32
// ----------------------------------------------------------------------------

#ifdef WORDS_BIGENDIAN
#define wxMBConvUTF32straight  wxMBConvUTF32BE
#define wxMBConvUTF32swap      wxMBConvUTF32LE
#else
#define wxMBConvUTF32swap      wxMBConvUTF32BE
#define wxMBConvUTF32straight  wxMBConvUTF32LE
#endif


WXDLLIMPEXP_DATA_BASE(wxMBConvUTF32LE) wxConvUTF32LE;
WXDLLIMPEXP_DATA_BASE(wxMBConvUTF32BE) wxConvUTF32BE;


#ifdef WC_UTF16

// copy 32bit MB to 16bit String
size_t wxMBConvUTF32straight::MB2WC(wchar_t *buf, const char *psz, size_t n) const
{
    size_t len=0;

    while (*(wxUint32*)psz && (!buf || len < n))
    {
        wxUint16 cc[2];

        size_t pa=encode_utf16(*(wxUint32*)psz, cc);
        if (pa == (size_t)-1)
            return pa;

        if (buf)
        {
            *buf++ = cc[0];
            if (pa > 1)
                *buf++ = cc[1];
        }
        len += pa;
        psz += sizeof(wxUint32);
    }
    if (buf && len<n)   *buf=0;

    return len;
}


// copy 16bit String to 32bit MB
size_t wxMBConvUTF32straight::WC2MB(char *buf, const wchar_t *psz, size_t n) const
{
    size_t len=0;

    while (*psz && (!buf || len < n))
    {
        wxUint32 cc;

        // cast is ok for WC_UTF16
        size_t pa = decode_utf16((const wxUint16 *)psz, cc);
        if (pa == (size_t)-1)
            return pa;

        if (buf)
        {
            *(wxUint32*)buf = cc;
            buf += sizeof(wxUint32);
        }
        len += sizeof(wxUint32);
        psz += pa;
    }

    if (buf && len<=n-sizeof(wxUint32))
        *(wxUint32*)buf=0;

    return len;
}



// swap 32bit MB to 16bit String
size_t wxMBConvUTF32swap::MB2WC(wchar_t *buf, const char *psz, size_t n) const
{
    size_t len=0;

    while (*(wxUint32*)psz && (!buf || len < n))
    {
        char tmp[4];
        tmp[0] = psz[3];   tmp[1] = psz[2];
        tmp[2] = psz[1];   tmp[3] = psz[0];


        wxUint16 cc[2];

        size_t pa=encode_utf16(*(wxUint32*)tmp, cc);
        if (pa == (size_t)-1)
            return pa;

        if (buf)
        {
            *buf++ = cc[0];
            if (pa > 1)
                *buf++ = cc[1];
        }
        len += pa;
        psz += sizeof(wxUint32);
    }

    if (buf && len<n)
        *buf=0;

    return len;
}


// swap 16bit String to 32bit MB
size_t wxMBConvUTF32swap::WC2MB(char *buf, const wchar_t *psz, size_t n) const
{
    size_t len=0;

    while (*psz && (!buf || len < n))
    {
        char cc[4];

        // cast is ok for WC_UTF16
        size_t pa=decode_utf16((const wxUint16 *)psz, *(wxUint32*)cc);
        if (pa == (size_t)-1)
            return pa;

        if (buf)
        {
            *buf++ = cc[3];
            *buf++ = cc[2];
            *buf++ = cc[1];
            *buf++ = cc[0];
        }
        len += sizeof(wxUint32);
        psz += pa;
    }

    if (buf && len<=n-sizeof(wxUint32))
        *(wxUint32*)buf=0;

    return len;
}

#else // WC_UTF16


// copy 32bit MB to 32bit String
size_t wxMBConvUTF32straight::MB2WC(wchar_t *buf, const char *psz, size_t n) const
{
    size_t len=0;

    while (*(wxUint32*)psz && (!buf || len < n))
    {
        if (buf)
            *buf++ = *(wxUint32*)psz;
        len++;
        psz += sizeof(wxUint32);
    }

    if (buf && len<n)
        *buf=0;

    return len;
}


// copy 32bit String to 32bit MB
size_t wxMBConvUTF32straight::WC2MB(char *buf, const wchar_t *psz, size_t n) const
{
    size_t len=0;

    while (*psz && (!buf || len < n))
    {
        if (buf)
        {
            *(wxUint32*)buf = *psz;
            buf += sizeof(wxUint32);
        }

        len += sizeof(wxUint32);
        psz++;
    }

    if (buf && len<=n-sizeof(wxUint32))
        *(wxUint32*)buf=0;

    return len;
}


// swap 32bit MB to 32bit String
size_t wxMBConvUTF32swap::MB2WC(wchar_t *buf, const char *psz, size_t n) const
{
    size_t len=0;

    while (*(wxUint32*)psz && (!buf || len < n))
    {
        if (buf)
        {
            ((char *)buf)[0] = psz[3];
            ((char *)buf)[1] = psz[2];
            ((char *)buf)[2] = psz[1];
            ((char *)buf)[3] = psz[0];
            buf++;
        }
        len++;
        psz += sizeof(wxUint32);
    }

    if (buf && len<n)
        *buf=0;

    return len;
}


// swap 32bit String to 32bit MB
size_t wxMBConvUTF32swap::WC2MB(char *buf, const wchar_t *psz, size_t n) const
{
    size_t len=0;

    while (*psz && (!buf || len < n))
    {
        if (buf)
        {
            *buf++ = ((char *)psz)[3];
            *buf++ = ((char *)psz)[2];
            *buf++ = ((char *)psz)[1];
            *buf++ = ((char *)psz)[0];
        }
        len += sizeof(wxUint32);
        psz++;
    }

    if (buf && len<=n-sizeof(wxUint32))
        *(wxUint32*)buf=0;

    return len;
}


#endif // WC_UTF16


// ============================================================================
// The classes doing conversion using the iconv_xxx() functions
// ============================================================================

#ifdef HAVE_ICONV

// VS: glibc 2.1.3 is broken in that iconv() conversion to/from UCS4 fails with
//     E2BIG if output buffer is _exactly_ as big as needed. Such case is
//     (unless there's yet another bug in glibc) the only case when iconv()
//     returns with (size_t)-1 (which means error) and says there are 0 bytes
//     left in the input buffer -- when _real_ error occurs,
//     bytes-left-in-input buffer is non-zero. Hence, this alternative test for
//     iconv() failure.
//     [This bug does not appear in glibc 2.2.]
#if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ <= 1
#define ICONV_FAILED(cres, bufLeft) ((cres == (size_t)-1) && \

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?