1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2024-12-12 10:04:14 +02:00

Move regExpMatchPtr()/regExpMatchStr() to build/common/regExp module.

Similar to b9be4fa5, these functions are not used by the core code so move them to the build module. The new implementation is a little less efficient but that is much less of a worry in the build/test code.

Also remove regExpMatchSize() since it was not longer needed.
This commit is contained in:
David Steele 2022-12-31 12:54:33 +07:00
parent 45ece13678
commit 8b218158ae
8 changed files with 94 additions and 81 deletions

View File

@ -22,7 +22,6 @@ SRCS_COMMON = \
common/io/write.c \
common/log.c \
common/memContext.c \
common/regExp.c \
common/stackTrace.c \
common/time.c \
common/type/blob.c \
@ -134,6 +133,7 @@ SRCS = \
common/io/tls/server.c \
common/io/tls/session.c \
common/lock.c \
common/regExp.c \
common/stat.c \
common/type/json.c \
common/type/string.c \
@ -211,6 +211,7 @@ pgbackrest: $(OBJS_PGBACKREST)
# Compile and link code builder
####################################################################################################################################
SRCS_BUILD_CODE = \
build/common/regExp.c \
build/common/render.c \
build/common/string.c \
build/common/xml.c \

60
src/build/common/regExp.c Normal file
View File

@ -0,0 +1,60 @@
/***********************************************************************************************************************************
Regular Expression Handler Extensions
***********************************************************************************************************************************/
// Include core module
#include "common/regExp.c"
/***********************************************************************************************************************************
Getters/Setters
***********************************************************************************************************************************/
const char *
regExpMatchPtr(RegExp *const this, const String *const string)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(REGEXP, this);
FUNCTION_TEST_PARAM(STRING, string);
FUNCTION_TEST_END();
ASSERT(this != NULL);
ASSERT(string != NULL);
// Test for a match
regmatch_t matchPtr;
const int result = regexec(&this->regExp, strZ(string), 1, &matchPtr, 0);
// Check for an error
regExpErrorCheck(result);
// Return pointer to match
if (result == 0)
FUNCTION_TEST_RETURN_CONST(STRINGZ, strZ(string) + matchPtr.rm_so);
// Return NULL when no match
FUNCTION_TEST_RETURN_CONST(STRINGZ, NULL);
}
String *
regExpMatchStr(RegExp *const this, const String *const string)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(REGEXP, this);
FUNCTION_TEST_PARAM(STRING, string);
FUNCTION_TEST_END();
ASSERT(this != NULL);
ASSERT(string != NULL);
// Test for a match
regmatch_t matchPtr;
int result = regexec(&this->regExp, strZ(string), 1, &matchPtr, 0);
// Check for an error
regExpErrorCheck(result);
// Return match as string
if (result == 0)
FUNCTION_TEST_RETURN(STRING, strNewZN(strZ(string) + matchPtr.rm_so, (size_t)(matchPtr.rm_eo - matchPtr.rm_so)));
// Return NULL when no match
FUNCTION_TEST_RETURN(STRING, NULL);
}

18
src/build/common/regExp.h Normal file
View File

@ -0,0 +1,18 @@
/***********************************************************************************************************************************
Regular Expression Handler Extensions
***********************************************************************************************************************************/
#ifndef BUILD_COMMON_REGEXP_H
#define BUILD_COMMON_REGEXP_H
#include "common/regExp.h"
/***********************************************************************************************************************************
Getters/Setters
***********************************************************************************************************************************/
// Get pointer to the last match. NULL if there was no match.
const char *regExpMatchPtr(RegExp *this, const String *string);
// Get the last match as a String. NULL if there was no match.
String *regExpMatchStr(RegExp *this, const String *string);
#endif

View File

