Skip to content

Commit aecf426

Browse files
Merge pull request #23048 from JuliaLang/sk/ver32
VersionNumber: make number type platform-independent (UInt32)
2 parents a62160e + 52d0aff commit aecf426

File tree

3 files changed

+42
-30
lines changed

3 files changed

+42
-30
lines changed

base/pkg/resolve/versionweight.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ struct VWPreBuildItem
7474
i::Int
7575
end
7676
VWPreBuildItem() = VWPreBuildItem(0, HierarchicalValue(Int), 0)
77-
VWPreBuildItem(i::Int) = VWPreBuildItem(1, HierarchicalValue(Int), i)
77+
VWPreBuildItem(i::Integer) = VWPreBuildItem(1, HierarchicalValue(Int), i)
7878
VWPreBuildItem(s::String) = VWPreBuildItem(1, HierarchicalValue(Int[s...]), 0)
7979

8080
Base.zero(::Type{VWPreBuildItem}) = VWPreBuildItem()
@@ -105,7 +105,7 @@ end
105105

106106
const _vwprebuild_zero = VWPreBuild(0, HierarchicalValue(VWPreBuildItem))
107107

108-
function VWPreBuild(ispre::Bool, desc::Tuple{Vararg{Union{Int,String}}})
108+
function VWPreBuild(ispre::Bool, desc::Tuple{Vararg{Union{Integer,String}}})
109109
isempty(desc) && return _vwprebuild_zero
110110
desc == ("",) && return VWPreBuild(ispre ? -1 : 1, HierarchicalValue(VWPreBuildItem[]))
111111
nonempty = ispre ? -1 : 0

base/version.jl

+36-28
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22

33
## semantic version numbers (http://semver.org)
44

5+
const VInt = UInt32
6+
57
struct VersionNumber
6-
major::Int
7-
minor::Int
8-
patch::Int
9-
prerelease::Tuple{Vararg{Union{Int,String}}}
10-
build::Tuple{Vararg{Union{Int,String}}}
11-
12-
function VersionNumber(major::Int, minor::Int, patch::Int,
13-
pre::Tuple{Vararg{Union{Int,String}}},
14-
bld::Tuple{Vararg{Union{Int,String}}})
8+
major::VInt
9+
minor::VInt
10+
patch::VInt
11+
prerelease::Tuple{Vararg{Union{UInt64,String}}}
12+
build::Tuple{Vararg{Union{UInt64,String}}}
13+
14+
function VersionNumber(major::VInt, minor::VInt, patch::VInt,
15+
pre::Tuple{Vararg{Union{UInt64,String}}},
16+
bld::Tuple{Vararg{Union{UInt64,String}}})
1517
major >= 0 || throw(ArgumentError("invalid negative major version: $major"))
1618
minor >= 0 || throw(ArgumentError("invalid negative minor version: $minor"))
1719
patch >= 0 || throw(ArgumentError("invalid negative patch version: $patch"))
1820
for ident in pre
19-
if isa(ident,Int)
21+
if ident isa Integer
2022
ident >= 0 || throw(ArgumentError("invalid negative pre-release identifier: $ident"))
2123
else
2224
if !ismatch(r"^(?:|[0-9a-z-]*[a-z-][0-9a-z-]*)$"i, ident) ||
@@ -26,7 +28,7 @@ struct VersionNumber
2628
end
2729
end
2830
for ident in bld
29-
if isa(ident,Int)
31+
if ident isa Integer
3032
ident >= 0 || throw(ArgumentError("invalid negative build identifier: $ident"))
3133
else
3234
if !ismatch(r"^(?:|[0-9a-z-]*[a-z-][0-9a-z-]*)$"i, ident) ||
@@ -41,9 +43,9 @@ end
4143
VersionNumber(major::Integer, minor::Integer = 0, patch::Integer = 0,
4244
pre::Tuple{Vararg{Union{Integer,AbstractString}}} = (),
4345
bld::Tuple{Vararg{Union{Integer,AbstractString}}} = ()) =
44-
VersionNumber(Int(major), Int(minor), Int(patch),
45-
map(x->isa(x,Integer) ? Int(x) : String(x), pre),
46-
map(x->isa(x,Integer) ? Int(x) : String(x), bld))
46+
VersionNumber(VInt(major), VInt(minor), VInt(patch),
47+
map(x->x isa Integer ? UInt64(x) : String(x), pre),
48+
map(x->x isa Integer ? UInt64(x) : String(x), bld))
4749

