github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/ranges/range.go (about) 1 // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. 2 3 package rangeutils 4 5 import "strconv" 6 7 type Range [2]int64 8 9 func NewRange(start int64, end int64) Range { 10 return [2]int64{start, end} 11 } 12 13 func (this Range) Start() int64 { 14 return this[0] 15 } 16 17 func (this Range) End() int64 { 18 return this[1] 19 } 20 21 func (this Range) Length() int64 { 22 return this[1] - this[0] + 1 23 } 24 25 func (this Range) Convert(total int64) (newRange Range, ok bool) { 26 if total <= 0 { 27 return this, false 28 } 29 if this[0] < 0 { 30 this[0] += total 31 if this[0] < 0 { 32 return this, false 33 } 34 this[1] = total - 1 35 } 36 if this[1] < 0 { 37 this[1] = total - 1 38 } 39 if this[1] > total-1 { 40 this[1] = total - 1 41 } 42 if this[0] > this[1] { 43 return this, false 44 } 45 46 return this, true 47 } 48 49 // ComposeContentRangeHeader 组合Content-Range Header 50 // totalSize 可能是一个数字,也可能是一个星号(*) 51 func (this Range) ComposeContentRangeHeader(totalSize string) string { 52 return "bytes " + strconv.FormatInt(this[0], 10) + "-" + strconv.FormatInt(this[1], 10) + "/" + totalSize 53 }