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.7 kB

11 months ago
11 months ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. }
  64. func (r *Range) Equals(other Range) bool {
  65. if r.Offset != other.Offset {
  66. return false
  67. }
  68. if r.Length == nil && other.Length == nil {
  69. return true
  70. }
  71. if r.Length == nil || other.Length == nil {
  72. return false
  73. }
  74. return *r.Length == *other.Length
  75. }