Provide a safer version of convertUTF16toUTF8

This commit is contained in:
Ben Payne 2015-01-20 17:45:31 -05:00
parent a88339c219
commit e3bbc42925
10 changed files with 31 additions and 25 deletions

View file

@ -359,7 +359,7 @@ void StringBuffer::getCopy8(UTF8 *buff, const U32 buffSize) const
{
incRequestCount8();
AssertFatal(mBuffer.last() == 0, "StringBuffer::get UTF8 - not a null terminated string!");
convertUTF16toUTF8(mBuffer.address(), buff, buffSize);
convertUTF16toUTF8N(mBuffer.address(), buff, buffSize);
}
void StringBuffer::getCopy(UTF16 *buff, const U32 buffSize) const
@ -408,7 +408,7 @@ void StringBuffer::updateBuffer8()
{
U32 slackLen = getUTF8BufferSizeEstimate();
mBuffer8.setSize(slackLen);
U32 len = convertUTF16toUTF8(mBuffer.address(), mBuffer8.address(), slackLen);
U32 len = convertUTF16toUTF8N(mBuffer.address(), mBuffer8.address(), slackLen);
mBuffer8.setSize(len+1);
mBuffer8.compact();
mDirty8 = false;

View file

@ -191,7 +191,7 @@ U32 convertUTF8toUTF16N(const UTF8 *unistring, UTF16 *outbuffer, U32 len)
}
//-----------------------------------------------------------------------------
U32 convertUTF16toUTF8( const UTF16 *unistring, UTF8 *outbuffer, U32 len)
U32 convertUTF16toUTF8N( const UTF16 *unistring, UTF8 *outbuffer, U32 len)
{
AssertFatal(len >= 1, "Buffer for unicode conversion must be large enough to hold at least the null terminator.");
PROFILE_START(convertUTF16toUTF8);
@ -274,7 +274,7 @@ UTF8* convertUTF16toUTF8( const UTF16* unistring)
FrameTemp<UTF8> buf(len);
// perform conversion
nCodeunits = convertUTF16toUTF8( unistring, buf, len);
nCodeunits = convertUTF16toUTF8N( unistring, buf, len);
// add 1 for the NULL terminator the converter promises it included.
nCodeunits++;

View file

@ -81,7 +81,7 @@ UTF8* convertUTF16toUTF8( const UTF16 *unistring);
/// - If the provided buffer is too small, the output will be truncated.
U32 convertUTF8toUTF16N(const UTF8 *unistring, UTF16 *outbuffer, U32 len);
U32 convertUTF16toUTF8( const UTF16 *unistring, UTF8 *outbuffer, U32 len);
U32 convertUTF16toUTF8N( const UTF16 *unistring, UTF8 *outbuffer, U32 len);
template <size_t N>
inline U32 convertUTF8toUTF16(const UTF8 *unistring, UTF16 (&outbuffer)[N])
@ -89,6 +89,12 @@ inline U32 convertUTF8toUTF16(const UTF8 *unistring, UTF16 (&outbuffer)[N])
return convertUTF8toUTF16N(unistring, outbuffer, (U32) N);
}
template <size_t N>
inline U32 convertUTF16toUTF8(const UTF16 *unistring, UTF8 (&outbuffer)[N])
{
return convertUTF16toUTF8N(unistring, outbuffer, (U32) N);
}
//-----------------------------------------------------------------------------
/// Functions that converts one unicode codepoint at a time
/// - Since these functions are designed to be used in tight loops, they do not