TextReplace changes (#1172)
- Added NULL return if the replacement string (`by`) is empty - Reordered sanity checks since there's no need to initialize variables if the strings are invalid.
This commit is contained in:
parent
8444c3f705
commit
9c280bc7af
1 changed files with 3 additions and 4 deletions
|
@ -1179,6 +1179,9 @@ const char *TextSubtext(const char *text, int position, int length)
|
||||||
// WARNING: Internally allocated memory must be freed by the user (if return != NULL)
|
// WARNING: Internally allocated memory must be freed by the user (if return != NULL)
|
||||||
char *TextReplace(char *text, const char *replace, const char *by)
|
char *TextReplace(char *text, const char *replace, const char *by)
|
||||||
{
|
{
|
||||||
|
// Sanity checks and initialization
|
||||||
|
if (!text || !replace || !by || by[0] == '\0') return NULL;
|
||||||
|
|
||||||
char *result;
|
char *result;
|
||||||
|
|
||||||
char *insertPoint; // Next insert point
|
char *insertPoint; // Next insert point
|
||||||
|
@ -1188,13 +1191,9 @@ char *TextReplace(char *text, const char *replace, const char *by)
|
||||||
int lastReplacePos; // Distance between replace and end of last replace
|
int lastReplacePos; // Distance between replace and end of last replace
|
||||||
int count; // Number of replacements
|
int count; // Number of replacements
|
||||||
|
|
||||||
// Sanity checks and initialization
|
|
||||||
if (!text || !replace) return NULL;
|
|
||||||
|
|
||||||
replaceLen = TextLength(replace);
|
replaceLen = TextLength(replace);
|
||||||
if (replaceLen == 0) return NULL; // Empty replace causes infinite loop during count
|
if (replaceLen == 0) return NULL; // Empty replace causes infinite loop during count
|
||||||
|
|
||||||
if (!by) by = ""; // Replace by nothing if not provided
|
|
||||||
byLen = TextLength(by);
|
byLen = TextLength(by);
|
||||||
|
|
||||||
// Count the number of replacements needed
|
// Count the number of replacements needed
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue