Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. package bench
  2.  
  3. import (
  4. "bufio"
  5. "bytes"
  6. "errors"
  7. "io"
  8. "os"
  9. "strconv"
  10. )
  11.  
  12. func Find(path, s string) (string, error) {
  13. if s == "" {
  14. return "", errors.New("")
  15. }
  16. var term []byte = []byte(s)
  17. var err error
  18. var file *os.File
  19. file, err = os.Open(path)
  20. defer file.Close()
  21. if err != nil {
  22. return "", err
  23. }
  24. var (
  25. r *bufio.Reader = bufio.NewReader(file)
  26. line []byte
  27. position int
  28. i int
  29. res bytes.Buffer
  30. ln int
  31. first bool = true
  32. lineLength int
  33. sLength int = len(term)
  34. )
  35. for {
  36. line, err = r.ReadBytes('\n')
  37. if err == io.EOF {
  38. break
  39. }
  40. ln++
  41. lineLength = len(line)
  42. Loop:
  43. for position = 0; position < lineLength; position++ {
  44. for i = 0; i < sLength; i++ {
  45. if term[i] != line[position+i] {
  46. continue Loop
  47. }
  48. }
  49. if first {
  50. first = false
  51. b := make([]byte, 0)
  52. b = strconv.AppendInt(b, int64(ln), 10)
  53. b = append(b, ':')
  54. b = strconv.AppendInt(b, int64(position), 10)
  55. res.Write(b)
  56. } else {
  57. b := make([]byte, 1)
  58. b[0] = ','
  59. b = strconv.AppendInt(b, int64(ln), 10)
  60. b = append(b, ':')
  61. b = strconv.AppendInt(b, int64(position), 10)
  62. res.Write(b)
  63. }
  64. }
  65. }
  66. return res.String(), nil
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement