Skip to content

Commit

Permalink
Change return type of replaceString function
Browse files Browse the repository at this point in the history
- Returns a bool indicating whether a match was made
- Accepts a parameter to force a copy even if no match was found
  • Loading branch information
jackdelv committed Oct 30, 2024
1 parent b6dc378 commit e3364e7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 24 deletions.
7 changes: 1 addition & 6 deletions rtl/eclrtl/eclrtl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6066,12 +6066,7 @@ void rtlAddExceptionTag(StringBuffer & errorText, const char * tag, const char *
void rtlSubstituteEmbeddedScript(size32_t &__lenResult, char * &__result, size32_t scriptChars, const char *script, size32_t outFieldsChars, const char *outFields, size32_t searchChars, const char *search)
{
StringBuffer result;
bool foundMatch = false;
size_t sourceLen = rtlUtf8Size(scriptChars, script);
::replaceString(result, sourceLen, script, rtlUtf8Size(searchChars, search), search, rtlUtf8Size(outFieldsChars, outFields), outFields, foundMatch);

if (!foundMatch)
result.append(sourceLen, script);
::replaceString(result, rtlUtf8Size(scriptChars, script), script, rtlUtf8Size(searchChars, search), search, rtlUtf8Size(outFieldsChars, outFields), outFields, true);

__lenResult = result.lengthUtf8();
__result = result.detach();
Expand Down
28 changes: 11 additions & 17 deletions system/jlib/jstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -935,27 +935,22 @@ StringBuffer & StringBuffer::replace(char oldChar, char newChar)
}

// Copy source to result, replacing all occurrences of "oldStr" with "newStr"
StringBuffer &replaceString(StringBuffer & result, size_t lenSource, const char *source, size_t lenOldStr, const char* oldStr, size_t lenNewStr, const char* newStr, bool & foundMatch)
bool replaceString(StringBuffer & result, size_t lenSource, const char *source, size_t lenOldStr, const char* oldStr, size_t lenNewStr, const char* newStr, bool forceCopy)
{
if (lenOldStr && lenSource >= lenOldStr)
{
size_t offset = 0;
size_t lastCopied = 0;
size_t maxOffset = lenSource - lenOldStr + 1;
char firstChar = oldStr[0];
foundMatch = false;
while (offset < maxOffset)
{
if (unlikely(source[offset] == firstChar)
&& unlikely((lenOldStr == 1) || memcmp(source + offset, oldStr, lenOldStr)==0))
{
// Wait to allocate memory until a match is found
if (unlikely(!foundMatch))
{
// Avoid allocating an unnecessarly large buffer and match the source string
result.ensureCapacity(lenSource);
foundMatch = true;
}
if (!lastCopied)
result.ensureCapacity(lenSource); // Avoid allocating an unnecessarly large buffer and match the source string

// If lastCopied matches the offset nothing is appended, but we can avoid a test for offset == lastCopied
result.append(offset - lastCopied, source + lastCopied);
Expand All @@ -967,11 +962,15 @@ StringBuffer &replaceString(StringBuffer & result, size_t lenSource, const char
offset++;
}

if (foundMatch)
if (lastCopied || forceCopy)
result.append(lenSource - lastCopied, source + lastCopied); // Append the remaining characters

return lastCopied;
}
else if (forceCopy)
result.append(lenSource, source); // Search string does not fit in source or is empty

return result;
return false;
}

StringBuffer &replaceVariables(StringBuffer & result, const char *source, bool exceptions, IVariableSubstitutionHelper *helper, const char* delim, const char* term)
Expand Down Expand Up @@ -1088,13 +1087,8 @@ StringBuffer & StringBuffer::replaceString(const char* oldStr, const char* newSt
else
{
StringBuffer temp;
bool foundMatch = false;
::replaceString(temp, curLen, buffer, oldlen, oldStr, newlen, newStr, foundMatch);

if (!foundMatch)
return *this;

swapWith(temp);
if(::replaceString(temp, curLen, buffer, oldlen, oldStr, newlen, newStr))
swapWith(temp);
}
}
return *this;
Expand Down
2 changes: 1 addition & 1 deletion system/jlib/jstring.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ extern jlib_decl void decodeXML(ISimpleReadStream &in, StringBuffer &out, unsign
extern jlib_decl int utf8CharLen(unsigned char ch);
extern jlib_decl int utf8CharLen(const unsigned char *ch, unsigned maxsize = (unsigned)-1);

extern jlib_decl StringBuffer &replaceString(StringBuffer & result, size_t lenSource, const char *source, size_t lenOldStr, const char* oldStr, size_t lenNewStr, const char* newStr, bool & foundMatch);
extern jlib_decl bool replaceString(StringBuffer & result, size_t lenSource, const char *source, size_t lenOldStr, const char* oldStr, size_t lenNewStr, const char* newStr, bool forceCopy = false);

interface IVariableSubstitutionHelper
{
Expand Down

0 comments on commit e3364e7

Please sign in to comment.