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/builtins.h"
#include "utils/json.h" #include "utils/json.h"
#include "distributed/utils/attribute.h" #include "distributed/utils/attribute.h"
#include "common/base64.h"
#include "miscadmin.h"
#include <time.h> #include <time.h>
@ -64,7 +63,6 @@ static int CreateTenantStats(MultiTenantMonitor *monitor);
static int FindTenantStats(MultiTenantMonitor *monitor); static int FindTenantStats(MultiTenantMonitor *monitor);
static size_t MultiTenantMonitorshmemSize(void); static size_t MultiTenantMonitorshmemSize(void);
static char * ExtractTopComment(const char *inputString); static char * ExtractTopComment(const char *inputString);
static char * Substring(const char *str, int start, int end);
static char * EscapeCommentChars(const char *str); static char * EscapeCommentChars(const char *str);
static char * UnescapeCommentChars(const char *str); static char * UnescapeCommentChars(const char *str);
@ -613,76 +611,42 @@ MultiTenantMonitorshmemSize(void)
static char * static char *
ExtractTopComment(const char *inputString) ExtractTopComment(const char *inputString)
{ {
int commentStartCharsLength = 2; int commentCharsLength = 2;
int inputStringLen = strlen(inputString); int inputStringLen = strlen(inputString);
if (inputStringLen < commentStartCharsLength) if (inputStringLen < commentCharsLength)
{ {
return NULL; return NULL;
} }
int commentEndCharsIndex = 0; const char *commentStartChars = "/*";
const char *commentEndChars = "*/";
/* If query doesn't start with a comment, return NULL */ /* If query doesn't start with a comment, return NULL */
if (inputString[commentEndCharsIndex] != '/' || if (strstr(inputString, commentStartChars) != inputString)
inputString[commentEndCharsIndex + 1] != '*') {
{ return NULL;
return NULL; }
}
/* Skip the comment start characters */ StringInfo commentData = makeStringInfo();
commentEndCharsIndex += commentStartCharsLength;
/* Find the first comment end character */ /* Skip the comment start characters */
while (commentEndCharsIndex < inputStringLen && const char *commentStart = inputString + commentCharsLength;
!(inputString[commentEndCharsIndex] == '*' &&
inputString [commentEndCharsIndex + 1] == '/'))
{
commentEndCharsIndex++;
}
/* If there is no end of comment chars , return NULL */ /* Find the first comment end character */
if (inputString[commentEndCharsIndex] != '*' && const char *commentEnd = strstr(commentStart, commentEndChars);
inputString[commentEndCharsIndex + 1] != '/') if (commentEnd == NULL)
{ {
return NULL; return NULL;
} }
if (commentEndCharsIndex > commentStartCharsLength) /* Append the comment to the StringInfo buffer */
{ int commentLength = commentEnd - commentStart;
return Substring(inputString, commentStartCharsLength, commentEndCharsIndex); appendStringInfo(commentData, "%.*s", commentLength, commentStart);
}
else /* Return the extracted comment */
{ return commentData->data;
return NULL;
}
} }
/* 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 */ /* EscapeCommentChars adds a backslash before each occurrence of '*' or '/' in the input string */
static char * static char *
EscapeCommentChars(const char *str) EscapeCommentChars(const char *str)