mirror of https://github.com/citusdata/citus.git
Rewrite ExtractTopComment by using strstr() and stringinfo
parent
16c75165cf
commit
852db8f2f9
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
StringInfo commentData = makeStringInfo();
|
||||||
|
|
||||||
/* Skip the comment start characters */
|
/* Skip the comment start characters */
|
||||||
commentEndCharsIndex += commentStartCharsLength;
|
const char *commentStart = inputString + commentCharsLength;
|
||||||
|
|
||||||
/* Find the first comment end character */
|
/* Find the first comment end character */
|
||||||
while (commentEndCharsIndex < inputStringLen &&
|
const char *commentEnd = strstr(commentStart, commentEndChars);
|
||||||
!(inputString[commentEndCharsIndex] == '*' &&
|
if (commentEnd == NULL)
|
||||||
inputString [commentEndCharsIndex + 1] == '/'))
|
|
||||||
{
|
|
||||||
commentEndCharsIndex++;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* If there is no end of comment chars , return NULL */
|
|
||||||
if (inputString[commentEndCharsIndex] != '*' &&
|
|
||||||
inputString[commentEndCharsIndex + 1] != '/')
|
|
||||||
{
|
{
|
||||||
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)
|
||||||
|
|
Loading…
Reference in New Issue