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-861f7616d084master
parent
a8709983e0
commit
bef604763f
|
@ -318,7 +318,7 @@ static size_t utf16_utf8_buffer_length(const utf_16_char* unicode_string)
|
||||||
return length;
|
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;
|
const utf_16_char* curChar;
|
||||||
|
|
||||||
|
@ -343,6 +343,12 @@ char *UTF16toUTF8(const utf_16_char* unicode_string)
|
||||||
// Terminate the string with a nul character
|
// Terminate the string with a nul character
|
||||||
utf8_string[utf8_length] = '\0';
|
utf8_string[utf8_length] = '\0';
|
||||||
|
|
||||||
|
// Set the number of bytes allocated
|
||||||
|
if (nbytes)
|
||||||
|
{
|
||||||
|
*nbytes = utf8_length + 1;
|
||||||
|
}
|
||||||
|
|
||||||
return utf8_string;
|
return utf8_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -376,7 +382,7 @@ static size_t utf8_as_utf16_buf_size(const char* utf8_string)
|
||||||
return length;
|
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 char* curChar = utf8_string;
|
||||||
const size_t unicode_length = utf8_as_utf16_buf_size(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
|
// Terminate the string with a nul
|
||||||
unicode_string[unicode_length] = '\0';
|
unicode_string[unicode_length] = '\0';
|
||||||
|
|
||||||
|
// Set the number of bytes allocated
|
||||||
|
if (nbytes)
|
||||||
|
{
|
||||||
|
*nbytes = sizeof(utf_16_char) * (unicode_length + 1);
|
||||||
|
}
|
||||||
|
|
||||||
return unicode_string;
|
return unicode_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -68,15 +68,17 @@ size_t UTF16CharacterCount(const uint16_t *utf16);
|
||||||
|
|
||||||
/** Encodes a UTF-16 encoded unicode string to a UTF-8 encoded string
|
/** 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 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)
|
* \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)
|
/** 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 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)
|
* \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);
|
char *UTF8CharacterAtOffset(const char *utf8_string, size_t index);
|
||||||
utf_16_char *UTF16CharacterAtOffset(const utf_16_char *utf16_string, size_t index);
|
utf_16_char *UTF16CharacterAtOffset(const utf_16_char *utf16_string, size_t index);
|
||||||
|
|
Loading…
Reference in New Issue