-
Notifications
You must be signed in to change notification settings - Fork 5
/
range.go
53 lines (43 loc) · 1.04 KB
/
range.go
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
48
49
50
51
52
53
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package rangeutils
import "strconv"
type Range [2]int64
func NewRange(start int64, end int64) Range {
return [2]int64{start, end}
}
func (this Range) Start() int64 {
return this[0]
}
func (this Range) End() int64 {
return this[1]
}
func (this Range) Length() int64 {
return this[1] - this[0] + 1
}
func (this Range) Convert(total int64) (newRange Range, ok bool) {
if total <= 0 {
return this, false
}
if this[0] < 0 {
this[0] += total
if this[0] < 0 {
return this, false
}
this[1] = total - 1
}
if this[1] < 0 {
this[1] = total - 1
}
if this[1] > total-1 {
this[1] = total - 1
}
if this[0] > this[1] {
return this, false
}
return this, true
}
// ComposeContentRangeHeader 组合Content-Range Header
// totalSize 可能是一个数字,也可能是一个星号(*)
func (this Range) ComposeContentRangeHeader(totalSize string) string {
return "bytes " + strconv.FormatInt(this[0], 10) + "-" + strconv.FormatInt(this[1], 10) + "/" + totalSize
}