Пример #1
0
func (re *Regexp) find(b []byte, n int, offset int) (match []int) {
	if n == 0 {
		b = []byte{0}
	}
	ptr := unsafe.Pointer(&b[0])
	matchData := re.matchData
	capturesPtr := unsafe.Pointer(&(matchData.indexes[matchData.count][0]))
	numCaptures := int32(0)
	numCapturesPtr := unsafe.Pointer(&numCaptures)
	pos := int(C.SearchOnigRegex((ptr), 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)))
	if pos >= 0 {
		if numCaptures <= 0 {
			panic("cannot have 0 captures when processing a match")
		}
		match2 := matchData.indexes[matchData.count][:numCaptures*2]
		match = make([]int, len(match2))
		for i := range match2 {
			match[i] = int(match2[i])
		}
		numCapturesInPattern := int32(C.onig_number_of_captures(re.regex)) + 1
		if numCapturesInPattern != numCaptures {
			log.Fatalf("expected %d captures but got %d\n", numCapturesInPattern, numCaptures)
		}
	}
	return
}
Пример #2
0
func NewRegexp(pattern string, option int) (re *Regexp, err error) {
	re = &Regexp{pattern: pattern}
	patternCharPtr := C.CString(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)
	if error_code != C.ONIG_NORMAL {
		err = errors.New(C.GoString(re.errorBuf))
	} else {
		err = nil
		numCapturesInPattern := int(C.onig_number_of_captures(re.regex)) + 1
		re.matchData = &MatchData{}
		re.matchData.indexes = make([][]int32, numMatchStartSize)
		for i := 0; i < numMatchStartSize; i++ {
			re.matchData.indexes[i] = make([]int32, numCapturesInPattern*2)
		}
		re.namedGroupInfo = re.getNamedGroupInfo()
		runtime.SetFinalizer(re, (*Regexp).Free)
	}
	return re, err
}
Пример #3
0
func (re *Regexp) NumSubexp() int {
	return (int)(C.onig_number_of_captures(re.regex))
}