Created
April 1, 2015 12:12
-
-
Save jmank88/e4892b2ef752ccc07340 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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