improved TextSubtext function to use memcpy
This commit is contained in:
parent
a5e8e064f8
commit
63cc7c671d
21
src/rtext.c
21
src/rtext.c
|
|
@ -1430,27 +1430,18 @@ bool TextIsEqual(const char *text1, const char *text2)
|
||||||
// Get a piece of a text string
|
// Get a piece of a text string
|
||||||
const char *TextSubtext(const char *text, int position, int length)
|
const char *TextSubtext(const char *text, int position, int length)
|
||||||
{
|
{
|
||||||
static char buffer[MAX_TEXT_BUFFER_LENGTH] = { 0 };
|
if (!length)
|
||||||
memset(buffer, 0, MAX_TEXT_BUFFER_LENGTH);
|
return "";
|
||||||
|
|
||||||
int textLength = TextLength(text);
|
int textLength = TextLength(text);
|
||||||
|
|
||||||
if (position >= textLength)
|
if (position >= textLength)
|
||||||
{
|
{
|
||||||
position = textLength - 1;
|
position = textLength - 1;
|
||||||
length = 0;
|
length = 0;
|
||||||
}
|
}
|
||||||
|
if (length > textLength) length = textLength;
|
||||||
if (length >= textLength) length = textLength;
|
char *buffer = malloc(length + 1);
|
||||||
|
memcpy(buffer, text + position, length);
|
||||||
for (int c = 0 ; c < length ; c++)
|
buffer[length] = '\0';
|
||||||
{
|
|
||||||
*(buffer + c) = *(text + position);
|
|
||||||
text++;
|
|
||||||
}
|
|
||||||
|
|
||||||
*(buffer + length) = '\0';
|
|
||||||
|
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user