Use libc for TextLength, TextCopy, TextSubtext, and TextInsert
This commit is contained in:
parent
27a4fe8851
commit
233a327d15
37
src/rtext.c
37
src/rtext.c
|
|
@ -1401,9 +1401,7 @@ unsigned int TextLength(const char *text)
|
||||||
|
|
||||||
if (text != NULL)
|
if (text != NULL)
|
||||||
{
|
{
|
||||||
// NOTE: Alternative: use strlen(text)
|
length = strlen(text);
|
||||||
|
|
||||||
while (*text++) length++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return length;
|
return length;
|
||||||
|
|
@ -1497,20 +1495,13 @@ int TextCopy(char *dst, const char *src)
|
||||||
{
|
{
|
||||||
int bytes = 0;
|
int bytes = 0;
|
||||||
|
|
||||||
if ((src != NULL) && (dst != NULL))
|
// strcpy is marked restrict, meaning src and dst must not alias.
|
||||||
|
// Attempt to defend against that, but this is not fully robust
|
||||||
|
// as someone could pass in two sub-portions of the same string.
|
||||||
|
if ((src != NULL) && (dst != NULL) && (src != dst))
|
||||||
{
|
{
|
||||||
// NOTE: Alternative: use strcpy(dst, src)
|
strcpy(dst, src);
|
||||||
|
bytes = strlen(src);
|
||||||
while (*src != '\0')
|
|
||||||
{
|
|
||||||
*dst = *src;
|
|
||||||
dst++;
|
|
||||||
src++;
|
|
||||||
|
|
||||||
bytes++;
|
|
||||||
}
|
|
||||||
|
|
||||||
*dst = '\0';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return bytes;
|
return bytes;
|
||||||
|
|
@ -1547,13 +1538,7 @@ const char *TextSubtext(const char *text, int position, int length)
|
||||||
if (length > maxLength) length = maxLength;
|
if (length > maxLength) length = maxLength;
|
||||||
if (length >= MAX_TEXT_BUFFER_LENGTH) length = MAX_TEXT_BUFFER_LENGTH - 1;
|
if (length >= MAX_TEXT_BUFFER_LENGTH) length = MAX_TEXT_BUFFER_LENGTH - 1;
|
||||||
|
|
||||||
// NOTE: Alternative: memcpy(buffer, text + position, length)
|
memcpy(buffer, text + position, length);
|
||||||
|
|
||||||
for (int c = 0 ; c < length ; c++)
|
|
||||||
{
|
|
||||||
buffer[c] = text[position + c];
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer[length] = '\0';
|
buffer[length] = '\0';
|
||||||
|
|
||||||
return buffer;
|
return buffer;
|
||||||
|
|
@ -1618,9 +1603,9 @@ char *TextInsert(const char *text, const char *insert, int position)
|
||||||
|
|
||||||
char *result = (char *)RL_MALLOC(textLen + insertLen + 1);
|
char *result = (char *)RL_MALLOC(textLen + insertLen + 1);
|
||||||
|
|
||||||
for (int i = 0; i < position; i++) result[i] = text[i];
|
memcpy(result, text, position);
|
||||||
for (int i = position; i < insertLen + position; i++) result[i] = insert[i];
|
memcpy(result + position, insert, insertLen);
|
||||||
for (int i = (insertLen + position); i < (textLen + insertLen); i++) result[i] = text[i];
|
memcpy(result + position + insertLen, text + position, textLen - position);
|
||||||
|
|
||||||
result[textLen + insertLen] = '\0'; // Make sure text string is valid!
|
result[textLen + insertLen] = '\0'; // Make sure text string is valid!
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user