Skip to content

Instantly share code, notes, and snippets.

@jmank88
Created April 1, 2015 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmank88/e4892b2ef752ccc07340 to your computer and use it in GitHub Desktop.
Save jmank88/e4892b2ef752ccc07340 to your computer and use it in GitHub Desktop.
package bench
import (
"bufio"
"errors"
"fmt"
"os"
"strings"
"unicode/utf8"
)
const (
bufferSize = 100
)
func Find(path, s string) (string, error) {
if len(s) == 0 {
return "", errors.New("")
}
f, err := os.Open(path)
if err != nil {
return "", err
}
defer f.Close()
scanner := bufio.NewScanner(f)
row := 1
matches := make([]string, 0)
for scanner.Scan() {
line := scanner.Text()
colOffset := 0
first, _ := utf8.DecodeRuneInString(s)
for col := strings.IndexRune(line, first); col > -1 && line[colOffset+col:len(s)] == s; col = strings.IndexRune(line[colOffset:], first) {
matches = append(matches, fmt.Sprintf("%d:%d", row, col+colOffset))
colOffset += col + 1
}
row++
}
return strings.Join(matches, ","), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment