summaryrefslogtreecommitdiff
path: root/external/semver/src/lib/semantic_version_v1.cpp
blob: 4df8bef9b4f6674057fb28b40b7cd3c4d9d37cbe (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "semantic_version.h"

#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <sstream>
#include <tuple>
#include <vector>

using namespace std;

//------------------------------------------------------------------------------
// From http://semver.org/ - Version 2.0.0

// Pre-release versions satisfy but have a lower precedence than the associated
// normal version.

// Build versions satisfy and have a higher precedence than the associated
// normal version.

// Precedence MUST be calculated by separating the version into major, minor,
// patch, pre-release, and build identifiers in that order. Major, minor, and
// patch versions are always compared numerically. Pre-release and build version
// precedence MUST be determined by comparing each dot separated identifier as
// follows: identifiers consisting of only digits are compared numerically and
// identifiers with letters or dashes are compared lexically in ASCII sort
// order. Numeric identifiers always have lower precedence than non-numeric
// identifiers. Example: 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-beta.2 <
// 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0-rc.1+build.1 < 1.0.0 < 1.0.0+0.3.7 <
// 1.3.7+build < 1.3.7+build.2.b8f12d7 < 1.3.7+build.11.e0f985a.

namespace
{
  void SplitDottedString(const string& s, vector<string>& v)
  {
    istringstream ss(s);
    for (string part; getline(ss, part, '.'); v.push_back(part));
  }

  int CompareIdentifiers(const string& s1, const string& s2)
  {
    // We need to split the dotted identifier list into individual identifiers,
    // and treat purely numeric identifiers as the numbers they represent.

    vector<string> v1;
    SplitDottedString(s1, v1);

    vector<string> v2;
    SplitDottedString(s2, v2);

    for (size_t i = 0; ; ++i)
    {
      // exhausted both vectors: they must be equal
      if (i >= v1.size() && i >= v2.size())
      {
        return 0;
      }

      // exhausted one vector: it's the smaller one
      if (i >= v1.size())
      {
        return -1;
      }
      if (i >= v2.size())
      {
        return 1;
      }

      // is either of v1[i] or v2[i] a number?
      const string& id1 = v1[i];
      bool id1IsNumber = all_of(id1.cbegin(), id1.cend(),
                                [] (char c) { return isdigit(c); });
      const string& id2 = v2[i];
      bool id2IsNumber = all_of(id2.cbegin(), id2.cend(),
                                [] (char c) { return isdigit(c); });

      // if both numbers - compare them as such
      if (id1IsNumber && id2IsNumber)
      {
        long num1 = atol(id1.c_str());
        long num2 = atol(id2.c_str());
        if (num1 - num2 != 0)
        {
          return num1 - num2;
        }
        else
        {
          continue;
        }
      }
      // if one is a number - that one is lesser
      if (id1IsNumber)
      {
        return -1;
      }
      if (id2IsNumber)
      {
        return 1;
      }
      // neither are numbers: compare them
      int c = id1.compare(id2);
      if (c != 0)
      {
        return c;
      }
    }
  }

  bool IdentifierIsValid(const string& i)
  {
    vector<string> v;
    SplitDottedString(i, v);

    for (const auto& s : v)
    {
      // Identifiers must not be empty.
      if (s.empty())
      {
        return false;
      }

      // Identifiers must contain only alphanumerics and '-'.
      if (any_of(s.cbegin(), s.cend(),
                 [] (char c) { return !isalnum(c) && c != '-'; }))
      {
        return false;
      }

      // Numeric identifiers must not contain leading zeroes.
      bool numeric = all_of(s.cbegin(), s.cend(),
                            [] (char c) { return isdigit(c); });
      if (numeric && s[0] == '0')
      {
        return false;
      }
    }

    return true;
  }
}

namespace semver {
  namespace v1 {

//------------------------------------------------------------------------------
Version::Version(
    unsigned int major,
    unsigned int minor,
    unsigned int patch,
    const std::string& prerelease,
    const std::string& build)
  : m_majorVersion(major)
  , m_minorVersion(minor)
  , m_patchVersion(patch)
  , m_prereleaseVersion(prerelease)
  , m_buildVersion(build)
{
}

Version::Version(const string& s)
{
  // major.minor.patch-release+build

  istringstream ss(s);
  string part;

  if (!getline(ss, part, '.')) return;
  m_majorVersion = static_cast<unsigned int>(strtoul(part.c_str(), 0, 0));
  if (!getline(ss, part, '.')) return;
  m_minorVersion = static_cast<unsigned int>(strtoul(part.c_str(), 0, 0));
  if (!getline(ss, part, '-')) return;
  m_patchVersion = static_cast<unsigned int>(strtoul(part.c_str(), 0, 0));

  if (!getline(ss, m_prereleaseVersion, '+')) return;
  getline(ss, m_buildVersion, '\0');
}

//------------------------------------------------------------------------------
bool Version::IsWellFormed() const
{
  return IdentifierIsValid(m_prereleaseVersion)
    && IdentifierIsValid(m_buildVersion);
}

//------------------------------------------------------------------------------
// When incrementing versions, all lower version parts are reset.

Version Version::NextMajorVersion() const
{
  return Version(m_majorVersion + 1, 0, 0);
}

Version Version::NextMinorVersion() const
{
  return Version(m_majorVersion, m_minorVersion + 1, 0);
}

Version Version::NextPatchVersion() const
{
  return Version(m_majorVersion, m_minorVersion, m_patchVersion + 1);
}


//------------------------------------------------------------------------------
bool Version::Satisfies(const Version& other) const
{
  return tie(m_majorVersion, m_minorVersion, m_patchVersion) >=
    tie(other.m_majorVersion, other.m_minorVersion, other.m_patchVersion);
}

//------------------------------------------------------------------------------
bool operator==(const Version& a, const Version& b)
{
  return
    tie(a.m_majorVersion, a.m_minorVersion, a.m_patchVersion,
        a.m_prereleaseVersion, a.m_buildVersion) ==
    tie(b.m_majorVersion, b.m_minorVersion, b.m_patchVersion,
        b.m_prereleaseVersion, b.m_buildVersion);
}

//------------------------------------------------------------------------------
bool operator<(const Version& a, const Version& b)
{
  if (tie(a.m_majorVersion, a.m_minorVersion, a.m_patchVersion) <
      tie(b.m_majorVersion, b.m_minorVersion, b.m_patchVersion))
  {
    return true;
  }

  // pre-release version < normal version
  if (!a.m_prereleaseVersion.empty() && b.m_prereleaseVersion.empty())
  {
    return true;
  }
  if (a.m_prereleaseVersion.empty() && !b.m_prereleaseVersion.empty())
  {
    return false;
  }

  int prComp = CompareIdentifiers(a.m_prereleaseVersion, b.m_prereleaseVersion);
  if (prComp < 0)
  {
    return true;
  }
  else if (prComp == 0)
  {
    // build version > normal version
    if (a.m_buildVersion.empty() && !b.m_buildVersion.empty())
    {
      return true;
    }
    if (!a.m_buildVersion.empty() && b.m_buildVersion.empty())
    {
      return false;
    }

    int bComp = CompareIdentifiers(a.m_buildVersion, b.m_buildVersion);
    if (bComp < 0)
    {
      return true;
    }
  }
  return false;
}

//------------------------------------------------------------------------------
ostream& operator<<(ostream& s, const Version& v)
{
  s << v.m_majorVersion
    << '.' << v.m_minorVersion
    << '.' << v.m_patchVersion;

  if (!v.m_prereleaseVersion.empty())
  {
    s << '-' << v.m_prereleaseVersion;
  }
  if (!v.m_buildVersion.empty())
  {
    s << '+' << v.m_buildVersion;
  }

  return s;
}

  }
}