Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2023 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * Author: Tristan Matthews <tristan.matthews@savoirfairelinux.com> |
| 5 | * Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | #include "string_utils.h" |
| 23 | |
| 24 | #include <fmt/core.h> |
| 25 | #include <fmt/ranges.h> |
| 26 | |
| 27 | #include <sstream> |
| 28 | #include <cctype> |
| 29 | #include <algorithm> |
| 30 | #include <ostream> |
| 31 | #include <iomanip> |
| 32 | #include <stdexcept> |
| 33 | #include <ios> |
| 34 | #include <charconv> |
| 35 | #include <string_view> |
| 36 | #ifdef _WIN32 |
| 37 | #include <windows.h> |
| 38 | #include <oleauto.h> |
| 39 | #endif |
| 40 | |
| 41 | #include <ciso646> // fix windows compiler bug |
| 42 | |
| 43 | namespace jami { |
| 44 | |
| 45 | #ifdef _WIN32 |
| 46 | std::wstring |
| 47 | to_wstring(const std::string& str, int codePage) |
| 48 | { |
| 49 | int srcLength = (int) str.length(); |
| 50 | int requiredSize = MultiByteToWideChar(codePage, 0, str.c_str(), srcLength, nullptr, 0); |
| 51 | if (!requiredSize) { |
| 52 | throw std::runtime_error("Can't convert string to wstring"); |
| 53 | } |
| 54 | std::wstring result((size_t) requiredSize, 0); |
| 55 | if (!MultiByteToWideChar(codePage, 0, str.c_str(), srcLength, &(*result.begin()), requiredSize)) { |
| 56 | throw std::runtime_error("Can't convert string to wstring"); |
| 57 | } |
| 58 | return result; |
| 59 | } |
| 60 | |
| 61 | std::string |
| 62 | to_string(const std::wstring& wstr, int codePage) |
| 63 | { |
| 64 | int srcLength = (int) wstr.length(); |
| 65 | int requiredSize = WideCharToMultiByte(codePage, 0, wstr.c_str(), srcLength, nullptr, 0, 0, 0); |
| 66 | if (!requiredSize) { |
| 67 | throw std::runtime_error("Can't convert wstring to string"); |
| 68 | } |
| 69 | std::string result((size_t) requiredSize, 0); |
| 70 | if (!WideCharToMultiByte( |
| 71 | codePage, 0, wstr.c_str(), srcLength, &(*result.begin()), requiredSize, 0, 0)) { |
| 72 | throw std::runtime_error("Can't convert wstring to string"); |
| 73 | } |
| 74 | return result; |
| 75 | } |
| 76 | #endif |
| 77 | |
| 78 | std::string |
| 79 | to_string(double value) |
| 80 | { |
| 81 | char buf[64]; |
| 82 | int len = snprintf(buf, sizeof(buf), "%-.*G", 16, value); |
| 83 | if (len <= 0) |
| 84 | throw std::invalid_argument {"can't parse double"}; |
| 85 | return {buf, (size_t) len}; |
| 86 | } |
| 87 | |
| 88 | std::string |
| 89 | to_hex_string(uint64_t id) |
| 90 | { |
| 91 | return fmt::format("{:016x}", id); |
| 92 | } |
| 93 | |
| 94 | uint64_t |
| 95 | from_hex_string(const std::string& str) |
| 96 | { |
| 97 | uint64_t id; |
| 98 | if (auto [p, ec] = std::from_chars(str.data(), str.data()+str.size(), id, 16); ec != std::errc()) { |
| 99 | throw std::invalid_argument("Can't parse id: " + str); |
| 100 | } |
| 101 | return id; |
| 102 | } |
| 103 | |
| 104 | std::string_view |
| 105 | trim(std::string_view s) |
| 106 | { |
| 107 | auto wsfront = std::find_if_not(s.cbegin(), s.cend(), [](int c) { return std::isspace(c); }); |
| 108 | return std::string_view(&*wsfront, std::find_if_not(s.rbegin(), |
| 109 | std::string_view::const_reverse_iterator(wsfront), |
| 110 | [](int c) { return std::isspace(c); }) |
| 111 | .base() - wsfront); |
| 112 | } |
| 113 | |
| 114 | std::vector<unsigned> |
| 115 | split_string_to_unsigned(std::string_view str, char delim) |
| 116 | { |
| 117 | std::vector<unsigned> output; |
| 118 | for (auto first = str.data(), second = str.data(), last = first + str.size(); second != last && first != last; first = second + 1) { |
| 119 | second = std::find(first, last, delim); |
| 120 | if (first != second) { |
| 121 | unsigned result; |
| 122 | auto [p, ec] = std::from_chars(first, second, result); |
| 123 | if (ec == std::errc()) |
| 124 | output.emplace_back(result); |
| 125 | } |
| 126 | } |
| 127 | return output; |
| 128 | } |
| 129 | |
| 130 | void |
| 131 | string_replace(std::string& str, const std::string& from, const std::string& to) |
| 132 | { |
| 133 | size_t start_pos = 0; |
| 134 | while ((start_pos = str.find(from, start_pos)) != std::string::npos) { |
| 135 | str.replace(start_pos, from.length(), to); |
| 136 | start_pos += to.length(); // Handles case where 'to' is a substring of 'from' |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | std::string_view |
| 141 | string_remove_suffix(std::string_view str, char separator) |
| 142 | { |
| 143 | auto it = str.find(separator); |
| 144 | if (it != std::string_view::npos) |
| 145 | str = str.substr(0, it); |
| 146 | return str; |
| 147 | } |
| 148 | |
| 149 | std::string |
| 150 | string_join(const std::set<std::string>& set, std::string_view separator) |
| 151 | { |
| 152 | return fmt::format("{}", fmt::join(set, separator)); |
| 153 | } |
| 154 | |
| 155 | std::set<std::string> |
| 156 | string_split_set(std::string& str, std::string_view separator) |
| 157 | { |
| 158 | std::set<std::string> output; |
| 159 | for (auto first = str.data(), second = str.data(), last = first + str.size(); second != last && first != last; first = second + 1) { |
| 160 | second = std::find_first_of(first, last, std::cbegin(separator), std::cend(separator)); |
| 161 | if (first != second) |
| 162 | output.emplace(first, second - first); |
| 163 | } |
| 164 | return output; |
| 165 | } |
| 166 | |
| 167 | } // namespace jami |