ClickHouse/src/Common/VersionNumber.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

60 lines
1.2 KiB
C++
Raw Normal View History

#include <Common/VersionNumber.h>
#include <cstdlib>
#include <iostream>
namespace DB
{
2021-06-14 07:23:29 +00:00
VersionNumber::VersionNumber(std::string version_string)
{
if (version_string.empty())
return;
char * start = &version_string.front();
char * end = start;
const char * eos = &version_string.back() + 1;
do
{
Int64 value = strtol(start, &end, 10);
2021-06-14 07:23:29 +00:00
components.push_back(value);
start = end + 1;
}
while (start < eos && (end < eos && *end == '.'));
2021-06-14 07:23:29 +00:00
}
2021-06-14 07:23:29 +00:00
std::string VersionNumber::toString() const
{
std::string str;
for (Int64 v : components)
{
2021-06-14 07:23:29 +00:00
if (!str.empty())
str += '.';
str += std::to_string(v);
}
2021-06-14 07:23:29 +00:00
return str;
}
2021-06-14 07:23:29 +00:00
int VersionNumber::compare(const VersionNumber & rhs) const
{
2021-06-14 07:23:29 +00:00
size_t min = std::min(components.size(), rhs.components.size());
for (size_t i = 0; i < min; ++i)
{
2022-09-10 02:07:51 +00:00
if (auto d = components[i] - rhs.components[i])
return d > 0 ? 1 : -1;
2021-06-14 07:23:29 +00:00
}
2021-06-14 07:23:29 +00:00
if (components.size() > min)
{
2022-09-10 02:07:51 +00:00
return components[min] >= 0 ? 1 : -1;
2021-06-14 07:23:29 +00:00
}
else if (rhs.components.size() > min)
{
2022-09-10 02:07:51 +00:00
return -rhs.components[min] > 0 ? 1 : -1;
2021-06-14 07:23:29 +00:00
}
2021-06-14 07:23:29 +00:00
return 0;
}
}