Line data Source code
1 : /*
2 : * Copyright (C) 2004-2024 Savoir-faire Linux Inc.
3 : *
4 : * This program is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 3 of the License, or
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
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 : */
17 : #ifdef HAVE_CONFIG_H
18 : #include "config.h"
19 : #endif
20 :
21 : #include "string_utils.h"
22 :
23 : #include <fmt/core.h>
24 : #include <fmt/ranges.h>
25 :
26 : #include <sstream>
27 : #include <cctype>
28 : #include <algorithm>
29 : #include <ostream>
30 : #include <iomanip>
31 : #include <stdexcept>
32 : #include <ios>
33 : #include <charconv>
34 : #include <string_view>
35 : #ifdef _WIN32
36 : #include <windows.h>
37 : #include <oleauto.h>
38 : #endif
39 :
40 : #include <ciso646> // fix windows compiler bug
41 :
42 : namespace jami {
43 :
44 : std::string_view
45 699 : userAgent()
46 : {
47 723 : static const std::string USER_AGENT = fmt::format("{:s} ({:s}/{:s})", PACKAGE_NAME, platform(), arch());
48 699 : return USER_AGENT;
49 : }
50 :
51 : #ifdef _WIN32
52 : std::wstring
53 : to_wstring(const std::string& str, int codePage)
54 : {
55 : int srcLength = (int) str.length();
56 : int requiredSize = MultiByteToWideChar(codePage, 0, str.c_str(), srcLength, nullptr, 0);
57 : if (!requiredSize) {
58 : throw std::runtime_error("Unable to convert string to wstring");
59 : }
60 : std::wstring result((size_t) requiredSize, 0);
61 : if (!MultiByteToWideChar(codePage, 0, str.c_str(), srcLength, &(*result.begin()), requiredSize)) {
62 : throw std::runtime_error("Unable to convert string to wstring");
63 : }
64 : return result;
65 : }
66 :
67 : std::string
68 : to_string(const std::wstring& wstr, int codePage)
69 : {
70 : int srcLength = (int) wstr.length();
71 : int requiredSize = WideCharToMultiByte(codePage, 0, wstr.c_str(), srcLength, nullptr, 0, 0, 0);
72 : if (!requiredSize) {
73 : throw std::runtime_error("Unable to convert wstring to string");
74 : }
75 : std::string result((size_t) requiredSize, 0);
76 : if (!WideCharToMultiByte(
77 : codePage, 0, wstr.c_str(), srcLength, &(*result.begin()), requiredSize, 0, 0)) {
78 : throw std::runtime_error("Unable to convert wstring to string");
79 : }
80 : return result;
81 : }
82 : #endif
83 :
84 : std::string
85 79 : to_string(double value)
86 : {
87 : char buf[64];
88 79 : int len = snprintf(buf, sizeof(buf), "%-.*G", 16, value);
89 79 : if (len <= 0)
90 0 : throw std::invalid_argument {"Unable to parse double"};
91 79 : return {buf, (size_t) len};
92 : }
93 :
94 : std::string
95 1097 : to_hex_string(uint64_t id)
96 : {
97 1097 : return fmt::format("{:016x}", id);
98 : }
99 :
100 : uint64_t
101 292 : from_hex_string(const std::string& str)
102 : {
103 : uint64_t id;
104 292 : if (auto [p, ec] = std::from_chars(str.data(), str.data()+str.size(), id, 16); ec != std::errc()) {
105 0 : throw std::invalid_argument("Unable to parse id: " + str);
106 : }
107 292 : return id;
108 : }
109 :
110 : std::string_view
111 117 : trim(std::string_view s)
112 : {
113 234 : auto wsfront = std::find_if_not(s.cbegin(), s.cend(), [](int c) { return std::isspace(c); });
114 117 : return std::string_view(&*wsfront, std::find_if_not(s.rbegin(),
115 117 : std::string_view::const_reverse_iterator(wsfront),
116 117 : [](int c) { return std::isspace(c); })
117 117 : .base() - wsfront);
118 : }
119 :
120 : std::vector<unsigned>
121 475 : split_string_to_unsigned(std::string_view str, char delim)
122 : {
123 475 : std::vector<unsigned> output;
124 1867 : for (auto first = str.data(), second = str.data(), last = first + str.size(); second != last && first != last; first = second + 1) {
125 1392 : second = std::find(first, last, delim);
126 1392 : if (first != second) {
127 : unsigned result;
128 1390 : auto [p, ec] = std::from_chars(first, second, result);
129 1390 : if (ec == std::errc())
130 1390 : output.emplace_back(result);
131 : }
132 : }
133 475 : return output;
134 0 : }
135 :
136 : void
137 9759 : string_replace(std::string& str, const std::string& from, const std::string& to)
138 : {
139 9759 : size_t start_pos = 0;
140 10221 : while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
141 462 : str.replace(start_pos, from.length(), to);
142 462 : start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
143 : }
144 9759 : }
145 :
146 : std::string_view
147 1014 : string_remove_suffix(std::string_view str, char separator)
148 : {
149 1014 : auto it = str.find(separator);
150 1014 : if (it != std::string_view::npos)
151 178 : str = str.substr(0, it);
152 1014 : return str;
153 : }
154 :
155 : std::string
156 690 : string_join(const std::set<std::string>& set, std::string_view separator)
157 : {
158 1380 : return fmt::format("{}", fmt::join(set, separator));
159 : }
160 :
161 : std::set<std::string>
162 1606 : string_split_set(std::string& str, std::string_view separator)
163 : {
164 1606 : std::set<std::string> output;
165 1606 : for (auto first = str.data(), second = str.data(), last = first + str.size(); second != last && first != last; first = second + 1) {
166 0 : second = std::find_first_of(first, last, std::cbegin(separator), std::cend(separator));
167 0 : if (first != second)
168 0 : output.emplace(first, second - first);
169 : }
170 1606 : return output;
171 0 : }
172 :
173 : } // namespace jami
|