Пример #1
0
func (r *Regexp) findSubmatchIndex(rs []rune, ls []int) []int {
	rptr, err := r.validRegexpPtr()
	if err != nil {
		return nil
	}

	matchData := C.pcre2_match_data_create_from_pattern(rptr, nil)
	defer C.pcre2_match_data_free(matchData)

	out := []int(nil)
	options := 0

	count := r.matchRuneArray(rs, 0, options, matchData)
	if count <= 0 {
		return nil
	}

	ovector := pcre2GetOvectorPointer(matchData, count)
	for i := 0; i < count; i++ {
		ovec0 := int(ovector[2*i])
		b1 := 0
		for x := 0; x < ovec0; x++ {
			b1 += ls[x]
		}

		ovec1 := int(ovector[2*i+1])
		b2 := b1
		for x := ovec0; x < ovec1; x++ {
			b2 += ls[x]
		}
		out = append(out, []int{b1, b2}...)
	}

	return out
}
Пример #2
0
func (r *Regexp) findAllSubmatchIndex(rs []rune, ls []int, n int) [][]int {
	if n == 0 {
		return nil
	}

	rptr, err := r.validRegexpPtr()
	if err != nil {
		return nil
	}

	matchData := C.pcre2_match_data_create_from_pattern(rptr, nil)
	defer C.pcre2_match_data_free(matchData)

	out := [][]int(nil)
	offset := 0
	options := 0
	for len(rs) > 0 {
		count := r.matchRuneArray(rs, 0, options, matchData)
		if count <= 0 {
			break
		}

		ovector := pcre2GetOvectorPointer(matchData, count)
		curmatch := make([]int, 0, count)
		for i := 0; i < count; i++ {
			ovec2i := int(ovector[2*i])

			b1 := 0
			for x := 0; x < ovec2i; x++ {
				b1 += ls[x]
			}
			b2 := b1
			for x := ovec2i; x < int(ovector[2*i+1]); x++ {
				b2 += ls[x]
			}
			curmatch = append(curmatch, offset+b1, offset+b2)
		}
		out = append(out, curmatch)

		units := int(ovector[1])
		for x := 0; x < units; x++ {
			offset += ls[x]
		}

		rs = rs[units:]
		ls = ls[units:]

		if n > 0 && len(out) >= n {
			break
		}
	}

	return out
}
Пример #3
0
func (r *Regexp) matchRuneArray(rs []rune, offset int, options int, matchData *C.pcre2_match_data) int {
	rptr, err := r.validRegexpPtr()
	if err != nil {
		return -1
	}

	if matchData == nil {
		matchData = C.pcre2_match_data_create_from_pattern(rptr, nil)
		defer C.pcre2_match_data_free(matchData)
	}

	rc := C.pcre2_match(
		rptr,
		(C.PCRE2_SPTR)(unsafe.Pointer(&rs[0])),
		C.size_t(len(rs)),
		(C.PCRE2_SIZE)(offset),
		(C.uint32_t)(options),
		matchData,
		nil,
	)

	return int(rc)
}