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

@ -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