Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2023 Savoir-faire Linux Inc. |
| 3 | * |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 4 | * This program is free software: you can redistribute it and/or modify |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 5 | * it under the terms of the GNU General Public License as published by |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 6 | * the Free Software Foundation, either version 3 of the License, or |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 16 | */ |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 17 | #pragma once |
| 18 | |
| 19 | #include <cstdint> |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | #include <set> |
| 23 | #include <algorithm> |
| 24 | #include <regex> |
| 25 | #include <iterator> |
| 26 | #include <charconv> |
| 27 | |
| 28 | #ifdef _WIN32 |
| 29 | #include <WTypes.h> |
| 30 | #endif |
| 31 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 32 | namespace dhtnet { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 33 | |
| 34 | constexpr static const char TRUE_STR[] = "true"; |
| 35 | constexpr static const char FALSE_STR[] = "false"; |
| 36 | |
| 37 | constexpr static const char* |
| 38 | bool_to_str(bool b) noexcept |
| 39 | { |
| 40 | return b ? TRUE_STR : FALSE_STR; |
| 41 | } |
| 42 | |
| 43 | std::string to_string(double value); |
| 44 | |
| 45 | #ifdef _WIN32 |
| 46 | std::wstring to_wstring(const std::string& str, int codePage = CP_UTF8); |
| 47 | std::string to_string(const std::wstring& wstr, int codePage = CP_UTF8); |
| 48 | #endif |
| 49 | |
| 50 | std::string to_hex_string(uint64_t id); |
| 51 | uint64_t from_hex_string(const std::string& str); |
| 52 | |
| 53 | template<typename T> |
| 54 | T |
| 55 | to_int(std::string_view str, T defaultValue) |
| 56 | { |
| 57 | T result; |
| 58 | auto [p, ec] = std::from_chars(str.data(), str.data()+str.size(), result); |
| 59 | if (ec == std::errc()) |
| 60 | return result; |
| 61 | else |
| 62 | return defaultValue; |
| 63 | } |
| 64 | |
| 65 | template<typename T> |
| 66 | T |
| 67 | to_int(std::string_view str) |
| 68 | { |
| 69 | T result; |
| 70 | auto [p, ec] = std::from_chars(str.data(), str.data()+str.size(), result); |
| 71 | if (ec == std::errc()) |
| 72 | return result; |
| 73 | if (ec == std::errc::invalid_argument) |
ovari123 | a15c688 | 2024-09-17 18:34:20 -0400 | [diff] [blame] | 74 | throw std::invalid_argument("Unable to parse integer: invalid_argument"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 75 | else if (ec == std::errc::result_out_of_range) |
ovari123 | a15c688 | 2024-09-17 18:34:20 -0400 | [diff] [blame] | 76 | throw std::out_of_range("Unable to parse integer: out of range"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 77 | throw std::system_error(std::make_error_code(ec)); |
| 78 | } |
| 79 | |
| 80 | static inline int |
| 81 | stoi(const std::string& str) |
| 82 | { |
| 83 | return std::stoi(str); |
| 84 | } |
| 85 | |
| 86 | static inline double |
| 87 | stod(const std::string& str) |
| 88 | { |
| 89 | return std::stod(str); |
| 90 | } |
| 91 | |
| 92 | template<typename... Args> |
| 93 | std::string concat(Args &&... args){ |
| 94 | static_assert((std::is_constructible_v<std::string_view, Args&&> && ...)); |
| 95 | std::string s; |
| 96 | s.reserve((std::string_view{ args }.size() + ...)); |
| 97 | (s.append(std::forward<Args>(args)), ...); |
| 98 | return s; |
| 99 | } |
| 100 | |
| 101 | std::string_view trim(std::string_view s); |
| 102 | |
| 103 | /** |
| 104 | * Split a string_view with an API similar to std::getline. |
| 105 | * @param str The input string stream to iterate on, trimed of line during iteration. |
| 106 | * @param line The output substring. |
| 107 | * @param delim The delimiter. |
| 108 | * @return True if line was set, false if the end of the input was reached. |
| 109 | */ |
| 110 | inline bool |
| 111 | getline_full(std::string_view& str, std::string_view& line, char delim = '\n') |
| 112 | { |
| 113 | if (str.empty()) |
| 114 | return false; |
| 115 | auto pos = str.find(delim); |
| 116 | line = str.substr(0, pos); |
| 117 | str.remove_prefix(pos < str.size() ? pos + 1 : str.size()); |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Similar to @getline_full but skips empty results. |
| 123 | */ |
| 124 | inline bool |
| 125 | getline(std::string_view& str, std::string_view& line, char delim = '\n') |
| 126 | { |
| 127 | do { |
| 128 | if (!getline_full(str, line, delim)) |
| 129 | return false; |
| 130 | } while (line.empty()); |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | inline std::vector<std::string_view> |
| 135 | split_string(std::string_view str, char delim) |
| 136 | { |
| 137 | std::vector<std::string_view> output; |
| 138 | for (auto first = str.data(), second = str.data(), last = first + str.size(); |
| 139 | second != last && first != last; |
| 140 | first = second + 1) { |
| 141 | second = std::find(first, last, delim); |
| 142 | if (first != second) |
| 143 | output.emplace_back(first, second - first); |
| 144 | } |
| 145 | return output; |
| 146 | } |
| 147 | |
| 148 | inline std::vector<std::string_view> |
| 149 | split_string(std::string_view str, std::string_view delims = " ") |
| 150 | { |
| 151 | std::vector<std::string_view> output; |
| 152 | for (auto first = str.data(), second = str.data(), last = first + str.size(); |
| 153 | second != last && first != last; |
| 154 | first = second + 1) { |
| 155 | second = std::find_first_of(first, last, std::cbegin(delims), std::cend(delims)); |
| 156 | if (first != second) |
| 157 | output.emplace_back(first, second - first); |
| 158 | } |
| 159 | return output; |
| 160 | } |
| 161 | |
| 162 | std::vector<unsigned> split_string_to_unsigned(std::string_view s, char sep); |
| 163 | |
| 164 | void string_replace(std::string& str, const std::string& from, const std::string& to); |
| 165 | |
Sébastien Blin | 464bdff | 2023-07-19 08:02:53 -0400 | [diff] [blame] | 166 | } // namespace dhtnet |