blob: 1c94abcb408ea33296eda6746bd7c392b485201a [file] [log] [blame]
Adrien Béraud612b55b2023-05-29 10:42:04 -04001/*
2 * Copyright (C) 2004-2023 Savoir-faire Linux Inc.
3 *
Adrien Béraudcb753622023-07-17 22:32:49 -04004 * This program is free software: you can redistribute it and/or modify
Adrien Béraud612b55b2023-05-29 10:42:04 -04005 * it under the terms of the GNU General Public License as published by
Adrien Béraudcb753622023-07-17 22:32:49 -04006 * the Free Software Foundation, either version 3 of the License, or
Adrien Béraud612b55b2023-05-29 10:42:04 -04007 * (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éraudcb753622023-07-17 22:32:49 -040011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Adrien Béraud612b55b2023-05-29 10:42:04 -040012 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Adrien Béraudcb753622023-07-17 22:32:49 -040015 * along with this program. If not, see <https://www.gnu.org/licenses/>.
Adrien Béraud612b55b2023-05-29 10:42:04 -040016 */
Adrien Béraud612b55b2023-05-29 10:42:04 -040017#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éraud1ae60aa2023-07-07 09:55:09 -040038namespace dhtnet {
Adrien Béraud612b55b2023-05-29 10:42:04 -040039
40#ifdef _WIN32
41std::wstring
42to_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) {
47 throw std::runtime_error("Can't convert string to wstring");
48 }
49 std::wstring result((size_t) requiredSize, 0);
50 if (!MultiByteToWideChar(codePage, 0, str.c_str(), srcLength, &(*result.begin()), requiredSize)) {
51 throw std::runtime_error("Can't convert string to wstring");
52 }
53 return result;
54}
55
56std::string
57to_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) {
62 throw std::runtime_error("Can't convert wstring to string");
63 }
64 std::string result((size_t) requiredSize, 0);
65 if (!WideCharToMultiByte(
66 codePage, 0, wstr.c_str(), srcLength, &(*result.begin()), requiredSize, 0, 0)) {
67 throw std::runtime_error("Can't convert wstring to string");
68 }
69 return result;
70}
71#endif
72
73std::string
74to_string(double value)
75{
76 char buf[64];
77 int len = snprintf(buf, sizeof(buf), "%-.*G", 16, value);
78 if (len <= 0)
79 throw std::invalid_argument {"can't parse double"};
80 return {buf, (size_t) len};
81}
82
83std::string
84to_hex_string(uint64_t id)
85{
86 return fmt::format("{:016x}", id);
87}
88
89uint64_t
90from_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()) {
94 throw std::invalid_argument("Can't parse id: " + str);
95 }
96 return id;
97}
98
99std::string_view
100trim(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
109std::vector<unsigned>
110split_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
125void
126string_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 Blin464bdff2023-07-19 08:02:53 -0400135} // namespace dhtnet