Skip to content

Commit

Permalink
remove reflection dependency (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
f-kozlov committed May 21, 2024
1 parent 94bab9e commit e04a866
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
3 changes: 1 addition & 2 deletions constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package version

import (
"fmt"
"reflect"
"regexp"
"sort"
"strings"
Expand Down Expand Up @@ -202,7 +201,7 @@ func prereleaseCheck(v, c *Version) bool {
case cPre && vPre:
// A constraint with a pre-release can only match a pre-release version
// with the same base segments.
return reflect.DeepEqual(c.Segments64(), v.Segments64())
return v.equalSegments(c)

case !cPre && vPre:
// A constraint without a pre-release can only match a version without a
Expand Down
23 changes: 18 additions & 5 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package version
import (
"bytes"
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -120,11 +119,8 @@ func (v *Version) Compare(other *Version) int {
return 0
}

segmentsSelf := v.Segments64()
segmentsOther := other.Segments64()

// If the segments are the same, we must compare on prerelease info
if reflect.DeepEqual(segmentsSelf, segmentsOther) {
if v.equalSegments(other) {
preSelf := v.Prerelease()
preOther := other.Prerelease()
if preSelf == "" && preOther == "" {
Expand All @@ -140,6 +136,8 @@ func (v *Version) Compare(other *Version) int {
return comparePrereleases(preSelf, preOther)
}

segmentsSelf := v.Segments64()
segmentsOther := other.Segments64()
// Get the highest specificity (hS), or if they're equal, just use segmentSelf length
lenSelf := len(segmentsSelf)
lenOther := len(segmentsOther)
Expand Down Expand Up @@ -183,6 +181,21 @@ func (v *Version) Compare(other *Version) int {
return 0
}

func (v *Version) equalSegments(other *Version) bool {
segmentsSelf := v.Segments64()
segmentsOther := other.Segments64()

if len(segmentsSelf) != len(segmentsOther) {
return false
}
for i, v := range segmentsSelf {
if v != segmentsOther[i] {
return false
}
}
return true
}

func allZero(segs []int64) bool {
for _, s := range segs {
if s != 0 {
Expand Down

0 comments on commit e04a866

Please sign in to comment.