4850
function print(io::IO, v::VersionNumber)
4951
v == typemax(VersionNumber) && return print(io, "")
@@ -82,7 +84,7 @@ function split_idents(s::AbstractString)
8284
idents = split(s, '.')
8385
ntuple(length(idents)) do i
8486
ident = idents[i]
85-
ismatch(r"^\d+$", ident) ? parse(Int, ident) : String(ident)
87+
ismatch(r"^\d+$", ident) ? parse(UInt64, ident) : String(ident)
8688
end
8789
end
8890

@@ -91,9 +93,9 @@ function VersionNumber(v::AbstractString)
9193
m = match(VERSION_REGEX, v)
9294
m === nothing && throw(ArgumentError("invalid version string: $v"))
9395
major, minor, patch, minus, prerl, plus, build = m.captures
94-
major = parse(Int, major)
95-
minor = minor !== nothing ? parse(Int, minor) : 0
96-
patch = patch !== nothing ? parse(Int, patch) : 0
96+
major = parse(VInt, major)
97+
minor = minor !== nothing ? parse(VInt, minor) : VInt(0)
98+
patch = patch !== nothing ? parse(VInt, patch) : VInt(0)
9799
if prerl !== nothing && !isempty(prerl) && prerl[1] == '-'
98100
prerl = prerl[2:end] # strip leading '-'
99101
end
@@ -107,15 +109,21 @@ convert(::Type{VersionNumber}, v::AbstractString) = VersionNumber(v)
107109
macro v_str(v); VersionNumber(v); end
108110

109111
typemin(::Type{VersionNumber}) = v"0-"
110-
typemax(::Type{VersionNumber}) = VersionNumber(typemax(Int),typemax(Int),typemax(Int),(),("",))
111112

112-
ident_cmp(a::Int, b::Int) = cmp(a,b)
113-
ident_cmp(a::Int, b::String) = isempty(b) ? +1 : -1
114-
ident_cmp(a::String, b::Int) = isempty(a) ? -1 : +1
115-
ident_cmp(a::String, b::String) = cmp(a,b)
113+
function typemax(::Type{VersionNumber})
114+
= typemax(VInt)
115+
VersionNumber(∞, ∞, ∞, (), ("",))
116+
end
117+
118+
ident_cmp(a::Integer, b::Integer) = cmp(a, b)
119+
ident_cmp(a::Integer, b::String ) = isempty(b) ? +1 : -1
120+
ident_cmp(a::String, b::Integer) = isempty(a) ? -1 : +1
121+
ident_cmp(a::String, b::String ) = cmp(a, b)
116122

117-
function ident_cmp(A::Tuple{Vararg{Union{Int,String}}},
118-
B::Tuple{Vararg{Union{Int,String}}})
123+
function ident_cmp(
124+
A::Tuple{Vararg{Union{Integer,String}}},
125+
B::Tuple{Vararg{Union{Integer,String}}},
126+
)
119127
i = start(A)
120128
j = start(B)
121129
while !done(A,i) && !done(B,i)
@@ -132,8 +140,8 @@ function ==(a::VersionNumber, b::VersionNumber)
132140
(a.major != b.major) && return false
133141
(a.minor != b.minor) && return false
134142
(a.patch != b.patch) && return false
135-
(ident_cmp(a.prerelease,b.prerelease) != 0) && return false
136-
(ident_cmp(a.build,b.build) != 0) && return false
143+
(ident_cmp(a.prerelease, b.prerelease) != 0) && return false
144+
(ident_cmp(a.build, b.build) != 0) && return false
137145
return true
138146
end
139147

@@ -186,7 +194,7 @@ function check_new_version(existing::Vector{VersionNumber}, ver::VersionNumber)
186194
end
187195
error("$ver is not a valid initial version (try 0.0.0, 0.0.1, 0.1 or 1.0)")
188196
end
189-
idx = searchsortedlast(existing,ver)
197+
idx = searchsortedlast(existing, ver)
190198
prv = existing[idx]
191199
ver == prv && error("version $ver already exists")
192200
nxt = thismajor(ver) != thismajor(prv) ? nextmajor(prv) :

test/version.jl

+4
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ show(io,v"4.3.2+1.a")
9595
# typemin and typemax
9696
@test typemin(VersionNumber) == v"0-"
9797
@test typemax(VersionNumber) == v""
98+
let= typemax(UInt32)
99+
@test typemin(VersionNumber) == VersionNumber(0, 0, 0, ("",), ())
100+
@test typemax(VersionNumber) == VersionNumber(∞, ∞, ∞, (), ("",))
101+
end
98102

99103
# issupbuild
100104
import Base.issupbuild

0 commit comments

Comments
 (0)