file/edit.go
func (e *edit) genReplace(r []Replacer, n int) error {
	if n == 0 {
		return nil
	}
	if _, err := e.file.Seek(0, os.SEEK_SET); err != nil {
		return err
	}

	content, err := ioutil.ReadAll(e.buf)
	if err != nil {
		return err
	}

	isNew := false

	for _, v := range r {
		reSearch, err := regexp.Compile(v.Search)
		if err != nil {
			return err
		}

		i := n
		repl := []byte(v.Replace)

		content = reSearch.ReplaceAllFunc(content, func(s []byte) []byte {
			if !isNew {
				isNew = true
			}

			if i != 0 {
				i--
				return repl
			}
			return s
		})
	}

	if isNew {
		return e.rewrite(content)
	}
	return nil
}
func (e *edit) genReplace(r []Replacer, n int) error {
	if n == 0 {
		return nil
	}
	if _, err := e.file.Seek(0, os.SEEK_SET); err != nil {
		return err
	}

	content, err := ioutil.ReadAll(e.buf)
	if err != nil {
		return err
	}

	isNew := false

	for _, v := range r {
		reSearch, err := regexp.Compile(v.Search)
		if err != nil {
			return err
		}

		i := n
		repl := []byte(v.Replace)

		content = reSearch.ReplaceAllFunc(content, func(s []byte) []byte {
			if !isNew {
				isNew = true
			}

			if i != 0 {
				i--
				return repl
			}
			return s
		})
	}

	if isNew {
		return e.rewrite(content)
	}
	return nil
}
func ( e * edit ) genReplace ( r [ ] Replacer , n int ) error { if n == 0 { return nil } if _ , err := e . file . Seek ( 0 , os . SEEK_SET ) ; err != nil { return err } content , err := ioutil . ReadAll ( e . buf ) if err != nil { return err } isNew := false for _ , v := range r { reSearch , err := regexp . Compile ( v . Search ) if err != nil { return err } i := n repl := [ ] byte ( v . Replace ) content = reSearch . ReplaceAllFunc ( content , func ( s [ ] byte ) [ ] byte { if ! isNew { isNew = true } if i != 0 { i -- return repl } return s } ) } if isNew { return e . rewrite ( content ) } return nil }
// Generic Replace: replaces a number of regular expressions matched in r.
Generic Replace : replaces a number of regular expressions matched in r .