Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove reflection dependency #91

Merged
merged 1 commit into from May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions constraint.go
Expand Up @@ -2,7 +2,6 @@ package version

import (
"fmt"
"reflect"
"regexp"
"sort"
"strings"
Expand Down Expand Up @@ -193,7 +192,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
Expand Up @@ -3,7 +3,6 @@ package version
import (
"bytes"
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -117,11 +116,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 @@ -137,6 +133,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 @@ -180,6 +178,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