Created
April 11, 2015 07:14
-
-
Save stakhiv/42b6f24aaf283d361815 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" | |
"bytes" | |
"errors" | |
"fmt" | |
"io" | |
"os" | |
) | |
func Find(path, s string) (string, error) { | |
if s == "" { | |
return "", errors.New("") | |
} | |
var err error | |
var file *os.File | |
file, err = os.Open(path) | |
if err != nil { | |
return "", err | |
} | |
defer file.Close() | |
var ( | |
r *bufio.Reader = bufio.NewReader(file) | |
line []byte | |
position int | |
skip int | |
res bytes.Buffer | |
ln int | |
first bool = true | |
lineLength int | |
sLength int = len(s) | |
length int = sLength - 1 | |
) | |
var skipTable map[byte]int = make(map[byte]int, sLength-1) | |
for skip = 0; skip < length; skip++ { | |
skipTable[s[skip]] = length - skip | |
} | |
for { | |
line, err = r.ReadBytes('\n') | |
if err == io.EOF { | |
break | |
} | |
ln++ | |
lineLength = len(line) | |
skip = 0 | |
Loop: | |
for lineLength-skip >= sLength { | |
position = length | |
for line[skip+position] == s[position] { | |
if position == 0 { | |
if first { | |
first = false | |
res.WriteString(fmt.Sprintf("%v:%v", ln, skip+position)) | |
} else { | |
res.WriteString(fmt.Sprintf(",%v:%v", ln, skip+position)) | |
} | |
skip++ | |
continue Loop | |
} | |
position-- | |
} | |
z, ok := skipTable[line[skip+length]] | |
if !ok { | |
z = sLength | |
} | |
skip = skip + z | |
} | |
} | |
return res.String(), nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment