line_reader.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2013 ChaiShushan <chaishushan{AT}gmail.com>. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package po
  5. import (
  6. "io"
  7. "strings"
  8. )
  9. type lineReader struct {
  10. lines []string
  11. pos int
  12. }
  13. func newLineReader(data string) *lineReader {
  14. data = strings.Replace(data, "\r", "", -1)
  15. lines := strings.Split(data, "\n")
  16. return &lineReader{lines: lines}
  17. }
  18. func (r *lineReader) skipBlankLine() error {
  19. for ; r.pos < len(r.lines); r.pos++ {
  20. if strings.TrimSpace(r.lines[r.pos]) != "" {
  21. break
  22. }
  23. }
  24. if r.pos >= len(r.lines) {
  25. return io.EOF
  26. }
  27. return nil
  28. }
  29. func (r *lineReader) currentPos() int {
  30. return r.pos
  31. }
  32. func (r *lineReader) currentLine() (s string, pos int, err error) {
  33. if r.pos >= len(r.lines) {
  34. err = io.EOF
  35. return
  36. }
  37. s, pos = r.lines[r.pos], r.pos
  38. return
  39. }
  40. func (r *lineReader) readLine() (s string, pos int, err error) {
  41. if r.pos >= len(r.lines) {
  42. err = io.EOF
  43. return
  44. }
  45. s, pos = r.lines[r.pos], r.pos
  46. r.pos++
  47. return
  48. }
  49. func (r *lineReader) unreadLine() {
  50. if r.pos >= 0 {
  51. r.pos--
  52. }
  53. }