Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bench
- import (
- "bufio"
- "bytes"
- "errors"
- "io"
- "os"
- "strconv"
- )
- func Find(path, s string) (string, error) {
- if s == "" {
- return "", errors.New("")
- }
- var term []byte = []byte(s)
- var err error
- var file *os.File
- file, err = os.Open(path)
- defer file.Close()
- if err != nil {
- return "", err
- }
- var (
- r *bufio.Reader = bufio.NewReader(file)
- line []byte
- position int
- i int
- res bytes.Buffer
- ln int
- first bool = true
- lineLength int
- sLength int = len(term)
- )
- for {
- line, err = r.ReadBytes('\n')
- if err == io.EOF {
- break
- }
- ln++
- lineLength = len(line)
- Loop:
- for position = 0; position < lineLength; position++ {
- for i = 0; i < sLength; i++ {
- if term[i] != line[position+i] {
- continue Loop
- }
- }
- if first {
- first = false
- b := make([]byte, 0)
- b = strconv.AppendInt(b, int64(ln), 10)
- b = append(b, ':')
- b = strconv.AppendInt(b, int64(position), 10)
- res.Write(b)
- } else {
- b := make([]byte, 1)
- b[0] = ','
- b = strconv.AppendInt(b, int64(ln), 10)
- b = append(b, ':')
- b = strconv.AppendInt(b, int64(position), 10)
- res.Write(b)
- }
- }
- }
- return res.String(), nil
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement