github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/video/subtitle/srt/line_reader.go (about)

     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  
     5  package srt
     6  
     7  import (
     8  	"io"
     9  	"strings"
    10  )
    11  
    12  type lineReader struct {
    13  	lines []string
    14  	pos   int
    15  }
    16  
    17  func newLineReader(data string) *lineReader {
    18  	data = strings.Replace(data, "\r", "", -1)
    19  	lines := strings.Split(data, "\n")
    20  	return &lineReader{lines: lines}
    21  }
    22  
    23  func (r *lineReader) AtEOF() bool {
    24  	return r.pos >= len(r.lines)
    25  }
    26  
    27  func (r *lineReader) CurrentPos() int {
    28  	return r.pos
    29  }
    30  
    31  func (r *lineReader) CurrentLine() string {
    32  	if !r.AtEOF() {
    33  		return r.lines[r.pos]
    34  	}
    35  	return ""
    36  }
    37  
    38  func (r *lineReader) Next() {
    39  	r.pos++
    40  }
    41  
    42  func (r *lineReader) ReadLine() (s string, err error) {
    43  	if r.pos >= len(r.lines) {
    44  		err = io.EOF
    45  		return
    46  	}
    47  	s = r.lines[r.pos]
    48  	r.pos++
    49  	return
    50  }
    51  
    52  func (r *lineReader) UnreadLine() {
    53  	if r.pos >= 0 {
    54  		r.pos--
    55  	}
    56  }
    57  
    58  func (r *lineReader) SkipBlankLine() {
    59  	for ; r.pos < len(r.lines); r.pos++ {
    60  		if strings.TrimSpace(r.lines[r.pos]) != "" {
    61  			break
    62  		}
    63  	}
    64  }
    65  
    66  func (r *lineReader) Reset() {
    67  	r.pos = 0
    68  }