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