Skip to content

Instantly share code, notes, and snippets.

@jmank88
Created April 1, 2015 02:27
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/ed4d0eb4a9a0ec821ee5 to your computer and use it in GitHub Desktop.
Save jmank88/ed4d0eb4a9a0ec821ee5 to your computer and use it in GitHub Desktop.
package bench
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"strings"
)
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()
if err != nil {
if err == io.EOF {
break
}
return "", err
}
colOffset := 0
for col := strings.Index(line, s); col > -1; col = strings.Index(line[colOffset:], s) {
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