Created
April 11, 2015 06:58
-
-
Save stakhiv/9a2a5d8de940325396c4 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) | |
defer file.Close() | |
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(s) | |
) | |
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 s[i] != line[position+i] { | |
continue Loop | |
} | |
} | |
if first { | |
first = false | |
res.WriteString(fmt.Sprintf("%v:%v", ln, position)) | |
} else { | |
res.WriteString(fmt.Sprintf(",%v:%v", ln, position)) | |
} | |
} | |
} | |
return res.String(), nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment