aboutsummaryrefslogtreecommitdiff
path: root/src/Version.cpp
blob: 26e5dbde3898b559ed7f85006af53b810358dbc0 (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
#include "../include/Version.hpp"
#include <cassert>

namespace sibs
{
    const char* asString(VersionOperation operation)
    {
        switch(operation)
        {
            case VersionOperation::LESS:                return "<";
            case VersionOperation::LESS_EQUAL:          return "<=";
            case VersionOperation::EQUAL:               return "=";
            case VersionOperation::GREATER:             return ">";
            case VersionOperation::GREATER_EQUAL:       return ">=";
            default:                                    return "";
        }
    }

    std::string PackageVersion::toString() const
    {
        std::string result;
        result += std::to_string(major);
        result += ".";
        result += std::to_string(minor);
        result += ".";
        result += std::to_string(patch);
        return result;
    }

    std::string PackageVersionRange::toString() const
    {
        std::string result;
        result += asString(startOperation);
        result += start.toString();
        if(endDefined)
        {
            result += " and ";
            result += asString(endOperation);
            result += end.toString();
        }
        return result;
    }

    static bool isInRangeOfEnd(const PackageVersionRange &versionRange, const PackageVersion &version)
    {
        if(!versionRange.endDefined)
            return true;

        switch(versionRange.endOperation)
        {
            case VersionOperation::LESS:
                return version < versionRange.end;
            case VersionOperation::LESS_EQUAL:
                return version <= versionRange.end;
            default:
                assert(false);
                return true;
        }
    }

    bool PackageVersionRange::isInRange(const PackageVersion &version) const
    {
        switch(startOperation)
        {
            case VersionOperation::LESS:
                return version < start;
            case VersionOperation::LESS_EQUAL:
                return version <= start;
            case VersionOperation::EQUAL:
                return version == start;
            case VersionOperation::GREATER:
            {
                if(version <= start)
                    return false;
                return isInRangeOfEnd(*this, version);
            }
            case VersionOperation::GREATER_EQUAL:
            {
                if(version < start)
                    return false;
                return isInRangeOfEnd(*this, version);
            }
        }
        return true;
    }
}