Use stringinfo for escaping/unescaping

pull/6763/head^2
Gokhan Gulbiz 2023-03-21 09:58:31 +03:00
parent 0744384bac
commit 0b06e64c3f
No known key found for this signature in database
GPG Key ID: 608EF06B6BD1B45B
1 changed files with 10 additions and 13 deletions

View File

@ -28,6 +28,7 @@
#include "utils/json.h" #include "utils/json.h"
#include "distributed/utils/attribute.h" #include "distributed/utils/attribute.h"
#include "common/base64.h" #include "common/base64.h"
#include "miscadmin.h"
#include <time.h> #include <time.h>
@ -757,31 +758,28 @@ static char *
EscapeCommentChars(const char *str) EscapeCommentChars(const char *str)
{ {
int originalStringLength = strlen(str); int originalStringLength = strlen(str);
char *escapedString = (char *) palloc0((originalStringLength * 2 + 1) * sizeof(char)); StringInfo escapedString = makeStringInfo();
int escapedStringIndex = 0;
for (int originalStringIndex = 0; originalStringIndex < originalStringLength; for (int originalStringIndex = 0; originalStringIndex < originalStringLength;
originalStringIndex++) originalStringIndex++)
{ {
if (str[originalStringIndex] == '*' || str[originalStringIndex] == '/') if (str[originalStringIndex] == '*' || str[originalStringIndex] == '/')
{ {
escapedString[escapedStringIndex++] = '\\'; appendStringInfoChar(escapedString, '\\');
}
escapedString[escapedStringIndex++] = str[originalStringIndex];
}
escapedString[escapedStringIndex] = '\0';
return escapedString;
} }
appendStringInfoChar(escapedString, str[originalStringIndex]);
}
return escapedString->data;
}
/* UnescapeCommentChars removes the backslash that precedes '*' or '/' in the input string. */ /* UnescapeCommentChars removes the backslash that precedes '*' or '/' in the input string. */
static char * static char *
UnescapeCommentChars(const char *str) UnescapeCommentChars(const char *str)
{ {
int originalStringLength = strlen(str); int originalStringLength = strlen(str);
char *unescapedString = (char *) palloc0((originalStringLength + 1) * sizeof(char)); StringInfo unescapedString = makeStringInfo();
int unescapedStringIndex = 0;
for (int originalStringindex = 0; originalStringindex < originalStringLength; for (int originalStringindex = 0; originalStringindex < originalStringLength;
originalStringindex++) originalStringindex++)
@ -793,9 +791,8 @@ UnescapeCommentChars(const char *str)
{ {
originalStringindex++; originalStringindex++;
} }
unescapedString[unescapedStringIndex++] = str[originalStringindex]; appendStringInfoChar(unescapedString, str[originalStringindex]);
} }
unescapedString[unescapedStringIndex] = '\0';
return unescapedString; return unescapedString->data;
} }