// SPDX-License-Identifier: GPL-3.0-or-later // Copyright 2019 Juan Palacios #pragma once #include #include #include #include #include #include #include namespace Utils::String { /// Cleans a prefix from a string. /// /// @returns substring of the input string without prefix std::string cleanPrefix(std::string const &str, std::string const &prefix); /// Converts a string in 'mayor.minor.patch' format into numeric value. /// /// @param version string in 'mayor.minor.patch' format /// @returns version [mayor, minor, patch] as integers or [0, 0, 0] on error std::tuple parseVersion(std::string const &version); /// Converts a string containing a number representation into its numerical value. /// @param value will store the result of the conversion /// @param rep representation of the number to convert /// @param base numerical base of the representation to convert from (for integers) /// @returns true on a successful conversion template bool toNumber(T &value, std::string const &rep, int base = 10) { try { if constexpr (std::is_same_v) { value = static_cast(std::stoi(rep, nullptr, base)); } else if constexpr (std::is_same_v) { value = static_cast(std::stoi(rep, nullptr, base)); } else if constexpr (std::is_same_v) value = std::stoi(rep, nullptr, base); else if constexpr (std::is_same_v) value = std::stol(rep, nullptr, base); else if constexpr (std::is_same_v) value = std::stoll(rep, nullptr, base); else if constexpr (std::is_same_v || std::is_same_v || std::is_same_v || std::is_same_v) value = static_cast(std::stoul(rep, nullptr, base)); else if constexpr (std::is_same_v) value = std::stoull(rep, nullptr, base); else if constexpr (std::is_same_v) value = std::stof(rep, nullptr); else if constexpr (std::is_same_v) value = std::stod(rep, nullptr); else if constexpr (std::is_same_v) value = std::stold(rep, nullptr); return true; } catch (std::exception const &e) { SPDLOG_DEBUG("Cannot parse a number from the string '{}'. Error: {}", rep, e.what()); } return false; } /// Splits a string into substrings using a delimiter. /// @param src string to be splitted /// @param delim delimiter to cut into substrings /// @returns collection of substrings std::vector split(std::string const &src, char delim = ' '); /// Parse kernel version from a string following the format used by the kernel /// on /proc/version. /// @param data string to parse the kernel version from /// @returns a string containing the kernel version following semver format std::optional parseKernelProcVersion(std::string const &data); } // namespace Utils::String