REDESIGN: TextToUtf8()
ADDED: CodepointToUtf8()
This commit is contained in:
parent
6bf746d531
commit
d73abe73e5
@ -1195,8 +1195,8 @@ RLAPI bool TextIsEqual(const char *text1, const char *text2);
|
||||
RLAPI unsigned int TextLength(const char *text); // Get text length, checks for '\0' ending
|
||||
RLAPI const char *TextFormat(const char *text, ...); // Text formatting with variables (sprintf style)
|
||||
RLAPI const char *TextSubtext(const char *text, int position, int length); // Get a piece of a text string
|
||||
RLAPI char *TextReplace(char *text, const char *replace, const char *by); // Replace text string (memory should be freed!)
|
||||
RLAPI char *TextInsert(const char *text, const char *insert, int position); // Insert text in a position (memory should be freed!)
|
||||
RLAPI char *TextReplace(char *text, const char *replace, const char *by); // Replace text string (memory must be freed!)
|
||||
RLAPI char *TextInsert(const char *text, const char *insert, int position); // Insert text in a position (memory must be freed!)
|
||||
RLAPI const char *TextJoin(const char **textList, int count, const char *delimiter); // Join text strings with delimiter
|
||||
RLAPI const char **TextSplit(const char *text, char delimiter, int *count); // Split text into multiple strings
|
||||
RLAPI void TextAppend(char *text, const char *append, int *position); // Append text at specific position and move cursor!
|
||||
@ -1205,12 +1205,13 @@ RLAPI const char *TextToUpper(const char *text); // Get upp
|
||||
RLAPI const char *TextToLower(const char *text); // Get lower case version of provided string
|
||||
RLAPI const char *TextToPascal(const char *text); // Get Pascal case notation version of provided string
|
||||
RLAPI int TextToInteger(const char *text); // Get integer value from text (negative values not supported)
|
||||
RLAPI const char *TextToUtf8(int codepoint, int *byteLength); // Encode codepoint into utf8 text (char array length returned as parameter)
|
||||
RLAPI char *TextToUtf8(int *codepoints, int length); // Encode text codepoint into utf8 text (memory must be freed!)
|
||||
|
||||
// UTF8 text strings management functions
|
||||
RLAPI int *GetCodepoints(const char *text, int *count); // Get all codepoints in a string, codepoints count returned by parameters
|
||||
RLAPI int GetCodepointsCount(const char *text); // Get total number of characters (codepoints) in a UTF8 encoded string
|
||||
RLAPI int GetNextCodepoint(const char *text, int *bytesProcessed); // Returns next codepoint in a UTF8 encoded string; 0x3f('?') is returned on failure
|
||||
RLAPI const char *CodepointToUtf8(int codepoint, int *byteLength); // Encode codepoint into utf8 text (char array length returned as parameter)
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Basic 3d Shapes Drawing Functions (Module: models)
|
||||
|
89
src/text.c
89
src/text.c
@ -1377,45 +1377,28 @@ int TextToInteger(const char *text)
|
||||
return result;
|
||||
}
|
||||
|
||||
// Encode codepoint into utf8 text (char array length returned as parameter)
|
||||
RLAPI const char *TextToUtf8(int codepoint, int *byteLength)
|
||||
// Encode text codepoint into utf8 text (memory must be freed!)
|
||||
char *TextToUtf8(int *codepoints, int length)
|
||||
{
|
||||
static char utf8[6] = { 0 };
|
||||
int length = 0;
|
||||
|
||||
if (codepoint <= 0x7f)
|
||||
// We allocate enough memory fo fit all possible codepoints
|
||||
// NOTE: 5 bytes for every codepoint should be enough
|
||||
char *text = (char *)calloc(length*5, 1);
|
||||
const char *utf8 = NULL;
|
||||
int size = 0;
|
||||
|
||||
for (int i = 0, bytes = 0; i < length; i++)
|
||||
{
|
||||
utf8[0] = (char)codepoint;
|
||||
length = 1;
|
||||
utf8 = CodepointToUtf8(codepoints[i], &bytes);
|
||||
strncpy(text + size, utf8, bytes);
|
||||
size += bytes;
|
||||
}
|
||||
else if (codepoint <= 0x7ff)
|
||||
{
|
||||
utf8[0] = (char)(((codepoint >> 6) & 0x1f) | 0xc0);
|
||||
utf8[1] = (char)((codepoint & 0x3f) | 0x80);
|
||||
length = 2;
|
||||
}
|
||||
else if (codepoint <= 0xffff)
|
||||
{
|
||||
utf8[0] = (char)(((codepoint >> 12) & 0x0f) | 0xe0);
|
||||
utf8[1] = (char)(((codepoint >> 6) & 0x3f) | 0x80);
|
||||
utf8[2] = (char)((codepoint & 0x3f) | 0x80);
|
||||
length = 3;
|
||||
}
|
||||
else if (codepoint <= 0x10ffff)
|
||||
{
|
||||
utf8[0] = (char)(((codepoint >> 18) & 0x07) | 0xf0);
|
||||
utf8[1] = (char)(((codepoint >> 12) & 0x3f) | 0x80);
|
||||
utf8[2] = (char)(((codepoint >> 6) & 0x3f) | 0x80);
|
||||
utf8[3] = (char)((codepoint & 0x3f) | 0x80);
|
||||
length = 4;
|
||||
}
|
||||
|
||||
*byteLength = length;
|
||||
|
||||
return utf8;
|
||||
|
||||
// Resize memory to text length + string NULL terminator
|
||||
realloc(text, size + 1);
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
|
||||
// Get all codepoints in a string, codepoints count returned by parameters
|
||||
int *GetCodepoints(const char *text, int *count)
|
||||
{
|
||||
@ -1570,6 +1553,44 @@ int GetNextCodepoint(const char *text, int *bytesProcessed)
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
// Encode codepoint into utf8 text (char array length returned as parameter)
|
||||
RLAPI const char *CodepointToUtf8(int codepoint, int *byteLength)
|
||||
{
|
||||
static char utf8[6] = { 0 };
|
||||
int length = 0;
|
||||
|
||||
if (codepoint <= 0x7f)
|
||||
{
|
||||
utf8[0] = (char)codepoint;
|
||||
length = 1;
|
||||
}
|
||||
else if (codepoint <= 0x7ff)
|
||||
{
|
||||
utf8[0] = (char)(((codepoint >> 6) & 0x1f) | 0xc0);
|
||||
utf8[1] = (char)((codepoint & 0x3f) | 0x80);
|
||||
length = 2;
|
||||
}
|
||||
else if (codepoint <= 0xffff)
|
||||
{
|
||||
utf8[0] = (char)(((codepoint >> 12) & 0x0f) | 0xe0);
|
||||
utf8[1] = (char)(((codepoint >> 6) & 0x3f) | 0x80);
|
||||
utf8[2] = (char)((codepoint & 0x3f) | 0x80);
|
||||
length = 3;
|
||||
}
|
||||
else if (codepoint <= 0x10ffff)
|
||||
{
|
||||
utf8[0] = (char)(((codepoint >> 18) & 0x07) | 0xf0);
|
||||
utf8[1] = (char)(((codepoint >> 12) & 0x3f) | 0x80);
|
||||
utf8[2] = (char)(((codepoint >> 6) & 0x3f) | 0x80);
|
||||
utf8[3] = (char)((codepoint & 0x3f) | 0x80);
|
||||
length = 4;
|
||||
}
|
||||
|
||||
*byteLength = length;
|
||||
|
||||
return utf8;
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
|
Loading…
Reference in New Issue
Block a user