github.com/puellanivis/breton@v0.2.16/lib/net/hls/m3u8/byterange.go (about) 1 package m3u8 2 3 import ( 4 "bytes" 5 "fmt" 6 "strconv" 7 ) 8 9 // ByteRange implements the BYTERANGE attribute of the m3u8 format. 10 type ByteRange struct { 11 Length int 12 Offset int 13 } 14 15 // TextUnmarshal implements encoding.TextUnmarshaler 16 func (r *ByteRange) TextUnmarshal(value []byte) error { 17 fields := bytes.Split(value, []byte{'@'}) 18 19 var offset int 20 21 switch len(fields) { 22 case 1: 23 case 2: 24 var err error 25 offset, err = strconv.Atoi(string(fields[1])) 26 if err != nil { 27 return fmt.Errorf("invalid offset in BYTERANGE: %q: %v", fields[1], err) 28 } 29 30 default: 31 return fmt.Errorf("invalid BYTERANGE: %q", value) 32 } 33 34 length, err := strconv.Atoi(string(fields[0])) 35 if err != nil { 36 return fmt.Errorf("invalid length in BYTERANGE: %q: %v", fields[0], err) 37 } 38 39 r.Length = length 40 r.Offset = offset 41 42 return nil 43 } 44 45 // TextMarshal implements encoding.TextMarshal. 46 func (r ByteRange) TextMarshal() ([]byte, error) { 47 return []byte(r.String()), nil 48 } 49 50 // String implements fmt.Stringer. 51 func (r ByteRange) String() string { 52 if r.Offset != 0 { 53 return fmt.Sprintf("%d@%d", r.Length, r.Offset) 54 } 55 56 return strconv.Itoa(r.Length) 57 }