mirror of
https://github.com/raysan5/raylib.git
synced 2026-02-20 20:49:17 -05:00
REVIEWED: TextReplace() and TextLength(), avoid using strcpy()
This commit is contained in:
58
src/rtext.c
58
src/rtext.c
@ -1497,15 +1497,14 @@ void UnloadTextLines(char **lines, int lineCount)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get text length in bytes, check for \0 character
|
// Get text length in bytes, check for \0 character
|
||||||
|
// NOTE: Alternative: use strlen(text)
|
||||||
unsigned int TextLength(const char *text)
|
unsigned int TextLength(const char *text)
|
||||||
{
|
{
|
||||||
unsigned int length = 0;
|
unsigned int length = 0;
|
||||||
|
|
||||||
if (text != NULL)
|
if (text != NULL)
|
||||||
{
|
{
|
||||||
// NOTE: Alternative: use strlen(text)
|
while (text[length] != '\0') length++;
|
||||||
|
|
||||||
while (*text++) length++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return length;
|
return length;
|
||||||
@ -1718,7 +1717,7 @@ char *TextReplace(const char *text, const char *search, const char *replacement)
|
|||||||
{
|
{
|
||||||
char *result = NULL;
|
char *result = NULL;
|
||||||
|
|
||||||
if ((text != NULL) && (search != NULL))
|
if ((text != NULL) && (search != NULL) && (search[0] != '\0'))
|
||||||
{
|
{
|
||||||
if (replacement == NULL) replacement = "";
|
if (replacement == NULL) replacement = "";
|
||||||
|
|
||||||
@ -1732,8 +1731,6 @@ char *TextReplace(const char *text, const char *search, const char *replacement)
|
|||||||
|
|
||||||
textLen = TextLength(text);
|
textLen = TextLength(text);
|
||||||
searchLen = TextLength(search);
|
searchLen = TextLength(search);
|
||||||
if (searchLen == 0) return NULL; // Empty search causes infinite loop during count
|
|
||||||
|
|
||||||
replaceLen = TextLength(replacement);
|
replaceLen = TextLength(replacement);
|
||||||
|
|
||||||
// Count the number of replacements needed
|
// Count the number of replacements needed
|
||||||
@ -1742,34 +1739,37 @@ char *TextReplace(const char *text, const char *search, const char *replacement)
|
|||||||
|
|
||||||
// Allocate returning string and point temp to it
|
// Allocate returning string and point temp to it
|
||||||
int tempLen = textLen + (replaceLen - searchLen)*count + 1;
|
int tempLen = textLen + (replaceLen - searchLen)*count + 1;
|
||||||
temp = result = (char *)RL_MALLOC(tempLen);
|
temp = result = (char *)RL_CALLOC(tempLen, sizeof(char));
|
||||||
|
|
||||||
if (!result) return NULL; // Memory could not be allocated
|
if (result != NULL) // Memory was allocated
|
||||||
|
|
||||||
// First time through the loop, all the variable are set correctly from here on,
|
|
||||||
// - 'temp' points to the end of the result string
|
|
||||||
// - 'insertPoint' points to the next occurrence of replace in text
|
|
||||||
// - 'text' points to the remainder of text after "end of replace"
|
|
||||||
while (count--)
|
|
||||||
{
|
{
|
||||||
insertPoint = (char *)strstr(text, search);
|
// First time through the loop, all the variable are set correctly from here on,
|
||||||
lastReplacePos = (int)(insertPoint - text);
|
// - 'temp' points to the end of the result string
|
||||||
|
// - 'insertPoint' points to the next occurrence of replace in text
|
||||||
memcpy(temp, text, lastReplacePos);
|
// - 'text' points to the remainder of text after "end of replace"
|
||||||
temp += lastReplacePos;
|
while (count > 0)
|
||||||
|
|
||||||
if (replaceLen > 0)
|
|
||||||
{
|
{
|
||||||
memcpy(temp, replacement, replaceLen);
|
insertPoint = (char *)strstr(text, search);
|
||||||
temp += replaceLen;
|
lastReplacePos = (int)(insertPoint - text);
|
||||||
|
|
||||||
|
memcpy(temp, text, lastReplacePos);
|
||||||
|
temp += lastReplacePos;
|
||||||
|
|
||||||
|
if (replaceLen > 0)
|
||||||
|
{
|
||||||
|
memcpy(temp, replacement, replaceLen);
|
||||||
|
temp += replaceLen;
|
||||||
|
}
|
||||||
|
|
||||||
|
text += (lastReplacePos + searchLen); // Move to next "end of replace"
|
||||||
|
|
||||||
|
count--;
|
||||||
}
|
}
|
||||||
|
|
||||||
text += lastReplacePos + searchLen; // Move to next "end of replace"
|
// Copy remaind text part after replacement to result (pointed by moving temp)
|
||||||
|
// NOTE: Text pointer internal copy has been updated along the process
|
||||||
|
strncpy(temp, text, TextLength(text));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy remaind text part after replacement to result (pointed by moving temp)
|
|
||||||
strcpy(temp, text); // OK
|
|
||||||
//strncpy(temp, text, tempLen - 1); // WRONG
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
Reference in New Issue
Block a user