X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/638357a03eced5cc1ecb9f79f8ad3f03f465c889..9386cb75e34acb49cd9fac945e09cf002f22db2d:/src/common/strconv.cpp diff --git a/src/common/strconv.cpp b/src/common/strconv.cpp index 3e8a373222..50571277e7 100644 --- a/src/common/strconv.cpp +++ b/src/common/strconv.cpp @@ -217,14 +217,18 @@ const wxCharBuffer wxMBConv::cWC2MB(const wchar_t *pwz) const return buf; } -size_t wxMBConv::MB2WC(wchar_t* szBuffer, const char* szString, - size_t outsize, size_t nStringLen) const +const wxWCharBuffer wxMBConv::cMB2WC(const char *szString, size_t nStringLen, size_t* pOutSize) const { + wxASSERT(pOutSize != NULL); + const char* szEnd = szString + nStringLen + 1; const char* szPos = szString; const char* szStart = szPos; size_t nActualLength = 0; + size_t nCurrentSize = nStringLen; //try normal size first (should never resize?) + + wxWCharBuffer theBuffer(nCurrentSize); //Convert the string until the length() is reached, continuing the //loop every time a null character is reached @@ -237,18 +241,31 @@ size_t wxMBConv::MB2WC(wchar_t* szBuffer, const char* szString, //Invalid conversion? if( nLen == (size_t)-1 ) - return nLen; + { + *pOutSize = 0; + theBuffer.data()[0u] = wxT('\0'); + return theBuffer; + } + //Increase the actual length (+1 for current null character) nActualLength += nLen + 1; - //Only copy data in if buffer size is big enough - if (szBuffer != NULL && - nActualLength <= outsize) + //if buffer too big, realloc the buffer + if (nActualLength > (nCurrentSize+1)) { - //Convert the current (sub)string - if ( MB2WC(&szBuffer[szPos - szStart], szPos, nLen + 1) == (size_t)-1 ) - return (size_t)-1; + wxWCharBuffer theNewBuffer(nCurrentSize << 1); + memcpy(theNewBuffer.data(), theBuffer.data(), nCurrentSize * sizeof(wchar_t)); + theBuffer = theNewBuffer; + nCurrentSize <<= 1; + } + + //Convert the current (sub)string + if ( MB2WC(&theBuffer.data()[szPos - szStart], szPos, nLen + 1) == (size_t)-1 ) + { + *pOutSize = 0; + theBuffer.data()[0u] = wxT('\0'); + return theBuffer; } //Increment to next (sub)string @@ -258,17 +275,23 @@ size_t wxMBConv::MB2WC(wchar_t* szBuffer, const char* szString, szPos += strlen(szPos) + 1; } - return nActualLength - 1; //success - return actual length + //success - return actual length and the buffer + *pOutSize = nActualLength; + return theBuffer; } -size_t wxMBConv::WC2MB(char* szBuffer, const wchar_t* szString, - size_t outsize, size_t nStringLen) const +const wxCharBuffer wxMBConv::cWC2MB(const wchar_t *szString, size_t nStringLen, size_t* pOutSize) const { + wxASSERT(pOutSize != NULL); + const wchar_t* szEnd = szString + nStringLen + 1; const wchar_t* szPos = szString; const wchar_t* szStart = szPos; size_t nActualLength = 0; + size_t nCurrentSize = nStringLen << 2; //try * 4 first + + wxCharBuffer theBuffer(nCurrentSize); //Convert the string until the length() is reached, continuing the //loop every time a null character is reached @@ -281,18 +304,30 @@ size_t wxMBConv::WC2MB(char* szBuffer, const wchar_t* szString, //Invalid conversion? if( nLen == (size_t)-1 ) - return nLen; + { + *pOutSize = 0; + theBuffer.data()[0u] = wxT('\0'); + return theBuffer; + } //Increase the actual length (+1 for current null character) nActualLength += nLen + 1; - //Only copy data in if buffer size is big enough - if (szBuffer != NULL && - nActualLength <= outsize) + //if buffer too big, realloc the buffer + if (nActualLength > (nCurrentSize+1)) { - //Convert the current (sub)string - if(WC2MB(&szBuffer[szPos - szStart], szPos, nLen + 1) == (size_t)-1 ) - return (size_t)-1; + wxCharBuffer theNewBuffer(nCurrentSize << 1); + memcpy(theNewBuffer.data(), theBuffer.data(), nCurrentSize); + theBuffer = theNewBuffer; + nCurrentSize <<= 1; + } + + //Convert the current (sub)string + if(WC2MB(&theBuffer.data()[szPos - szStart], szPos, nLen + 1) == (size_t)-1 ) + { + *pOutSize = 0; + theBuffer.data()[0u] = wxT('\0'); + return theBuffer; } //Increment to next (sub)string @@ -302,7 +337,9 @@ size_t wxMBConv::WC2MB(char* szBuffer, const wchar_t* szString, szPos += wxWcslen(szPos) + 1; } - return nActualLength - 1; //success - return actual length + //success - return actual length and the buffer + *pOutSize = nActualLength; + return theBuffer; } // ---------------------------------------------------------------------------- @@ -399,7 +436,7 @@ size_t wxMBConvUTF7::MB2WC(wchar_t *buf, const char *psz, size_t n) const d += cc; for (l += 6; l >= 8; lsb = !lsb) { - c = (d >> (l -= 8)) % 256; + c = (unsigned char)((d >> (l -= 8)) % 256); if (lsb) { if (buf) @@ -408,7 +445,7 @@ size_t wxMBConvUTF7::MB2WC(wchar_t *buf, const char *psz, size_t n) const } else if (buf) - *buf = c << 8; + *buf = (wchar_t)(c << 8); } } if (*psz == '-') @@ -473,8 +510,8 @@ size_t wxMBConvUTF7::WC2MB(char *buf, const wchar_t len++; } #ifndef WC_UTF16 - else if (cc > ((const wchar_t)0xffff)) - { + else if (((wxUint32)cc) > 0xffff) + { // no surrogate pair generation (yet?) return (size_t)-1; } @@ -2020,7 +2057,7 @@ public: if (buf == NULL) { //apple specs say at least 32 - n = 32 ; + n = wxMax( 32 , byteInLen ) ; tbuf = (wchar_t*) malloc( n * SIZEOF_WCHAR_T) ; } ByteCount byteBufferLen = n * sizeof( UniChar ) ; @@ -2061,7 +2098,7 @@ public: if (buf == NULL) { //apple specs say at least 32 - n = 32; + n = wxMax( 32 , ((byteInLen / SIZEOF_WCHAR_T) * 8) + SIZEOF_WCHAR_T ); tbuf = (char*) malloc( n ) ; }