Skip to content

Commit

Permalink
Merge branch 'performance-improvements' into fancybits
Browse files Browse the repository at this point in the history
  • Loading branch information
eric committed Jul 31, 2021
2 parents d3495b3 + 8935516 commit df782a9
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions reader.go
Expand Up @@ -91,19 +91,19 @@ func (p *MasterPlaylist) decode(buf *bytes.Buffer, strict bool) error {
// Decode parses a media playlist passed from the buffer. If `strict`
// parameter is true then return first syntax error.
func (p *MediaPlaylist) Decode(data bytes.Buffer, strict bool) error {
return p.decode(&data, strict)
return p.decode(data.String(), strict)
}

// DecodeFrom parses a media playlist passed from the io.Reader
// stream. If `strict` parameter is true then it returns first syntax
// error.
func (p *MediaPlaylist) DecodeFrom(reader io.Reader, strict bool) error {
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(reader)
buf := new(strings.Builder)
_, err := io.Copy(buf, reader)
if err != nil {
return err
}
return p.decode(buf, strict)
return p.decode(buf.String(), strict)
}

// WithCustomDecoders adds custom tag decoders to the media playlist for decoding
Expand All @@ -118,26 +118,31 @@ func (p *MediaPlaylist) WithCustomDecoders(customDecoders []CustomDecoder) Playl
return p
}

func (p *MediaPlaylist) decode(buf *bytes.Buffer, strict bool) error {
func (p *MediaPlaylist) decode(buf string, strict bool) error {
var eof bool
var line string
var err error
var start, pos int

state := new(decodingState)
wv := new(WV)

for !eof {
if line, err = buf.ReadString('\n'); err == io.EOF {
pos = strings.IndexByte(buf[start:], '\n')
if pos == -1 {
eof = true
} else if err != nil {
break
line = buf[start:]
} else {
line = buf[start : start+pos]

// Move past the newline
start += pos + 1
}

err = decodeLineOfMediaPlaylist(p, wv, state, line, strict)
if strict && err != nil {
return err
}

}
if state.tagWV {
p.WV = wv
Expand Down

0 comments on commit df782a9

Please sign in to comment.