File: byteRange.go

package info (click to toggle)
golang-github-etherlabsio-go-m3u8 1.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 348 kB
  • sloc: makefile: 3
file content (47 lines) | stat: -rw-r--r-- 802 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package m3u8

import (
	"fmt"
	"strconv"
	"strings"
)

// ByteRange represents sub range of a resource
type ByteRange struct {
	Length *int
	Start  *int
}

// NewByteRange parses a text line in playlist file and returns a *ByteRange
func NewByteRange(text string) (*ByteRange, error) {
	if text == "" {
		return nil, nil
	}

	values := strings.Split(text, "@")

	lengthValue, err := strconv.Atoi(values[0])
	if err != nil {
		return nil, err
	}

	br := ByteRange{Length: &lengthValue}

	if len(values) >= 2 {
		startValue, err := strconv.Atoi(values[1])
		if err != nil {
			return &br, err
		}
		br.Start = &startValue
	}

	return &br, nil
}

func (br *ByteRange) String() string {
	if br.Start == nil {
		return fmt.Sprintf("%d", *br.Length)
	}

	return fmt.Sprintf("%d@%d", *br.Length, *br.Start)
}