From 0685c657057037d460f4831a165120a6c234b347 Mon Sep 17 00:00:00 2001 From: "K.Kosako" Date: Sun, 25 Feb 2018 16:57:00 +0900 Subject: [PATCH] add callout_data member into OnigMatchParam --- src/oniguruma.h | 7 ++--- src/regexec.c | 68 +++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 61 insertions(+), 14 deletions(-) diff --git a/src/oniguruma.h b/src/oniguruma.h index 01e4cf5..9fccb94 100644 --- a/src/oniguruma.h +++ b/src/oniguruma.h @@ -746,7 +746,8 @@ typedef enum { #define ONIG_NO_NAME_ID -1 -#define ONIG_CALLOUT_MAX_ARG_NUM 4 +#define ONIG_CALLOUT_MAX_ARG_NUM 4 +#define ONIG_CALLOUT_DATA_SLOT_NUM 5 struct OnigCalloutArgsStruct; typedef struct OnigCalloutArgsStruct OnigCalloutArgs; @@ -912,11 +913,11 @@ const char* onig_copyright P_((void)); /* for OnigMatchParam */ ONIG_EXTERN -OnigMatchParam* onig_new_match_param P_((void)); +OnigMatchParam* onig_new_match_param P_((OnigRegex reg)); ONIG_EXTERN void onig_free_match_param P_((OnigMatchParam* p)); ONIG_EXTERN -void onig_initialize_match_param P_((OnigMatchParam* mp)); +int onig_initialize_match_param P_((OnigRegex reg, OnigMatchParam* mp)); ONIG_EXTERN int onig_set_match_stack_limit_size_of_match_param P_((OnigMatchParam* param, unsigned int limit)); ONIG_EXTERN diff --git a/src/regexec.c b/src/regexec.c index d154a8d..1d1fc51 100644 --- a/src/regexec.c +++ b/src/regexec.c @@ -39,13 +39,22 @@ #define CHECK_INTERRUPT_IN_MATCH +#ifdef USE_CALLOUT +typedef struct { + OnigType t; + OnigValue v; +} CalloutData; +#endif struct OnigMatchParamStruct { unsigned int match_stack_limit; unsigned long retry_limit_in_match; OnigCalloutFunc callout_of_contents; OnigCalloutFunc retraction_callout_of_contents; +#ifdef USE_CALLOUT void* callout_user_data; + CalloutData* callout_data; +#endif }; extern int @@ -1136,26 +1145,40 @@ static OnigCalloutFunc DefaultCallout; static OnigCalloutFunc DefaultRetractionCallout; extern OnigMatchParam* -onig_new_match_param(void) +onig_new_match_param(regex_t* reg) { OnigMatchParam* p; p = (OnigMatchParam* )xmalloc(sizeof(*p)); if (IS_NOT_NULL(p)) { - onig_initialize_match_param(p); + onig_initialize_match_param(reg, p); } return p; } +static void +free_match_param_content(OnigMatchParam* p) +{ +#ifdef USE_CALLOUT + if (IS_NOT_NULL(p->callout_data)) { + xfree(p->callout_data); + p->callout_data = 0; + } +#endif +} + extern void onig_free_match_param(OnigMatchParam* p) { - if (IS_NOT_NULL(p)) xfree(p); + if (IS_NOT_NULL(p)) { + free_match_param_content(p); + xfree(p); + } } -extern void -onig_initialize_match_param(OnigMatchParam* mp) +extern int +onig_initialize_match_param(regex_t* reg, OnigMatchParam* mp) { mp->match_stack_limit = MatchStackLimit; #ifdef USE_RETRY_LIMIT_IN_MATCH @@ -1163,7 +1186,23 @@ onig_initialize_match_param(OnigMatchParam* mp) #endif mp->callout_of_contents = DefaultCallout; mp->retraction_callout_of_contents = DefaultRetractionCallout; - mp->callout_user_data = 0; + +#ifdef USE_CALLOUT + mp->callout_user_data = 0; + mp->callout_data = 0; + if (IS_NOT_NULL(REG_EXTP(reg))) { + RegexExt* ext = REG_EXTP(reg); + if (ext->callout_num > 0) { + CalloutData* d; + int n = ONIG_CALLOUT_DATA_SLOT_NUM * ext->callout_num; + d = (CalloutData* )xmalloc(sizeof(*d) * n); + CHECK_NULL_RETURN_MEMERR(d); + mp->callout_data = d; + } + } +#endif + + return ONIG_NORMAL; } @@ -2098,7 +2137,7 @@ typedef struct { static int match_at(regex_t* reg, const UChar* str, const UChar* end, const UChar* in_right_range, const UChar* sstart, UChar* sprev, - MatchArg* msa) + MatchArg* msa) { static UChar FinishCode[] = { OP_FINISH }; @@ -4074,10 +4113,13 @@ extern int onig_match(regex_t* reg, const UChar* str, const UChar* end, const UChar* at, OnigRegion* region, OnigOptionType option) { + int r; OnigMatchParam mp; - onig_initialize_match_param(&mp); - return onig_match_with_param(reg, str, end, at, region, option, &mp); + onig_initialize_match_param(reg, &mp); + r = onig_match_with_param(reg, str, end, at, region, option, &mp); + free_match_param_content(&mp); + return r; } extern int @@ -4370,10 +4412,14 @@ onig_search(regex_t* reg, const UChar* str, const UChar* end, const UChar* start, const UChar* range, OnigRegion* region, OnigOptionType option) { + int r; OnigMatchParam mp; - onig_initialize_match_param(&mp); - return onig_search_with_param(reg, str, end, start, range, region, option, &mp); + onig_initialize_match_param(reg, &mp); + r = onig_search_with_param(reg, str, end, start, range, region, option, &mp); + free_match_param_content(&mp); + return r; + } extern int -- 2.40.0