@ -15,8 +15,6 @@ Contains information about the regular expression handler
struct RegExp
{
regex_t regExp;
const char *matchPtr;
size_t matchSize;
};
/***********************************************************************************************************************************
@ -95,7 +93,7 @@ regExpNew(const String *expression)
/**********************************************************************************************************************************/
bool
regExpMatch(RegExp *this, const String *string)
regExpMatch(RegExp *const this, const String *const string)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(REGEXP, this);
@ -107,66 +105,14 @@ regExpMatch(RegExp *this, const String *string)
// Test for a match
regmatch_t matchPtr;
int result = regexec(&this->regExp, strZ(string), 1, &matchPtr, 0);
const int result = regexec(&this->regExp, strZ(string), 1, &matchPtr, 0);
// Check for an error
regExpErrorCheck(result);
// Store match results
if (result == 0)
{
this->matchPtr = strZ(string) + matchPtr.rm_so;
this->matchSize = (size_t)(matchPtr.rm_eo - matchPtr.rm_so);
}
// Else reset match results
else
{
this->matchPtr = NULL;
this->matchSize = 0;
}
FUNCTION_TEST_RETURN(BOOL, result == 0);
}
/***********************************************************************************************************************************
Getters/Setters
***********************************************************************************************************************************/
const char *
regExpMatchPtr(RegExp *this)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(REGEXP, this);
FUNCTION_TEST_END();
ASSERT(this != NULL);
FUNCTION_TEST_RETURN_CONST(STRINGZ, this->matchPtr);
}
size_t
regExpMatchSize(RegExp *this)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(REGEXP, this);
FUNCTION_TEST_END();
ASSERT(this != NULL);
FUNCTION_TEST_RETURN(SIZE, this->matchSize);
}
String *
regExpMatchStr(RegExp *this)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(REGEXP, this);
FUNCTION_TEST_END();
ASSERT(this != NULL);
FUNCTION_TEST_RETURN(STRING, this->matchPtr == NULL ? NULL : strNewZN(regExpMatchPtr(this), regExpMatchSize(this)));
}
/**********************************************************************************************************************************/
bool
regExpMatchOne(const String *expression, const String *string)

View File

@ -23,18 +23,6 @@ Functions
// Match on a regular expression
bool regExpMatch(RegExp *this, const String *string);
/***********************************************************************************************************************************
Getters/Setters
***********************************************************************************************************************************/
// Get pointer to the last match. NULL if there was no match.
const char *regExpMatchPtr(RegExp *this);
// Get size of the last match. 0 if there was no match.
size_t regExpMatchSize(RegExp *this);
// Get the last match as a String. NULL if there was no match.
String *regExpMatchStr(RegExp *this);
/***********************************************************************************************************************************
Helper functions
***********************************************************************************************************************************/

View File

@ -188,7 +188,8 @@ unit:
total: 3
coverage:
- common/regExp
- build/common/regExp
- common/regExp: included
# ----------------------------------------------------------------------------------------------------------------------------
- name: log

View File

@ -9,9 +9,9 @@ Log Test Harness
#include <stdio.h>
#include <string.h>
#include "build/common/regExp.h"
#include "common/log.h"
#include "common/memContext.h"
#include "common/regExp.h"
#include "common/type/stringList.h"
#include "common/harnessDebug.h"
@ -325,10 +325,11 @@ hrnLogReplace(void)
while (regExpMatch(logReplace->regExp, STRDEF(harnessLogBuffer)))
{
// Get the match
String *match = regExpMatchStr(logReplace->regExp);
String *match = regExpMatchStr(logReplace->regExp, STRDEF(harnessLogBuffer));
// Find beginning of match
char *begin = harnessLogBuffer + (regExpMatchPtr(logReplace->regExp) - harnessLogBuffer);
char *begin =
harnessLogBuffer + (regExpMatchPtr(logReplace->regExp, STRDEF(harnessLogBuffer)) - harnessLogBuffer);
// If there is a sub expression then evaluate it
if (logReplace->regExpSub != NULL)
@ -342,10 +343,10 @@ hrnLogReplace(void)
}
// Find beginning of match
begin += regExpMatchPtr(logReplace->regExpSub) - strZ(match);
begin += regExpMatchPtr(logReplace->regExpSub, match) - strZ(match);
// Get the match
match = regExpMatchStr(logReplace->regExpSub);
match = regExpMatchStr(logReplace->regExpSub, match);
}
// Build replacement string. If versioned then append the version number.

View File

@ -58,17 +58,15 @@ testRun(void)
const String *string = STRDEF("abcdef");
TEST_RESULT_BOOL(regExpMatch(regExp, string), true, "match regexp");
TEST_RESULT_PTR(regExpMatchPtr(regExp), strZ(string), "check ptr");
TEST_RESULT_UINT(regExpMatchSize(regExp), 3, "check size");
TEST_RESULT_STR_Z(regExpMatchStr(regExp), "abc", "check str");
TEST_RESULT_PTR(regExpMatchPtr(regExp, string), strZ(string), "check ptr");
TEST_RESULT_STR_Z(regExpMatchStr(regExp, string), "abc", "check str");
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("no regexp match");
TEST_RESULT_BOOL(regExpMatch(regExp, STRDEF("bcdef")), false, "no match regexp");
TEST_RESULT_PTR(regExpMatchPtr(regExp), NULL, "check ptr");
TEST_RESULT_UINT(regExpMatchSize(regExp), 0, "check size");
TEST_RESULT_STR(regExpMatchStr(regExp), NULL, "check str");
TEST_RESULT_PTR(regExpMatchPtr(regExp, STRDEF("bcdef")), NULL, "check ptr");
TEST_RESULT_STR(regExpMatchStr(regExp, STRDEF("bcdef")), NULL, "check str");
// -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("free regexp");