Rewrite ExtractTopComment by using strstr() and stringinfo

pull/6753/head
Gokhan Gulbiz 2023-03-21 12:34:05 +03:00
parent 16c75165cf
commit 852db8f2f9
No known key found for this signature in database
GPG Key ID: 608EF06B6BD1B45B
1 changed files with 30 additions and 66 deletions

View File

@ -24,8 +24,7 @@
#include "utils/builtins.h"
#include "utils/json.h"
#include "distributed/utils/attribute.h"
#include "common/base64.h"
#include "miscadmin.h"
#include <time.h>
@ -64,7 +63,6 @@ static int CreateTenantStats(MultiTenantMonitor *monitor);
static int FindTenantStats(MultiTenantMonitor *monitor);
static size_t MultiTenantMonitorshmemSize(void);
static char * ExtractTopComment(const char *inputString);
static char * Substring(const char *str, int start, int end);
static char * EscapeCommentChars(const char *str);
static char * UnescapeCommentChars(const char *str);
@ -613,76 +611,42 @@ MultiTenantMonitorshmemSize(void)
static char *
ExtractTopComment(const char *inputString)
{
int commentStartCharsLength = 2;
int commentCharsLength = 2;
int inputStringLen = strlen(inputString);
if (inputStringLen < commentStartCharsLength)
if (inputStringLen < commentCharsLength)
{
return NULL;
}
int commentEndCharsIndex = 0;
const char *commentStartChars = "/*";
const char *commentEndChars = "*/";
/* If query doesn't start with a comment, return NULL */
if (inputString[commentEndCharsIndex] != '/' ||
inputString[commentEndCharsIndex + 1] != '*')
if (strstr(inputString, commentStartChars) != inputString)
{
return NULL;
}
StringInfo commentData = makeStringInfo();
/* Skip the comment start characters */
commentEndCharsIndex += commentStartCharsLength;
const char *commentStart = inputString + commentCharsLength;
/* Find the first comment end character */
while (commentEndCharsIndex < inputStringLen &&
!(inputString[commentEndCharsIndex] == '*' &&
inputString [commentEndCharsIndex + 1] == '/'))
{
commentEndCharsIndex++;
}
/* If there is no end of comment chars , return NULL */
if (inputString[commentEndCharsIndex] != '*' &&
inputString[commentEndCharsIndex + 1] != '/')
const char *commentEnd = strstr(commentStart, commentEndChars);
if (commentEnd == NULL)
{
return NULL;
}
if (commentEndCharsIndex > commentStartCharsLength)
{
return Substring(inputString, commentStartCharsLength, commentEndCharsIndex);
}
else
{
return NULL;
}
/* Append the comment to the StringInfo buffer */
int commentLength = commentEnd - commentStart;
appendStringInfo(commentData, "%.*s", commentLength, commentStart);
/* Return the extracted comment */
return commentData->data;
}
/* Extracts a substring from the input string between the specified start and end indices.*/
static char *
Substring(const char *str, int start, int end)
{
int len = strlen(str);
/* Ensure start and end are within the bounds of the string */
if (start < 0 || end > len || start > end)
{
return NULL;
}
/* Allocate memory for the substring */
char *substr = (char *) palloc((end - start + 1) * sizeof(char));
/* Copy the substring to the new memory location */
strncpy_s(substr, end - start + 1, str + start, end - start);
/* Add null terminator to end of the substring */
substr[end - start] = '\0';
return substr;
}
/* EscapeCommentChars adds a backslash before each occurrence of '*' or '/' in the input string */
static char *
EscapeCommentChars(const char *str)