Minor refactoring and cleanup.

This commit is contained in:
Marko Zivanovic
2015-10-08 16:23:08 +02:00
parent 2f5981c7d5
commit 4b4b90eeab
3 changed files with 22 additions and 38 deletions
-1
View File
@@ -24,7 +24,6 @@ SOFTWARE.
#pragma once
#include <iosfwd>
#include <string>
#include <vector>
+21 -16
View File
@@ -22,6 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <algorithm>
#include "version.h"
using namespace std;
@@ -30,7 +31,17 @@ namespace {
using namespace version;
int compare_prerel_identifiers(const Prerelease_identifier& l, Prerelease_identifier& r) {
int compare_normal(const Version_data& l, const Version_data& r) {
if (l.major > r.major) return 1;
if (l.major < r.major) return -1;
if (l.minor > r.minor) return 1;
if (l.minor < r.minor) return -1;
if (l.patch > r.patch) return 1;
if (l.patch < r.patch) return -1;
return 0;
}
int compare_prerel_identifiers(const Prerelease_identifier& l, const Prerelease_identifier& r) {
if (l.second == Identifier_type::alnum && r.second == Identifier_type::alnum) {
return l.first.compare(r.first);
} else if (l.second == Identifier_type::alnum && r.second == Identifier_type::num) {
@@ -43,7 +54,7 @@ namespace {
if (li == ri) return 0;
return li > ri ? 1 : -1;
}
throw runtime_error("unexpected identifier types: " + to_string(static_cast<int>(l.second)) + ", " +
throw logic_error("unexpected identifier types: " + to_string(static_cast<int>(l.second)) + ", " +
to_string(static_cast<int>(r.second)));
}
}
@@ -51,29 +62,23 @@ namespace {
namespace version {
int Semver200_comparator::compare(const Version_data& l, const Version_data& r) {
if (l.major > r.major) return 1;
if (l.major < r.major) return -1;
if (l.minor > r.minor) return 1;
if (l.minor < r.minor) return -1;
if (l.patch > r.patch) return 1;
if (l.patch < r.patch) return -1;
int cmp = compare_normal(l, r);
if (cmp != 0) return cmp;
// release version is always higher than prerelease
if (l.prerelease_ids.empty() && !r.prerelease_ids.empty()) return 1;
if (r.prerelease_ids.empty() && !l.prerelease_ids.empty()) return -1;
// compare release by looking at each identifier: numeric ones are compared as numbers, alphanum as ASCII strings
auto shorter = l.prerelease_ids.size() <= r.prerelease_ids.size() ? l.prerelease_ids.size() : r.prerelease_ids.size();
// compare release by looking at each identifier: numeric ones are compared as numbers,
// alphanum as ASCII strings
auto shorter = min(l.prerelease_ids.size(), r.prerelease_ids.size());
for (size_t i = 0; i < shorter; i++) {
auto lid = l.prerelease_ids[i];
auto rid = r.prerelease_ids[i];
int cmp = compare_prerel_identifiers(lid, rid);
cmp = compare_prerel_identifiers(l.prerelease_ids[i], r.prerelease_ids[i]);
if (cmp != 0) return cmp;
}
// prerels are the same, to the length of the shorter one;
// if they are the same length, then version are equal, otherwise, longer wins
// if they are the same length, then versions are equal, otherwise, longer wins
if (l.prerelease_ids.size() == r.prerelease_ids.size()) return 0;
return l.prerelease_ids.size() > r.prerelease_ids.size() ? 1 : -1;
}
+1 -21
View File
@@ -42,7 +42,7 @@ namespace {
}
inline void process_char(const char c, Parser_phase& phase, Parser_phase& prev_phase,
const vector<Transition> transitions, string& target, Validator validate) {
const vector<Transition>& transitions, string& target, Validator validate) {
for (const auto& t : transitions) {
if (c == get<0>(t)) {
prev_phase = phase;
@@ -161,24 +161,4 @@ namespace version {
throw Parse_error(ex.what());
}
}
ostream& operator<<(ostream& os, const Prerelease_identifier& id) {
os << id.first << "<" << static_cast<int>(id.second) << ">";
return os;
}
ostream& operator<<(ostream& os, const Prerelease_identifiers& ids) {
for (const auto& id : ids) {
os << id << ",";
}
return os;
}
ostream& operator<<(ostream& os, const Build_identifiers& ids) {
for (const auto& id : ids) {
os << id << ",";
}
return os;
}
}