Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions normalization.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,13 @@ func formatPEP440(v pep440Version) string {
result.WriteString(trimLeadingZeros(v.epoch))
result.WriteByte('!')
}
for i, part := range v.release {
if i > 0 {
for rest := v.release; rest != ""; {
part, next, _ := strings.Cut(rest, ".")
result.WriteString(trimLeadingZeros(part))
if next != "" {
result.WriteByte('.')
}
result.WriteString(trimLeadingZeros(part))
rest = next
}
if v.hasPre {
result.WriteString([]string{"a", "b", "rc"}[v.preTag])
Expand All @@ -153,15 +155,17 @@ func formatPEP440(v pep440Version) string {
}
if len(v.local) > 0 {
result.WriteByte('+')
for i, part := range v.local {
if i > 0 {
result.WriteByte('.')
}
if part.isNum {
result.WriteString(trimLeadingZeros(part.s))
for rest := v.local; rest != ""; {
part, next := cutPEP440Local(rest)
if isDigits(part) {
result.WriteString(trimLeadingZeros(part))
} else {
result.WriteString(part.s)
result.WriteString(strings.ToLower(part))
}
if next != "" {
result.WriteByte('.')
}
rest = next
}
}
return result.String()
Expand Down
4 changes: 1 addition & 3 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1008,9 +1008,7 @@ func parsePypiPrefixRange(version string, exclude bool) (*Range, error) {
return nil, fmt.Errorf("invalid pypi prefix constraint: %s", version)
}
lower := prefix + ".dev0"
upperRelease := append([]string(nil), v.release...)
upperRelease[len(upperRelease)-1] = incNumStr(upperRelease[len(upperRelease)-1])
upper := strings.Join(upperRelease, ".") + ".dev0"
upper := bumpPEP440Release(v.release) + ".dev0"
if bang := strings.IndexByte(prefix, '!'); bang >= 0 {
epoch := trimLeadingZeros(v.epoch)
lower = epoch + "!" + prefix[bang+1:] + ".dev0"
Expand Down
248 changes: 154 additions & 94 deletions pypi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vers
import (
"regexp"
"strings"
"unicode/utf8"
)

// PEP 440 version parsing and comparison.
Expand Down Expand Up @@ -34,57 +35,139 @@ var pep440PreTags = map[string]int{
// converted to int.
type pep440Version struct {
epoch string
release []string
release string
hasPre bool
preTag int
preNum string
hasPost bool
post string
hasDev bool
dev string
local []pep440LocalPart
}

type pep440LocalPart struct {
s string
isNum bool
local string
}

func parsePEP440(s string) (pep440Version, bool) {
m := pep440Regex.FindStringSubmatch(s)
if m == nil {
return pep440Version{}, false
// (?i) folds non-ASCII letters such as the Kelvin sign; keep the regexp for those.
if !isASCIIString(s) {
return parsePEP440Regexp(s)
}
s = strings.Trim(s, " \t\r\n\f") // RE2's \s, not Unicode whitespace
if len(s) > 0 && (s[0] == 'v' || s[0] == 'V') {
s = s[1:]
}

v := pep440Version{epoch: m[1]}

v.release = strings.Split(m[2], ".")

if m[3] != "" {
v.hasPre = true
v.preTag = pep440PreTags[strings.ToLower(m[3])]
v.preNum = m[4]
var v pep440Version
first, rest := cutPEP440Digits(s)
if first == "" {
return pep440Version{}, false
}
if strings.HasPrefix(rest, "!") {
v.epoch = first
s = rest[1:]
first, rest = cutPEP440Digits(s)
if first == "" {
return pep440Version{}, false
}
}
for len(rest) >= 2 && rest[0] == '.' && isASCIIDigit(rest[1]) {
_, rest = cutPEP440Digits(rest[1:])
}
v.release = s[:len(s)-len(rest)]
if rest == "" {
return v, true
}

if m[5] != "" || m[7] != "" {
pre, n, remaining := cutPEP440Qualifier(rest, "preview", "alpha", "beta", "pre", "rc", "a", "b", "c")
if pre != "" {
v.hasPre, v.preTag, v.preNum, rest = true, pep440PreTags[pre], n, remaining
}
if len(rest) >= 2 && rest[0] == '-' && isASCIIDigit(rest[1]) {
v.hasPost = true
if m[7] != "" {
v.post = m[7]
v.post, rest = cutPEP440Digits(rest[1:])
} else if tag, n, remaining := cutPEP440Qualifier(rest, "post", "rev", "r"); tag != "" {
v.hasPost, v.post, rest = true, n, remaining
}
if tag, n, remaining := cutPEP440Qualifier(rest, "dev"); tag != "" {
v.hasDev, v.dev, rest = true, n, remaining
}
if rest == "" {
return v, true
}
if rest[0] != '+' || len(rest) == 1 {
return pep440Version{}, false
}
v.local = rest[1:]
needAlnum := true
for i := range len(v.local) {
c := v.local[i]
if isASCIIAlnum(c) {
needAlnum = false
} else if !needAlnum && isPEP440Separator(c) {
needAlnum = true
} else {
v.post = m[6]
return pep440Version{}, false
}
}
if needAlnum {
return pep440Version{}, false
}
return v, true
}

if m[8] != "" {
v.hasDev = true
v.dev = m[9]
func parsePEP440Regexp(s string) (pep440Version, bool) {
m := pep440Regex.FindStringSubmatch(s)
if m == nil {
return pep440Version{}, false
}
v := pep440Version{
epoch: m[1], release: m[2],
hasPre: m[3] != "", preTag: pep440PreTags[strings.ToLower(m[3])], preNum: m[4],
hasPost: m[5] != "" || m[7] != "", post: m[6],
hasDev: m[8] != "", dev: m[9],
local: m[10],
}
if m[7] != "" {
v.post = m[7]
}
return v, true
}

if m[10] != "" {
v.local = parsePEP440Local(m[10])
func isASCIIString(s string) bool {
for i := range len(s) {
if s[i] >= utf8.RuneSelf {
return false
}
}
return true
}

return v, true
func cutPEP440Digits(s string) (digits, rest string) {
i := 0
for i < len(s) && isASCIIDigit(s[i]) {
i++
}
return s[:i], s[i:]
}

func isPEP440Separator(c byte) bool { return c == '.' || c == '-' || c == '_' }

func cutPEP440Qualifier(s string, tags ...string) (tag, digits, rest string) {
input := s
if s != "" && isPEP440Separator(s[0]) {
s = s[1:]
}
for _, tag := range tags {
if len(s) < len(tag) || !strings.EqualFold(s[:len(tag)], tag) {
continue
}
s = s[len(tag):]
if s != "" && isPEP440Separator(s[0]) {
s = s[1:]
}
digits, rest = cutPEP440Digits(s)
return tag, digits, rest
}
return "", "", input
}

// pep440CompatibleUpper returns the exclusive upper bound for a ~= clause.
Expand All @@ -93,19 +176,22 @@ func parsePEP440(s string) (pep440Version, bool) {
// The bound is derived from release segments only and preserves the epoch.
func pep440CompatibleUpper(version string) (string, bool) {
v, ok := parsePEP440(version)
if !ok || len(v.release) < 2 { //nolint:mnd
last := strings.LastIndexByte(v.release, '.')
if !ok || last < 0 {
return "", false
}
head := make([]string, len(v.release)-1)
copy(head, v.release[:len(v.release)-1])
head[len(head)-1] = incNumStr(head[len(head)-1])
upper := strings.Join(head, ".")
upper := bumpPEP440Release(v.release[:last])
if cmpNumStr(v.epoch, "0") != 0 {
upper = trimLeadingZeros(v.epoch) + "!" + upper
}
return upper, true
}

func bumpPEP440Release(release string) string {
start := strings.LastIndexByte(release, '.') + 1
return release[:start] + incNumStr(release[start:])
}

// incNumStr returns the decimal string s+1 for a non-negative integer string.
func incNumStr(s string) string {
s = trimLeadingZeros(s)
Expand All @@ -120,17 +206,6 @@ func incNumStr(s string) string {
return "1" + string(b)
}

func parsePEP440Local(s string) []pep440LocalPart {
raw := strings.FieldsFunc(strings.ToLower(s), func(r rune) bool {
return r == '.' || r == '-' || r == '_'
})
parts := make([]pep440LocalPart, 0, len(raw))
for _, p := range raw {
parts = append(parts, pep440LocalPart{s: p, isNum: isDigits(p)})
}
return parts
}

func isDigits(s string) bool {
if s == "" {
return false
Expand All @@ -155,7 +230,7 @@ func comparePyPI(a, b string) int {
if c := cmpNumStr(va.epoch, vb.epoch); c != 0 {
return c
}
if c := cmpNumStrSlice(va.release, vb.release); c != 0 {
if c := compareDotNumbers(va.release, vb.release); c != 0 {
return c
}
if c := cmpPEP440Pre(va, vb); c != 0 {
Expand Down Expand Up @@ -247,65 +322,50 @@ func trimLeadingZeros(s string) string {
return s[i:]
}

func cmpNumStrSlice(a, b []string) int {
n := len(a)
if len(b) > n {
n = len(b)
}
for i := 0; i < n; i++ {
var x, y string
if i < len(a) {
x = a[i]
func cutPEP440Local(s string) (part, rest string) {
if i := strings.IndexAny(s, ".-_"); i >= 0 {
return s[:i], s[i+1:]
}
return s, ""
}

func cmpPEP440Local(a, b string) int {
for a != "" && b != "" {
pa, ra := cutPEP440Local(a)
pb, rb := cutPEP440Local(b)
na, nb := isDigits(pa), isDigits(pb)
if na != nb {
return cmpInt(boolInt(na), boolInt(nb)) // numeric labels sort last
}
if i < len(b) {
y = b[i]
var c int
if na {
c = cmpNumStr(pa, pb)
} else {
c = cmpPEP440LocalText(pa, pb)
}
if c := cmpNumStr(x, y); c != 0 {
if c != 0 {
return c
}
a, b = ra, rb
}
return 0
return cmpInt(boolInt(a != ""), boolInt(b != ""))
}

func cmpPEP440Local(a, b []pep440LocalPart) int {
// No local sorts before any local.
if len(a) == 0 && len(b) == 0 {
return 0
}
if len(a) == 0 {
return -1
}
if len(b) == 0 {
return 1
}
n := len(a)
if len(b) > n {
n = len(b)
}
for i := 0; i < n; i++ {
if i >= len(a) {
return -1
func cmpPEP440LocalText(a, b string) int {
for i := range min(len(a), len(b)) {
x, y := a[i], b[i]
if x >= utf8.RuneSelf || y >= utf8.RuneSelf {
return cmpString(strings.ToLower(a), strings.ToLower(b))
}
if i >= len(b) {
return 1
if x >= 'A' && x <= 'Z' {
x += 'a' - 'A'
}
pa, pb := a[i], b[i]
// Numeric segments sort after strings; within kind, natural order.
if pa.isNum != pb.isNum {
if pa.isNum {
return 1
}
return -1
if y >= 'A' && y <= 'Z' {
y += 'a' - 'A'
}
if pa.isNum {
if c := cmpNumStr(pa.s, pb.s); c != 0 {
return c
}
} else {
if c := cmpString(pa.s, pb.s); c != 0 {
return c
}
if x != y {
return cmpInt(int(x), int(y))
}
}
return 0
return cmpInt(len(a), len(b))
}
Loading