From 9c8424ed7c79e3e0d89f7e22fedb1eb411c7ba83 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Wed, 6 May 2020 16:52:01 +0200 Subject: [PATCH] Create new region for each regexp usage --- chelper.c | 22 +++++++++++++++------- chelper.h | 8 ++++---- regex.go | 11 +++-------- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/chelper.c b/chelper.c index d768a77..035f3b4 100644 --- a/chelper.c +++ b/chelper.c @@ -7,7 +7,7 @@ #include "chelper.h" int NewOnigRegex( char *pattern, int pattern_length, int option, - OnigRegex *regex, OnigRegion **region, OnigEncoding *encoding, OnigErrorInfo **error_info, char **error_buffer) { + OnigRegex *regex, OnigEncoding *encoding, OnigErrorInfo **error_info, char **error_buffer) { int ret = ONIG_NORMAL; int error_msg_len = 0; @@ -23,8 +23,6 @@ int NewOnigRegex( char *pattern, int pattern_length, int option, memset(*error_buffer, 0, ONIG_MAX_ERROR_MESSAGE_LEN * sizeof(char)); - *region = onig_region_new(); - ret = onig_new(regex, pattern_start, pattern_end, (OnigOptionType)(option), *encoding, OnigDefaultSyntax, *error_info); if (ret != ONIG_NORMAL) { @@ -38,9 +36,10 @@ int NewOnigRegex( char *pattern, int pattern_length, int option, } int SearchOnigRegex( void *str, int str_length, int offset, int option, - OnigRegex regex, OnigRegion *region, OnigErrorInfo *error_info, char *error_buffer, int *captures, int *numCaptures) { + OnigRegex regex, OnigErrorInfo *error_info, char *error_buffer, int *captures, int *numCaptures) { int ret = ONIG_MISMATCH; int error_msg_len = 0; + OnigRegion *region; #ifdef BENCHMARK_CHELP struct timeval tim1, tim2; long t; @@ -55,6 +54,8 @@ int SearchOnigRegex( void *str, int str_length, int offset, int option, gettimeofday(&tim1, NULL); #endif + region = onig_region_new(); + ret = onig_search(regex, str_start, str_end, search_start, search_end, region, option); if (ret < 0 && error_buffer != NULL) { error_msg_len = onig_error_code_to_str((unsigned char*)(error_buffer), ret, error_info); @@ -74,6 +75,8 @@ int SearchOnigRegex( void *str, int str_length, int offset, int option, *numCaptures = count; } + onig_region_free(region, 1); + #ifdef BENCHMARK_CHELP gettimeofday(&tim2, NULL); t = (tim2.tv_sec - tim1.tv_sec) * 1000000 + tim2.tv_usec - tim1.tv_usec; @@ -83,9 +86,10 @@ int SearchOnigRegex( void *str, int str_length, int offset, int option, } int MatchOnigRegex(void *str, int str_length, int offset, int option, - OnigRegex regex, OnigRegion *region) { + OnigRegex regex) { int ret = ONIG_MISMATCH; int error_msg_len = 0; + OnigRegion *region; #ifdef BENCHMARK_CHELP struct timeval tim1, tim2; long t; @@ -98,7 +102,9 @@ int MatchOnigRegex(void *str, int str_length, int offset, int option, #ifdef BENCHMARK_CHELP gettimeofday(&tim1, NULL); #endif + region = onig_region_new(); ret = onig_match(regex, str_start, str_end, search_start, region, option); + onig_region_free(region, 1); #ifdef BENCHMARK_CHELP gettimeofday(&tim2, NULL); t = (tim2.tv_sec - tim1.tv_sec) * 1000000 + tim2.tv_usec - tim1.tv_usec; @@ -108,8 +114,9 @@ int MatchOnigRegex(void *str, int str_length, int offset, int option, } int LookupOnigCaptureByName(char *name, int name_length, - OnigRegex regex, OnigRegion *region) { + OnigRegex regex) { int ret = ONIGERR_UNDEFINED_NAME_REFERENCE; + OnigRegion *region; #ifdef BENCHMARK_CHELP struct timeval tim1, tim2; long t; @@ -119,7 +126,9 @@ int LookupOnigCaptureByName(char *name, int name_length, #ifdef BENCHMARK_CHELP gettimeofday(&tim1, NULL); #endif + region = onig_region_new(); ret = onig_name_to_backref_number(regex, name_start, name_end, region); + onig_region_free(region, 1); #ifdef BENCHMARK_CHELP gettimeofday(&tim2, NULL); t = (tim2.tv_sec - tim1.tv_sec) * 1000000 + tim2.tv_usec - tim1.tv_usec; @@ -181,4 +190,3 @@ int GetCaptureNames(OnigRegex reg, void *buffer, int bufferSize, int* groupNumbe onig_foreach_name(reg, name_callback, (void* )&groupInfo); return groupInfo.bufferOffset; } - diff --git a/chelper.h b/chelper.h index 7926fc2..4d00e7f 100644 --- a/chelper.h +++ b/chelper.h @@ -1,14 +1,14 @@ #include extern int NewOnigRegex( char *pattern, int pattern_length, int option, - OnigRegex *regex, OnigRegion **region, OnigEncoding *encoding, OnigErrorInfo **error_info, char **error_buffer); + OnigRegex *regex, OnigEncoding *encoding, OnigErrorInfo **error_info, char **error_buffer); extern int SearchOnigRegex( void *str, int str_length, int offset, int option, - OnigRegex regex, OnigRegion *region, OnigErrorInfo *error_info, char *error_buffer, int *captures, int *numCaptures); + OnigRegex regex, OnigErrorInfo *error_info, char *error_buffer, int *captures, int *numCaptures); extern int MatchOnigRegex( void *str, int str_length, int offset, int option, - OnigRegex regex, OnigRegion *region); + OnigRegex regex); -extern int LookupOnigCaptureByName(char *name, int name_length, OnigRegex regex, OnigRegion *region); +extern int LookupOnigCaptureByName(char *name, int name_length, OnigRegex regex); extern int GetCaptureNames(OnigRegex regex, void *buffer, int bufferSize, int* groupNumbers); diff --git a/regex.go b/regex.go index 486412c..fbe661a 100644 --- a/regex.go +++ b/regex.go @@ -31,7 +31,6 @@ type NamedGroupInfo map[string]int type Regexp struct { pattern string regex C.OnigRegex - region *C.OnigRegion encoding C.OnigEncoding errorInfo *C.OnigErrorInfo errorBuf *C.char @@ -57,7 +56,7 @@ func initRegexp(re *Regexp, option int) (*Regexp, error) { mutex.Lock() defer mutex.Unlock() - errorCode := C.NewOnigRegex(patternCharPtr, C.int(len(re.pattern)), C.int(option), &re.regex, &re.region, &re.encoding, &re.errorInfo, &re.errorBuf) + errorCode := C.NewOnigRegex(patternCharPtr, C.int(len(re.pattern)), C.int(option), &re.regex, &re.encoding, &re.errorInfo, &re.errorBuf) if errorCode != C.ONIG_NORMAL { return re, errors.New(C.GoString(re.errorBuf)) } @@ -112,10 +111,6 @@ func (re *Regexp) Free() { C.onig_free(re.regex) re.regex = nil } - if re.region != nil { - C.onig_region_free(re.region, 1) - re.region = nil - } mutex.Unlock() if re.errorInfo != nil { C.free(unsafe.Pointer(re.errorInfo)) @@ -184,7 +179,7 @@ func (re *Regexp) find(b []byte, n int, offset int) []int { pos := int(C.SearchOnigRegex( bytesPtr, C.int(n), C.int(offset), C.int(ONIG_OPTION_DEFAULT), - re.regex, re.region, re.errorInfo, (*C.char)(nil), (*C.int)(capturesPtr), (*C.int)(numCapturesPtr), + re.regex, re.errorInfo, (*C.char)(nil), (*C.int)(capturesPtr), (*C.int)(numCapturesPtr), )) if pos < 0 { @@ -222,7 +217,7 @@ func (re *Regexp) match(b []byte, n int, offset int) bool { bytesPtr := unsafe.Pointer(&b[0]) pos := int(C.SearchOnigRegex( bytesPtr, C.int(n), C.int(offset), C.int(ONIG_OPTION_DEFAULT), - re.regex, re.region, re.errorInfo, nil, nil, nil, + re.regex, re.errorInfo, nil, nil, nil, )) return pos >= 0