* Use a typedef utf_32_char in the UTF-8 code to store UTF-32 characters in
* This way it should be easy to change the type of utf_32_char if ever needed (currently it is uint_fast32_t) git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@2284 4a71c877-e1ca-e34f-864e-861f7616d084master
parent
100309f547
commit
f78bb18a5b
|
@ -114,9 +114,9 @@ size_t utf8_character_count(const char* utf8_string)
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t unicode_utf8_buffer_length(const uint_fast32_t* unicode_string)
|
size_t unicode_utf8_buffer_length(const utf_32_char* unicode_string)
|
||||||
{
|
{
|
||||||
const uint_fast32_t* curChar;
|
const utf_32_char* curChar;
|
||||||
|
|
||||||
// Determine length of string (in octets) when encoded in UTF-8
|
// Determine length of string (in octets) when encoded in UTF-8
|
||||||
size_t length = 0;
|
size_t length = 0;
|
||||||
|
@ -142,9 +142,9 @@ size_t unicode_utf8_buffer_length(const uint_fast32_t* unicode_string)
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
char* utf8_encode(const uint_fast32_t* unicode_string)
|
char* utf8_encode(const utf_32_char* unicode_string)
|
||||||
{
|
{
|
||||||
const uint_fast32_t* curChar;
|
const utf_32_char* curChar;
|
||||||
|
|
||||||
const size_t utf8_length = unicode_utf8_buffer_length(unicode_string);
|
const size_t utf8_length = unicode_utf8_buffer_length(unicode_string);
|
||||||
|
|
||||||
|
@ -241,14 +241,14 @@ char* utf8_encode(const uint_fast32_t* unicode_string)
|
||||||
return utf8_string;
|
return utf8_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint_fast32_t* utf8_decode(const char* utf8_string)
|
utf_32_char* utf8_decode(const char* utf8_string)
|
||||||
{
|
{
|
||||||
const char* curChar = utf8_string;
|
const char* curChar = utf8_string;
|
||||||
const size_t unicode_length = utf8_character_count(utf8_string);
|
const size_t unicode_length = utf8_character_count(utf8_string);
|
||||||
|
|
||||||
// Allocate memory to hold the UTF-32 encoded string (plus a terminating nul)
|
// Allocate memory to hold the UTF-32 encoded string (plus a terminating nul)
|
||||||
uint_fast32_t* unicode_string = malloc(sizeof(uint_fast32_t) * (unicode_length + 1));
|
utf_32_char* unicode_string = malloc(sizeof(utf_32_char) * (unicode_length + 1));
|
||||||
uint_fast32_t* curOutPos = unicode_string;
|
utf_32_char* curOutPos = unicode_string;
|
||||||
|
|
||||||
if (unicode_string == NULL)
|
if (unicode_string == NULL)
|
||||||
{
|
{
|
||||||
|
|
|
@ -27,6 +27,10 @@
|
||||||
|
|
||||||
#include "frame.h"
|
#include "frame.h"
|
||||||
|
|
||||||
|
/** Used to store a UTF-32 character in
|
||||||
|
*/
|
||||||
|
typedef uint_fast32_t utf_32_char;
|
||||||
|
|
||||||
/** Determines the amount of unicode characters in a UTF-8 encoded string
|
/** Determines the amount of unicode characters in a UTF-8 encoded string
|
||||||
* \param utf8_string the UTF-8 encoded string to count
|
* \param utf8_string the UTF-8 encoded string to count
|
||||||
* \return the amount of characters found in the UTF-8 string
|
* \return the amount of characters found in the UTF-8 string
|
||||||
|
@ -37,18 +41,18 @@ size_t utf8_character_count(const char* utf8_string);
|
||||||
* \param unicode_string the string to determine the UTF-8 buffer length of
|
* \param unicode_string the string to determine the UTF-8 buffer length of
|
||||||
* \return the size required to hold unicode_string if encoded in UTF-8
|
* \return the size required to hold unicode_string if encoded in UTF-8
|
||||||
*/
|
*/
|
||||||
size_t unicode_utf8_buffer_length(const uint_fast32_t* unicode_string);
|
size_t unicode_utf8_buffer_length(const utf_32_char* unicode_string);
|
||||||
|
|
||||||
/** Encodes a UTF-32 encoded unicode string to a UTF-8 encoded string
|
/** Encodes a UTF-32 encoded unicode string to a UTF-8 encoded string
|
||||||
* \param unicode_string the UTF-32 encoded unicode string to encode into UTF-8
|
* \param unicode_string the UTF-32 encoded unicode string to encode into UTF-8
|
||||||
* \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* utf8_encode(const uint_fast32_t* unicode_string);
|
char* utf8_encode(const utf_32_char* unicode_string);
|
||||||
|
|
||||||
/** Decodes a UTF-8 encode string to a UTF-32 encoded string (native endianess)
|
/** Decodes a UTF-8 encode string to a UTF-32 encoded string (native endianess)
|
||||||
* \param utf8_string a UTF-8 encoded nul terminated string
|
* \param utf8_string a UTF-8 encoded nul terminated string
|
||||||
* \return a UTF-32 encoded unicode nul terminated string (use free() to deallocate it)
|
* \return a UTF-32 encoded unicode nul terminated string (use free() to deallocate it)
|
||||||
*/
|
*/
|
||||||
uint_fast32_t* utf8_decode(const char* utf8_string);
|
utf_32_char* utf8_decode(const char* utf8_string);
|
||||||
|
|
||||||
#endif // __INCLUDE_LIB_FRAMEWORK_UTF8_H__
|
#endif // __INCLUDE_LIB_FRAMEWORK_UTF8_H__
|
||||||
|
|
Loading…
Reference in New Issue