aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/component/version.cpp
blob: 7204af028f4e5b4a462251f59c05b794920de40b (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "version.h"
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/vespalib/stllike/asciistream.h>
#include <climits>

namespace vespalib {

Version::Version(int major, int minor, int micro, const string & qualifier)
    : _major(major),
      _minor(minor),
      _micro(micro),
      _qualifier(qualifier),
      _stringValue()
{
    initialize();
}

Version::Version(const Version &) = default;
Version & Version::operator = (const Version &) = default;
Version::~Version() = default;

void
Version::initialize()
{
    asciistream buf;
    if (_qualifier != "") {
        buf << _major << "." << _minor << "." << _micro << "." << _qualifier;
    } else if (_micro > 0) {
        buf << _major << "." << _minor << "." << _micro;
    } else if (_minor > 0) {
        buf << _major << "." << _minor;
    } else if (_major > 0) {
        buf << _major;
    }
    _stringValue = buf.str();
    if ((_major < 0) || (_minor < 0) || (_micro < 0) || !_qualifier.empty()) {
        verifySanity();
    }
}

void
Version::verifySanity()
{
    if (_major < 0)
        throw IllegalArgumentException("Negative major in " + _stringValue);

    if (_minor < 0)
        throw IllegalArgumentException("Negative minor in " + _stringValue);

    if (_micro < 0)
        throw IllegalArgumentException("Negative micro in " + _stringValue);

    if (_qualifier != "") {
        for (size_t i = 0; i < _qualifier.length(); i++) {
            unsigned char c = _qualifier[i];
            if (! isalnum(c)) {
                throw IllegalArgumentException("Error in " + _stringValue + ": Invalid character in qualifier");
            }
        }
    }
}

// Precondition: input.empty() == false
static int parseInteger(stringref input) __attribute__((noinline));
static int parseInteger(stringref input)
{
    const char *s = input.data();
    unsigned char firstDigit = s[0];
    if (!isdigit(firstDigit))
        throw IllegalArgumentException("integer must start with a digit");
    char *ep;
    long ret = strtol(s, &ep, 10);
    if (ret > INT_MAX || ret < 0) {
        throw IllegalArgumentException("integer out of range");
    }
    if (s + input.size() != ep ) {
        throw IllegalArgumentException("extra characters after integer");
    }
    return ret;
}


Version::Version(const string & versionString)
    : _major(0),
      _minor(0),
      _micro(0),
      _qualifier(),
      _stringValue(versionString)
{
    if ( ! versionString.empty()) {
        stringref r(versionString.c_str(), versionString.size());
        stringref::size_type dot(r.find('.'));
        stringref majorS(r.substr(0, dot)); 

        if ( !majorS.empty()) {
            _major = parseInteger(majorS);
            if (dot == stringref::npos) return;
            r = r.substr(dot + 1);
            dot = r.find('.');
            stringref minorS(r.substr(0, dot)); 
            if ( !minorS.empty()) {
                _minor = parseInteger(minorS);

                if (dot == stringref::npos) return;
                r = r.substr(dot + 1);
                dot = r.find('.');
                stringref microS(r.substr(0, dot)); 
                if ( ! microS.empty()) {
                    _micro = parseInteger(microS);

                    if (dot == stringref::npos) return;
                    r = r.substr(dot + 1);
                    dot = r.find('.');
                    if (dot == stringref::npos) {
                        _qualifier = r; 
                    } else {
                        throw IllegalArgumentException("too many dot-separated components in version string '" + versionString + "'");
                    }
                }
            }
        }
    }
    verifySanity();
}

bool
Version::equals(const Version& other) const
{
    if (_major != other._major) return false;
    if (_minor != other._minor) return false;
    if (_micro != other._micro) return false;
    if (_qualifier != other._qualifier) return false;

    return true;
}

int
Version::compareTo(const Version& other) const
{
    int result = getMajor() - other.getMajor();
    if (result != 0) return result;

    result = getMinor() - other.getMinor();
    if (result != 0) return result;

    result = getMicro() - other.getMicro();
    if (result != 0) return result;

    return getQualifier().compare(other.getQualifier());
}

} // namespace vespalib