Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/common/stringinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,16 +364,32 @@ enlargeStringInfo(StringInfo str, int needed)
void
replaceStringInfoString(StringInfo str, char *replace, char *replacement)
{
char *ptr;
char *match_ptr = NULL;
char *start_ptr = str->data;
char *dup = NULL;
size_t replace_len = strlen(replace);

while ((ptr = strstr(str->data, replace)) != NULL)
{
char *dup = pstrdup(str->data);
// prevent empty loop, cuz strstr will always return start_ptr
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cuz -> because

if (replace_len == 0)
return;

resetStringInfo(str);
appendBinaryStringInfo(str, dup, ptr - str->data);
while ((match_ptr = strstr(start_ptr, replace)) != NULL)
{
if (dup == NULL)
{
dup = pstrdup(str->data);
start_ptr = dup;
match_ptr = dup + (match_ptr - str->data);
resetStringInfo(str);
}

appendBinaryStringInfo(str, start_ptr, match_ptr - start_ptr);
appendStringInfoString(str, replacement);
appendStringInfoString(str, dup + (ptr - str->data) + strlen(replace));
start_ptr = match_ptr + replace_len;
}
if (dup != NULL)
{
appendStringInfoString(str, start_ptr);
pfree(dup);
}
}
Loading