aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Common/VersionNumber.cpp
blob: cb02f6f8db25497c476334bb8531e19a5e734000 (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
#include <Common/VersionNumber.h>
#include <IO/ReadBufferFromString.h>
#include <IO/ReadHelpers.h>

namespace DB
{

VersionNumber::VersionNumber(std::string version_string)
{
    if (version_string.empty())
        return;

    ReadBufferFromString rb(version_string);
    while (!rb.eof())
    {
        Int64 value;
        if (!tryReadIntText(value, rb))
            break;
        components.push_back(value);
        if (!checkChar('.', rb))
            break;
    }
}

std::string VersionNumber::toString() const
{
    return fmt::format("{}", fmt::join(components, "."));
}

int VersionNumber::compare(const VersionNumber & rhs) const
{
    size_t min = std::min(components.size(), rhs.components.size());
    for (size_t i = 0; i < min; ++i)
    {
        if (auto d = components[i] - rhs.components[i])
            return d > 0 ? 1 : -1;
    }

    if (components.size() > min)
    {
        return components[min] >= 0 ? 1 : -1;
    }
    else if (rhs.components.size() > min)
    {
        return -rhs.components[min] > 0 ? 1 : -1;
    }

    return 0;
}

}