Add an nbytes parameter to UTF8toUTF16 and UTF16toUTF8 which allow for fetching the number of bytes allocated.

git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@5917 4a71c877-e1ca-e34f-864e-861f7616d084
master
Freddie Witherden 2008-09-02 14:37:01 +00:00
parent a8709983e0
commit bef604763f
2 changed files with 18 additions and 4 deletions

View File

@ -318,7 +318,7 @@ static size_t utf16_utf8_buffer_length(const utf_16_char* unicode_string)
return length;
}
char *UTF16toUTF8(const utf_16_char* unicode_string)
char *UTF16toUTF8(const utf_16_char *unicode_string, size_t *nbytes)
{
const utf_16_char* curChar;
@ -343,6 +343,12 @@ char *UTF16toUTF8(const utf_16_char* unicode_string)
// Terminate the string with a nul character
utf8_string[utf8_length] = '\0';
// Set the number of bytes allocated
if (nbytes)
{
*nbytes = utf8_length + 1;
}
return utf8_string;
}
@ -376,7 +382,7 @@ static size_t utf8_as_utf16_buf_size(const char* utf8_string)
return length;
}
utf_16_char *UTF8toUTF16(const char* utf8_string)
utf_16_char *UTF8toUTF16(const char* utf8_string, size_t *nbytes)
{
const char* curChar = utf8_string;
const size_t unicode_length = utf8_as_utf16_buf_size(utf8_string);
@ -399,6 +405,12 @@ utf_16_char *UTF8toUTF16(const char* utf8_string)
// Terminate the string with a nul
unicode_string[unicode_length] = '\0';
// Set the number of bytes allocated
if (nbytes)
{
*nbytes = sizeof(utf_16_char) * (unicode_length + 1);
}
return unicode_string;
}

View File

@ -68,15 +68,17 @@ size_t UTF16CharacterCount(const uint16_t *utf16);
/** Encodes a UTF-16 encoded unicode string to a UTF-8 encoded string
* \param unicode_string the UTF-16 encoded unicode string to encode into UTF-8
* \param[out] nbytes the number of bytes allocated, may be NULL
* \return a UTF-8 encoded unicode nul terminated string (use free() to deallocate it)
*/
char *UTF16toUTF8(const utf_16_char *unicode_string);
char *UTF16toUTF8(const utf_16_char *unicode_string, size_t *nbytes);
/** Decodes a UTF-8 encode string to a UTF-16 encoded string (native endianess)
* \param utf8_string a UTF-8 encoded nul terminated string
* \param[out] nbytes the number of bytes allocated, may be NULL
* \return a UTF-16 encoded unicode nul terminated string (use free() to deallocate it)
*/
utf_16_char *UTF8toUTF16(const char *utf8_string);
utf_16_char *UTF8toUTF16(const char *utf8_string, size_t *nbytes);
char *UTF8CharacterAtOffset(const char *utf8_string, size_t index);
utf_16_char *UTF16CharacterAtOffset(const utf_16_char *utf16_string, size_t index);