Debian Codegraph @

src/common/stringutils.h

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright 2019 Juan Palacios <[email protected]>
3
4 #pragma once
5
6 #include <exception>
7 #include <optional>
8 #include <spdlog/spdlog.h>
9 #include <string>
10 #include <tuple>
11 #include <type_traits>
12 #include <vector>
13
14 namespace Utils::String {
15
16 /// Cleans a prefix from a string.
17 ///
18 /// @returns substring of the input string without prefix
19 std::string cleanPrefix(std::string const &str, std::string const &prefix);
20
21 /// Converts a string in 'mayor.minor.patch' format into numeric value.
22 ///
23 /// @param version string in 'mayor.minor.patch' format
24 /// @returns version [mayor, minor, patch] as integers or [0, 0, 0] on error
25 std::tuple<int, int, int> parseVersion(std::string const &version);
26
27 /// Converts a string containing a number representation into its numerical value.
28 /// @param value will store the result of the conversion
29 /// @param rep representation of the number to convert
30 /// @param base numerical base of the representation to convert from (for integers)
31 /// @returns true on a successful conversion
32 template<typename T>
33 bool toNumber(T &value, std::string const &rep, int base = 10)
34 {
35 try {
36 if constexpr (std::is_same_v<T, char>) {
37 value = static_cast<char>(std::stoi(rep, nullptr, base));
38 }
39 else if constexpr (std::is_same_v<T, short>) {
40 value = static_cast<short>(std::stoi(rep, nullptr, base));
41 }
42 else if constexpr (std::is_same_v<T, int>)
43 value = std::stoi(rep, nullptr, base);
44 else if constexpr (std::is_same_v<T, long>)
45 value = std::stol(rep, nullptr, base);
46 else if constexpr (std::is_same_v<T, long long>)
47 value = std::stoll(rep, nullptr, base);
48 else if constexpr (std::is_same_v<T, unsigned char> ||
49 std::is_same_v<T, unsigned short> ||
50 std::is_same_v<T, unsigned int> ||
51 std::is_same_v<T, unsigned long>)
52 value = static_cast<T>(std::stoul(rep, nullptr, base));
53 else if constexpr (std::is_same_v<T, unsigned long long>)
54 value = std::stoull(rep, nullptr, base);
55 else if constexpr (std::is_same_v<T, float>)
56 value = std::stof(rep, nullptr);
57 else if constexpr (std::is_same_v<T, double>)
58 value = std::stod(rep, nullptr);
59 else if constexpr (std::is_same_v<T, long double>)
60 value = std::stold(rep, nullptr);
61
62 return true;
63 }
64 catch (std::exception const &e) {
65 SPDLOG_DEBUG("Cannot parse a number from the string '{}'. Error: {}", rep,
66 e.what());
67 }
68
69 return false;
70 }
71
72 /// Splits a string into substrings using a delimiter.
73 /// @param src string to be splitted
74 /// @param delim delimiter to cut into substrings
75 /// @returns collection of substrings
76 std::vector<std::string> split(std::string const &src, char delim = ' ');
77
78 /// Parse kernel version from a string following the format used by the kernel
79 /// on /proc/version.
80 /// @param data string to parse the kernel version from
81 /// @returns a string containing the kernel version following semver format
82 std::optional<std::string> parseKernelProcVersion(std::string const &data);
83
84 } // namespace Utils::String