Skip to content

Instantly share code, notes, and snippets.

@stakhiv
Created April 11, 2015 07:54
Show Gist options
  • Save stakhiv/2fec1bcd3ec7052d43f8 to your computer and use it in GitHub Desktop.
Save stakhiv/2fec1bcd3ec7052d43f8 to your computer and use it in GitHub Desktop.
package bench
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os"
)
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)
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
fmt.Fprintf(&res, "%v:%v", ln, position)
} else {
fmt.Fprintf(&res, ",%v:%v", ln, position)
}
}
}
file.Close()
return res.String(), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment