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 | #include "string_utils.h" |
| 18 | |
| 19 | #include <fmt/core.h> |
| 20 | #include <fmt/ranges.h> |
| 21 | |
| 22 | #include <sstream> |
| 23 | #include <cctype> |
| 24 | #include <algorithm> |
| 25 | #include <ostream> |
| 26 | #include <iomanip> |
| 27 | #include <stdexcept> |
| 28 | #include <ios> |
| 29 | #include <charconv> |
| 30 | #include <string_view> |
| 31 | #ifdef _WIN32 |
| 32 | #include <windows.h> |
| 33 | #include <oleauto.h> |
| 34 | #endif |
| 35 | |
| 36 | #include <ciso646> // fix windows compiler bug |
| 37 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 38 | namespace dhtnet { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 39 | |
| 40 | #ifdef _WIN32 |
| 41 | std::wstring |
| 42 | to_wstring(const std::string& str, int codePage) |
| 43 | { |
| 44 | int srcLength = (int) str.length(); |
| 45 | int requiredSize = MultiByteToWideChar(codePage, 0, str.c_str(), srcLength, nullptr, 0); |
| 46 | if (!requiredSize) { |
ovari123 | a15c688 | 2024-09-17 18:34:20 -0400 | [diff] [blame^] | 47 | throw std::runtime_error("Unable to convert string to wstring"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 48 | } |
| 49 | std::wstring result((size_t) requiredSize, 0); |
| 50 | if (!MultiByteToWideChar(codePage, 0, str.c_str(), srcLength, &(*result.begin()), requiredSize)) { |
ovari123 | a15c688 | 2024-09-17 18:34:20 -0400 | [diff] [blame^] | 51 | throw std::runtime_error("Unable to convert string to wstring"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 52 | } |
| 53 | return result; |
| 54 | } |
| 55 | |
| 56 | std::string |
| 57 | to_string(const std::wstring& wstr, int codePage) |
| 58 | { |
| 59 | int srcLength = (int) wstr.length(); |
| 60 | int requiredSize = WideCharToMultiByte(codePage, 0, wstr.c_str(), srcLength, nullptr, 0, 0, 0); |
| 61 | if (!requiredSize) { |
ovari123 | a15c688 | 2024-09-17 18:34:20 -0400 | [diff] [blame^] | 62 | throw std::runtime_error("Unable to convert wstring to string"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 63 | } |
| 64 | std::string result((size_t) requiredSize, 0); |
| 65 | if (!WideCharToMultiByte( |
| 66 | codePage, 0, wstr.c_str(), srcLength, &(*result.begin()), requiredSize, 0, 0)) { |
ovari123 | a15c688 | 2024-09-17 18:34:20 -0400 | [diff] [blame^] | 67 | throw std::runtime_error("Unable to convert wstring to string"); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 68 | } |
| 69 | return result; |
| 70 | } |
| 71 | #endif |
| 72 | |
| 73 | std::string |
| 74 | to_string(double value) |
| 75 | { |
| 76 | char buf[64]; |
| 77 | int len = snprintf(buf, sizeof(buf), "%-.*G", 16, value); |
| 78 | if (len <= 0) |
ovari123 | a15c688 | 2024-09-17 18:34:20 -0400 | [diff] [blame^] | 79 | throw std::invalid_argument {"Unable to parse double"}; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 80 | return {buf, (size_t) len}; |
| 81 | } |
| 82 | |
| 83 | std::string |
| 84 | to_hex_string(uint64_t id) |
| 85 | { |
| 86 | return fmt::format("{:016x}", id); |
| 87 | } |
| 88 | |
| 89 | uint64_t |
| 90 | from_hex_string(const std::string& str) |
| 91 | { |
| 92 | uint64_t id; |
| 93 | if (auto [p, ec] = std::from_chars(str.data(), str.data()+str.size(), id, 16); ec != std::errc()) { |
ovari123 | a15c688 | 2024-09-17 18:34:20 -0400 | [diff] [blame^] | 94 | throw std::invalid_argument("Unable to parse id: " + str); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 95 | } |
| 96 | return id; |
| 97 | } |
| 98 | |
| 99 | std::string_view |
| 100 | trim(std::string_view s) |
| 101 | { |
| 102 | auto wsfront = std::find_if_not(s.cbegin(), s.cend(), [](int c) { return std::isspace(c); }); |
| 103 | return std::string_view(&*wsfront, std::find_if_not(s.rbegin(), |
| 104 | std::string_view::const_reverse_iterator(wsfront), |
| 105 | [](int c) { return std::isspace(c); }) |
| 106 | .base() - wsfront); |
| 107 | } |
| 108 | |
| 109 | std::vector<unsigned> |
| 110 | split_string_to_unsigned(std::string_view str, char delim) |
| 111 | { |
| 112 | std::vector<unsigned> output; |
| 113 | for (auto first = str.data(), second = str.data(), last = first + str.size(); second != last && first != last; first = second + 1) { |
| 114 | second = std::find(first, last, delim); |
| 115 | if (first != second) { |
| 116 | unsigned result; |
| 117 | auto [p, ec] = std::from_chars(first, second, result); |
| 118 | if (ec == std::errc()) |
| 119 | output.emplace_back(result); |
| 120 | } |
| 121 | } |
| 122 | return output; |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | string_replace(std::string& str, const std::string& from, const std::string& to) |
| 127 | { |
| 128 | size_t start_pos = 0; |
| 129 | while ((start_pos = str.find(from, start_pos)) != std::string::npos) { |
| 130 | str.replace(start_pos, from.length(), to); |
| 131 | start_pos += to.length(); // Handles case where 'to' is a substring of 'from' |
| 132 | } |
| 133 | } |
| 134 | |
Sébastien Blin | 464bdff | 2023-07-19 08:02:53 -0400 | [diff] [blame] | 135 | } // namespace dhtnet |