Branch data Line data Source code
1 : : #include "version_helper.hpp"
2 : : #include <regex>
3 : :
4 : 23 : std::optional<OtaVersion> VersionHelper::parse(const std::string& version_str)
5 : : {
6 : : // Regex to find X.Y.Z pattern at the start of the string.
7 : : // Supports an optional 'v' prefix.
8 : : // The negative lookahead (?!\.\d) ensures we don't match if there's a fourth numeric component.
9 : 23 : std::regex version_regex(R"(^v?(\d+)\.(\d+)\.(\d+)(?!\.\d))");
10 : 23 : std::smatch match;
11 : :
12 [ + + ]: 23 : if (std::regex_search(version_str, match, version_regex)) {
13 : 17 : unsigned long major = std::strtoul(match[1].str().c_str(), nullptr, 10);
14 : 17 : unsigned long minor = std::strtoul(match[2].str().c_str(), nullptr, 10);
15 : 17 : unsigned long patch = std::strtoul(match[3].str().c_str(), nullptr, 10);
16 : :
17 [ + - - + ]: 17 : if (major > UINT16_MAX || minor > UINT16_MAX || patch > UINT16_MAX) {
18 : 0 : return std::nullopt;
19 : : }
20 : :
21 : 17 : return OtaVersion{static_cast<uint16_t>(major), static_cast<uint16_t>(minor), static_cast<uint16_t>(patch)};
22 : : }
23 : :
24 : 6 : return std::nullopt;
25 : 23 : }
|