| 1 | // SPDX-License-Identifier: GPL-3.0-or-latercxx . . $ `<file>/src/common/stringutils.cpp`/ |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "stringutils.h"cxx . . $ `<file>/src/common/stringutils.h`/ |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <cstddef> |
| 8 | #include <regex> |
| 9 | #include <sstream> |
| 10 | |
| 11 | namespace Utilscxx . . $ Utils/::Stringcxx . . $ Utils/String/ { |
| 12 | |
| 13 | stdcxx . . $ std/::stringcxx . . $ std/string# cleanPrefixcxx . . $ Utils/String/cleanPrefix(203b42cd324c6c40).(stdcxx . . $ std/::stringcxx . . $ std/string# const &str, stdcxx . . $ std/::stringcxx . . $ std/string# const &prefix) |
| 14 | { |
| 15 | auto it = stdcxx . . $ std/::searchcxx . . $ std/search(31c92b7a471755b4).(str.cbegincxx . . $ std/__cxx11/basic_string#cbegin(a865d278ca20c908).(), str.cendcxx . . $ std/__cxx11/basic_string#cend(a865d278ca20c908).(), prefix.cbegincxx . . $ std/__cxx11/basic_string#cbegin(a865d278ca20c908).(), prefix.cendcxx . . $ std/__cxx11/basic_string#cend(a865d278ca20c908).()); |
| 16 | if (it != str.cendcxx . . $ std/__cxx11/basic_string#cend(a865d278ca20c908).()) { |
| 17 | size_t const pos = prefix.sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).(); |
| 18 | return str.substrcxx . . $ std/__cxx11/basic_string#substr(d59a03e0b0717fa0).(pos, str.sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).() - pos); |
| 19 | } |
| 20 | |
| 21 | return strcxx . . $ std/__cxx11/basic_string#basic_string(a47f53d4115f6ce5).; |
| 22 | } |
| 23 | |
| 24 | stdcxx . . $ std/::tuplecxx . . $ std/tuple#<int, int, int> parseVersioncxx . . $ Utils/String/parseVersion(18acf75b68842036).(stdcxx . . $ std/::stringcxx . . $ std/string# const &version) |
| 25 | { |
| 26 | try { |
| 27 | if (stdcxx . . $ std/::countcxx . . $ std/count(698bbaade62a0610).(version.cbegincxx . . $ std/__cxx11/basic_string#cbegin(a865d278ca20c908).(), version.cendcxx . . $ std/__cxx11/basic_string#cend(a865d278ca20c908).(), '.') == 2) { |
| 28 | auto firstDot = version.findcxx . . $ std/__cxx11/basic_string#find(5ed402c22c298042).("."); |
| 29 | auto secondDot = version.findcxx . . $ std/__cxx11/basic_string#find(5ed402c22c298042).(".", firstDot + 1); |
| 30 | if (firstDot > 0 && // mayor version exists |
| 31 | firstDot + 1 < secondDot && // minor version exists |
| 32 | secondDot < version.lengthcxx . . $ std/__cxx11/basic_string#length(7e80fff40d76986b).() - 1) // patch version exists |
| 33 | return {cxx . . $ std/tuple#tuple(7b0701f99332c8a8).stdcxx . . $ std/::stoicxx . . $ std/__cxx11/stoi(ffee248579a0a688).(version.substrcxx . . $ std/__cxx11/basic_string#substr(d59a03e0b0717fa0).(0, firstDot)), |
| 34 | stdcxx . . $ std/::stoicxx . . $ std/__cxx11/stoi(ffee248579a0a688).(version.substrcxx . . $ std/__cxx11/basic_string#substr(d59a03e0b0717fa0).(firstDot + 1, secondDot)), |
| 35 | stdcxx . . $ std/::stoicxx . . $ std/__cxx11/stoi(ffee248579a0a688).(version.substrcxx . . $ std/__cxx11/basic_string#substr(d59a03e0b0717fa0).(secondDot + 1, stdcxx . . $ std/::stringcxx . . $ std/string#::nposcxx . . $ std/__cxx11/basic_string#npos.))}; |
| 36 | } |
| 37 | SPDLOG_DEBUGcxx . . $ `<external>/13613935168682455490/spdlog.h:311:13`!("'{}' is not a valid version string", version); |
| 38 | } |
| 39 | catch (stdcxx . . $ std/::exception const &e) { |
| 40 | SPDLOG_DEBUGcxx . . $ `<external>/13613935168682455490/spdlog.h:311:13`!("Cannot parse version string '{}'. Error: {}", version, |
| 41 | e.what()); |
| 42 | } |
| 43 | |
| 44 | return {cxx . . $ std/tuple#tuple(7b0701f99332c8a8).0, 0, 0}; |
| 45 | } |
| 46 | |
| 47 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::stringcxx . . $ std/string#> splitcxx . . $ Utils/String/split(119f23dcbaa30370).(stdcxx . . $ std/::stringcxx . . $ std/string# const &src, char delim) |
| 48 | { |
| 49 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::stringcxx . . $ std/string#> resultcxx . . $ std/vector#vector(49f6e7a06ebc5aa8).; |
| 50 | |
| 51 | stdcxx . . $ std/::istringstreamcxx . . $ std/istringstream# istreamcxx . . $ std/__cxx11/basic_istringstream#basic_istringstream(d3c8ea0e6521074c).(src); |
| 52 | for (stdcxx . . $ std/::stringcxx . . $ std/string# partcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).; stdcxx . . $ std/::getlinecxx . . $ std/getline(30074676001a27e3).(istream, part, delim);) |
| 53 | if (!part.emptycxx . . $ std/__cxx11/basic_string#empty(3482b152b9333168).()) // skip empty parts |
| 54 | result.push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(part); |
| 55 | |
| 56 | return resultcxx . . $ std/vector#vector(6f559d22b039ccdb).; |
| 57 | } |
| 58 | |
| 59 | stdcxx . . $ std/::optionalcxx . . $ std/optional#<stdcxx . . $ std/::stringcxx . . $ std/string#> parseKernelProcVersioncxx . . $ Utils/String/parseKernelProcVersion(1bef80927fe03eda).(stdcxx . . $ std/::stringcxx . . $ std/string# const &data) |
| 60 | { |
| 61 | stdcxx . . $ std/::regexcxx . . $ std/__cxx11/regex# const regexcxx . . $ std/__cxx11/basic_regex#basic_regex(d1adbc785ae219ab).(R"(^Linux\s*version\s*(\d+\.\d+(?:\.\d+){0,1}).*)"); |
| 62 | |
| 63 | stdcxx . . $ std/::smatchcxx . . $ std/__cxx11/smatch# resultcxx . . $ std/__cxx11/match_results#match_results(49f6e7a06ebc5aa8).; |
| 64 | if (!stdcxx . . $ std/::regex_searchcxx . . $ std/regex_search(428e6b8e81328b43).(data, result, regex)) { |
| 65 | SPDLOG_DEBUGcxx . . $ `<external>/13613935168682455490/spdlog.h:311:13`!("Cannot parse kernel version"); |
| 66 | return {cxx . . $ std/optional#optional(ced63f7c635d850d).}; |
| 67 | } |
| 68 | |
| 69 | // Append .0 when the patch version number is missing, see #254 |
| 70 | stdcxx . . $ std/::stringcxx . . $ std/string# version = result[cxx . . $ std/__cxx11/match_results#`operator[]`(f2afb01e2bf6aaf8).1]; |
| 71 | if (stdcxx . . $ std/::countcxx . . $ std/count(698bbaade62a0610).(version.cbegincxx . . $ std/__cxx11/basic_string#cbegin(a865d278ca20c908).(), version.cendcxx . . $ std/__cxx11/basic_string#cend(a865d278ca20c908).(), '.') == 1) |
| 72 | version.appendcxx . . $ std/__cxx11/basic_string#append(978ca1133b5f6e74).(".0"); |
| 73 | |
| 74 | return versioncxx . . $ std/optional#optional(e3f0d6382bd52f24).; |
| 75 | } |
| 76 | |
| 77 | } // namespace Utils::String |