You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.go 1.4 kB

11 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package exec
  2. import (
  3. "github.com/google/uuid"
  4. "gitlink.org.cn/cloudream/common/utils/math2"
  5. )
  6. func genRandomPlanID() PlanID {
  7. return PlanID(uuid.NewString())
  8. }
  9. type Range struct {
  10. Offset int64
  11. Length *int64
  12. }
  13. func NewRange(offset int64, length int64) Range {
  14. return Range{Offset: offset, Length: &length}
  15. }
  16. func (r *Range) Extend(other Range) {
  17. newOffset := math2.Min(r.Offset, other.Offset)
  18. if r.Length == nil {
  19. r.Offset = newOffset
  20. return
  21. }
  22. if other.Length == nil {
  23. r.Offset = newOffset
  24. r.Length = nil
  25. return
  26. }
  27. otherEnd := other.Offset + *other.Length
  28. rEnd := r.Offset + *r.Length
  29. newEnd := math2.Max(otherEnd, rEnd)
  30. r.Offset = newOffset
  31. *r.Length = newEnd - newOffset
  32. }
  33. func (r *Range) ExtendStart(start int64) {
  34. r.Offset = math2.Min(r.Offset, start)
  35. }
  36. func (r *Range) ExtendEnd(end int64) {
  37. if r.Length == nil {
  38. return
  39. }
  40. rEnd := r.Offset + *r.Length
  41. newLen := math2.Max(end, rEnd) - r.Offset
  42. r.Length = &newLen
  43. }
  44. func (r *Range) Fix(maxLength int64) {
  45. if r.Length != nil {
  46. return
  47. }
  48. len := maxLength - r.Offset
  49. r.Length = &len
  50. }
  51. func (r *Range) ToStartEnd(maxLen int64) (start int64, end int64) {
  52. if r.Length == nil {
  53. return r.Offset, maxLen
  54. }
  55. end = r.Offset + *r.Length
  56. return r.Offset, end
  57. }
  58. func (r *Range) ClampLength(maxLen int64) {
  59. if r.Length == nil {
  60. return
  61. }
  62. *r.Length = math2.Min(*r.Length, maxLen-r.Offset)
  63. }