Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ASCII-only option, to mimic default RE2 behavior #1

Merged
merged 9 commits into from
May 8, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
dist: trusty
language: go
go:
- '1.11.x'
- '1.12.x'

env:
global:
- LD_LIBRARY_PATH="/usr/local/lib":${LD_LIBRARY_PATH}
Expand All @@ -8,8 +13,11 @@ addons:
packages:
- libonig-dev

jobs:
include:
- go: 1.11.x
script:
- go test -v --cover -race
before_install:
- sudo apt-get install -y dpkg # dpkg >= 1.17.5ubuntu5.8, which fixes https://bugs.launchpad.net/ubuntu/+source/dpkg/+bug/1730627- sudo dpkg -i libonig-dev_6.9.1-1_amd64.deb
bzz marked this conversation as resolved.
Show resolved Hide resolved
- wget http://archive.ubuntu.com/ubuntu/pool/universe/libo/libonig/libonig5_6.9.1-1_amd64.deb
- sudo dpkg -i libonig5_6.9.1-1_amd64.deb
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need non dev version?

Copy link
Author

@bzz bzz May 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because it's a direct dependency of the dev version

- wget http://archive.ubuntu.com/ubuntu/pool/universe/libo/libonig/libonig-dev_6.9.1-1_amd64.deb
- sudo dpkg -i libonig-dev_6.9.1-1_amd64.deb
script:
- go test -v --cover -race
3 changes: 2 additions & 1 deletion chelper.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ int NewOnigRegex( char *pattern, int pattern_length, int option,
*error_info = (OnigErrorInfo *) malloc(sizeof(OnigErrorInfo));
memset(*error_info, 0, sizeof(OnigErrorInfo));

*encoding = (void*)ONIG_ENCODING_UTF8;
OnigEncoding use_encs[] = { *encoding };
onig_initialize(use_encs, sizeof(use_encs)/sizeof(use_encs[0]));
bzz marked this conversation as resolved.
Show resolved Hide resolved

*error_buffer = (char*) malloc(ONIG_MAX_ERROR_MESSAGE_LEN * sizeof(char));

Expand Down
27 changes: 23 additions & 4 deletions regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,23 @@ type Regexp struct {
}

func NewRegexp(pattern string, option int) (re *Regexp, err error) {
re = &Regexp{pattern: pattern}
patternCharPtr := C.CString(pattern)
defer C.free(unsafe.Pointer(patternCharPtr))
re = &Regexp{pattern: pattern, encoding: C.ONIG_ENCODING_UTF8}
bzz marked this conversation as resolved.
Show resolved Hide resolved
return initRegexp(re, option)
}

// NewRegexpASCII is equivalent of NewRegexp but matching only ASCII.
func NewRegexpASCII(pattern string, option int) (re *Regexp, err error) {
re = &Regexp{pattern: pattern, encoding: C.ONIG_ENCODING_ASCII}
bzz marked this conversation as resolved.
Show resolved Hide resolved
return initRegexp(re, option)
}

func initRegexp(re *Regexp, option int) (*Regexp, error) {
var err error
patternCharPtr := C.CString(re.pattern)
defer C.free(unsafe.Pointer(patternCharPtr))
mutex.Lock()
defer mutex.Unlock()
error_code := C.NewOnigRegex(patternCharPtr, C.int(len(pattern)), C.int(option), &re.regex, &re.region, &re.encoding, &re.errorInfo, &re.errorBuf)
error_code := C.NewOnigRegex(patternCharPtr, C.int(len(re.pattern)), C.int(option), &re.regex, &re.region, &re.encoding, &re.errorInfo, &re.errorBuf)
bzz marked this conversation as resolved.
Show resolved Hide resolved
if error_code != C.ONIG_NORMAL {
err = errors.New(C.GoString(re.errorBuf))
} else {
Expand Down Expand Up @@ -95,6 +105,15 @@ func MustCompileWithOption(str string, option int) *Regexp {
return regexp
}

// MustCompileASCII equivalent of MustCompile but with char matching only ASCII.
bzz marked this conversation as resolved.
Show resolved Hide resolved
func MustCompileASCII(str string) *Regexp {
regexp, error := NewRegexpASCII(str, ONIG_OPTION_DEFAULT)
if error != nil {
panic("regexp: compiling " + str + ": " + error.Error())
}
return regexp
}

func (re *Regexp) Free() {
mutex.Lock()
if re.regex != nil {
Expand Down