// Compile pattern with jit compilation. flagC is Compile flags, // flagS is study flag. func CompileJIT(pattern string, flagsC, flagsS int) (Regexp, error) { patternC := C.CString(pattern) defer C.free(unsafe.Pointer(patternC)) if clen := int(C.strlen(patternC)); clen != len(pattern) { return Regexp{}, fmt.Errorf("%s (%d): %s", pattern, clen, "NUL byte in pattern", ) } var errptr *C.char var erroffset C.int ptr := C.pcre_compile(patternC, C.int(flagsC), &errptr, &erroffset, nil) if ptr == nil { return Regexp{}, fmt.Errorf("%s (%d): %s", pattern, int(erroffset), C.GoString(errptr), ) } psize := pcresize(ptr) var re Regexp re.ptr = make([]byte, psize) C.memcpy(unsafe.Pointer(&re.ptr[0]), unsafe.Pointer(ptr), psize) errS := re.study(flagsS) if errS != nil { return re, fmt.Errorf("study error: %s", errS) } return re, nil }
// Try to compile the pattern. If an error occurs, the second return // value is non-nil. func Compile(pattern string, flags int) (Regexp, *CompileError) { pattern1 := C.CString(pattern) defer C.free(unsafe.Pointer(pattern1)) if clen := int(C.strlen(pattern1)); clen != len(pattern) { return Regexp{}, &CompileError{ Pattern: pattern, Message: "NUL byte in pattern", Offset: clen, } } var errptr *C.char var erroffset C.int ptr := C.pcre_compile(pattern1, C.int(flags), &errptr, &erroffset, nil) if ptr == nil { return Regexp{}, &CompileError{ Pattern: pattern, Message: C.GoString(errptr), Offset: int(erroffset), } } return toheap(ptr), nil }