| 1 | // SPDX-License-Identifier: GPL-3.0-or-latercxx . . $ `<file>/src/common/stringutils.h`/ |
| 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 Utilscxx . . $ Utils/::Stringcxx . . $ Utils/String/ { |
| 15 | |
| 16 | /// Cleans a prefix from a string. |
| 17 | /// |
| 18 | /// @returns substring of the input string without prefix |
| 19 | stdcxx . . $ std/::stringcxx . . $ std/string# cleanPrefixcxx . . $ Utils/String/cleanPrefix(203b42cd324c6c40).(stdcxx . . $ std/::stringcxx . . $ std/string# const &str, stdcxx . . $ std/::stringcxx . . $ 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 | stdcxx . . $ std/::tuplecxx . . $ std/tuple#<int, int, int> parseVersioncxx . . $ Utils/String/parseVersion(18acf75b68842036).(stdcxx . . $ std/::stringcxx . . $ 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 toNumbercxx . . $ Utils/String/toNumber(c75d689ebd728b7b).(T &value, stdcxx . . $ std/::stringcxx . . $ std/string# const &rep, int base = 10) |
| 34 | { |
| 35 | try { |
| 36 | if constexpr (stdcxx . . $ std/::is_same_v<T, char>) { |
| 37 | value = static_cast<char>(stdcxx . . $ std/::stoicxx . . $ std/__cxx11/stoi(ffee248579a0a688).(rep, nullptr, base)); |
| 38 | } |
| 39 | else if constexpr (stdcxx . . $ std/::is_same_v<T, short>) { |
| 40 | value = static_cast<short>(stdcxx . . $ std/::stoicxx . . $ std/__cxx11/stoi(ffee248579a0a688).(rep, nullptr, base)); |
| 41 | } |
| 42 | else if constexpr (stdcxx . . $ std/::is_same_v<T, int>) |
| 43 | value = stdcxx . . $ std/::stoicxx . . $ std/__cxx11/stoi(ffee248579a0a688).(rep, nullptr, base); |
| 44 | else if constexpr (stdcxx . . $ std/::is_same_v<T, long>) |
| 45 | value = stdcxx . . $ std/::stolcxx . . $ std/__cxx11/stol(85082be2742e71bc).(rep, nullptr, base); |
| 46 | else if constexpr (stdcxx . . $ std/::is_same_v<T, long long>) |
| 47 | value = stdcxx . . $ std/::stollcxx . . $ std/__cxx11/stoll(3277fef7064a8d65).(rep, nullptr, base); |
| 48 | else if constexpr (stdcxx . . $ std/::is_same_v<T, unsigned char> || |
| 49 | stdcxx . . $ std/::is_same_v<T, unsigned short> || |
| 50 | stdcxx . . $ std/::is_same_v<T, unsigned int> || |
| 51 | stdcxx . . $ std/::is_same_v<T, unsigned long>) |
| 52 | value = static_cast<T>(stdcxx . . $ std/::stoulcxx . . $ std/__cxx11/stoul(83a5b6ad71410cff).(rep, nullptr, base)); |
| 53 | else if constexpr (stdcxx . . $ std/::is_same_v<T, unsigned long long>) |
| 54 | value = stdcxx . . $ std/::stoullcxx . . $ std/__cxx11/stoull(a77875cccde39aac).(rep, nullptr, base); |
| 55 | else if constexpr (stdcxx . . $ std/::is_same_v<T, float>) |
| 56 | value = stdcxx . . $ std/::stofcxx . . $ std/__cxx11/stof(88ae9c73207ec85b).(rep, nullptr); |
| 57 | else if constexpr (stdcxx . . $ std/::is_same_v<T, double>) |
| 58 | value = stdcxx . . $ std/::stodcxx . . $ std/__cxx11/stod(af16c46e5dd8f341).(rep, nullptr); |
| 59 | else if constexpr (stdcxx . . $ std/::is_same_v<T, long double>) |
| 60 | value = stdcxx . . $ std/::stoldcxx . . $ std/__cxx11/stold(bc00ed76a2cfb1d1).(rep, nullptr); |
| 61 | |
| 62 | return true; |
| 63 | } |
| 64 | catch (stdcxx . . $ std/::exception const &e) { |
| 65 | SPDLOG_DEBUGcxx . . $ `<external>/13613935168682455490/spdlog.h:311:13`!("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 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::stringcxx . . $ std/string#> splitcxx . . $ Utils/String/split(119f23dcbaa30370).(stdcxx . . $ std/::stringcxx . . $ 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 | stdcxx . . $ std/::optionalcxx . . $ std/optional#<stdcxx . . $ std/::stringcxx . . $ std/string#> parseKernelProcVersioncxx . . $ Utils/String/parseKernelProcVersion(1bef80927fe03eda).(stdcxx . . $ std/::stringcxx . . $ std/string# const &data); |
| 83 | |
| 84 | } // namespace Utils::String |