forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.cpp
More file actions
170 lines (143 loc) · 4.06 KB
/
version.cpp
File metadata and controls
170 lines (143 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#include <cassert>
#include "pal.h"
#include "version.h"
// This type matches semantics of System.Version used by tooling and differs from fx_ver_t:
// -- version_t does not require the third segment
// -- version_t does not support the "pre" label behavior
// -- if version_t is an empty value (-1 for all segments) then as_str() returns an empty string
// -- Different terminology; fx_ver_t(major, minor, patch, build) vs version_t(major, minor, build, revision)
version_t::version_t() : version_t(-1, -1, -1, -1) { }
version_t::version_t(int major, int minor, int build, int revision)
: m_major(major)
, m_minor(minor)
, m_build(build)
, m_revision(revision) { }
bool version_t::operator ==(const version_t& b) const
{
return compare(*this, b) == 0;
}
bool version_t::operator !=(const version_t& b) const
{
return !operator ==(b);
}
bool version_t::operator <(const version_t& b) const
{
return compare(*this, b) < 0;
}
bool version_t::operator >(const version_t& b) const
{
return compare(*this, b) > 0;
}
bool version_t::operator <=(const version_t& b) const
{
return compare(*this, b) <= 0;
}
bool version_t::operator >=(const version_t& b) const
{
return compare(*this, b) >= 0;
}
pal::string_t version_t::as_str() const
{
pal::stringstream_t stream;
if (m_major >= 0)
{
stream << m_major;
if (m_minor >= 0)
{
stream << _X(".") << m_minor;
if (m_build >= 0)
{
stream << _X(".") << m_build;
if (m_revision >= 0)
{
stream << _X(".") << m_revision;
}
}
}
}
return stream.str();
}
/*static*/ int version_t::compare(const version_t&a, const version_t& b)
{
if (a.m_major != b.m_major)
{
return (a.m_major > b.m_major) ? 1 : -1;
}
if (a.m_minor != b.m_minor)
{
return (a.m_minor > b.m_minor) ? 1 : -1;
}
if (a.m_build != b.m_build)
{
return (a.m_build > b.m_build) ? 1 : -1;
}
if (a.m_revision != b.m_revision)
{
return (a.m_revision > b.m_revision) ? 1 : -1;
}
return 0;
}
bool parse_internal(const pal::string_t& ver, version_t* ver_out)
{
unsigned major = -1;
size_t maj_start = 0;
size_t maj_sep = ver.find(_X('.'));
if (maj_sep == pal::string_t::npos)
{
return false; // minor required
}
if (!try_stou(ver.substr(maj_start, maj_sep), &major))
{
return false;
}
unsigned minor = -1;
size_t min_start = maj_sep + 1;
size_t min_sep = ver.find(_X('.'), min_start);
if (min_sep == pal::string_t::npos)
{
if (!try_stou(ver.substr(min_start), &minor))
{
return false;
}
*ver_out = version_t(major, minor, -1, -1);
return true; // build and revision not required
}
if (!try_stou(ver.substr(min_start, min_sep - min_start), &minor))
{
return false;
}
unsigned build = -1;
size_t build_start = min_sep + 1;
size_t build_sep = ver.find(_X('.'), build_start);
if (build_sep == pal::string_t::npos)
{
if (!try_stou(ver.substr(build_start), &build))
{
return false;
}
*ver_out = version_t(major, minor, build, -1);
return true; // revision not required
}
if (!try_stou(ver.substr(build_start, build_sep - build_start), &build))
{
return false;
}
unsigned revision = -1;
size_t revision_start = build_sep + 1;
if (!try_stou(ver.substr(revision_start), &revision))
{
return false;
}
*ver_out = version_t(major, minor, build, revision);
return true;
}
/* static */
bool version_t::parse(const pal::string_t& ver, version_t* ver_out)
{
bool valid = parse_internal(ver, ver_out);
assert(!valid || ver_out->as_str() == ver);
return valid;
}