Beispiel #1
0
func (r reAddr) whereFrom(from int64, ed *Editor) (a addr, err error) {
	opts := re1.Options{Delimited: true, Reverse: r.rev}
	re, err := re1.Compile(strings.NewReader(r.re), opts)
	if err != nil {
		return a, err
	}
	fwd := &forward{Buffer: ed.buf.runes}
	rs := re1.Runes(fwd)
	if r.rev {
		rs = &reverse{fwd}
		from = rs.Size() - from
	}
	switch match := re.Match(rs, from); {
	case fwd.err != nil:
		return a, fwd.err
	case match == nil:
		return a, ErrNoMatch
	default:
		a = addr{from: match[0][0], to: match[0][1]}
		if r.rev {
			a.from, a.to = rs.Size()-a.to, rs.Size()-a.from
		}
		return a, nil
	}

}
Beispiel #2
0
func parseRegexp2(rs io.RuneScanner) ([]rune, error) {
	if err := skipSpace(rs); err != nil {
		return nil, err
	}
	rs1 := &re1Scanner{rs: rs}
	if _, err := re1.Compile(rs1, re1.Options{Delimited: true}); err != nil {
		return nil, err
	}
	return rs1.scanned, nil
}
Beispiel #3
0
func (e Substitute) do(ed *Editor, _ io.Writer) (addr, error) {
	at, err := e.A.where(ed)
	if err != nil {
		return addr{}, err
	}
	re, err := re1.Compile(strings.NewReader(e.RE), re1.Options{Delimited: true})
	if err != nil {
		return addr{}, err
	}
	from := at.from
	for {
		m, err := subSingle(ed, addr{from, at.to}, re, e.With, e.From)
		if err != nil {
			return addr{}, err
		}
		if !e.Global || m == nil || m[0][1] == at.to {
			break
		}
		from = m[0][1]
		e.From = 1 // reset n to 1, so that on future iterations of this loop we get the next instance.
	}
	return at, nil

}