summaryrefslogtreecommitdiff
path: root/src/common/string_util.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/string_util.cpp')
-rw-r--r--src/common/string_util.cpp60
1 files changed, 31 insertions, 29 deletions
diff --git a/src/common/string_util.cpp b/src/common/string_util.cpp
index f3ad3d68a..1f0456aee 100644
--- a/src/common/string_util.cpp
+++ b/src/common/string_util.cpp
@@ -162,21 +162,21 @@ void SplitString(const std::string& str, const char delim, std::vector<std::stri
162 std::istringstream iss(str); 162 std::istringstream iss(str);
163 output.resize(1); 163 output.resize(1);
164 164
165 while (std::getline(iss, *output.rbegin(), delim)) 165 while (std::getline(iss, *output.rbegin(), delim)) {
166 output.push_back(""); 166 output.emplace_back();
167 }
167 168
168 output.pop_back(); 169 output.pop_back();
169} 170}
170 171
171std::string TabsToSpaces(int tab_size, const std::string& in) { 172std::string TabsToSpaces(int tab_size, std::string in) {
172 const std::string spaces(tab_size, ' ');
173 std::string out(in);
174
175 size_t i = 0; 173 size_t i = 0;
176 while (out.npos != (i = out.find('\t')))
177 out.replace(i, 1, spaces);
178 174
179 return out; 175 while ((i = in.find('\t')) != std::string::npos) {
176 in.replace(i, 1, tab_size, ' ');
177 }
178
179 return in;
180} 180}
181 181
182std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest) { 182std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest) {
@@ -220,31 +220,37 @@ std::u16string UTF8ToUTF16(const std::string& input) {
220} 220}
221 221
222static std::wstring CPToUTF16(u32 code_page, const std::string& input) { 222static std::wstring CPToUTF16(u32 code_page, const std::string& input) {
223 auto const size = 223 const auto size =
224 MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()), nullptr, 0); 224 MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()), nullptr, 0);
225 225
226 std::wstring output; 226 if (size == 0) {
227 output.resize(size); 227 return L"";
228 }
229
230 std::wstring output(size, L'\0');
228 231
229 if (size == 0 || 232 if (size != MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()),
230 size != MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()), 233 &output[0], static_cast<int>(output.size()))) {
231 &output[0], static_cast<int>(output.size())))
232 output.clear(); 234 output.clear();
235 }
233 236
234 return output; 237 return output;
235} 238}
236 239
237std::string UTF16ToUTF8(const std::wstring& input) { 240std::string UTF16ToUTF8(const std::wstring& input) {
238 auto const size = WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()), 241 const auto size = WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()),
239 nullptr, 0, nullptr, nullptr); 242 nullptr, 0, nullptr, nullptr);
243 if (size == 0) {
244 return "";
245 }
240 246
241 std::string output; 247 std::string output(size, '\0');
242 output.resize(size);
243 248
244 if (size == 0 || 249 if (size != WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()),
245 size != WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()), 250 &output[0], static_cast<int>(output.size()), nullptr,
246 &output[0], static_cast<int>(output.size()), nullptr, nullptr)) 251 nullptr)) {
247 output.clear(); 252 output.clear();
253 }
248 254
249 return output; 255 return output;
250} 256}
@@ -265,8 +271,6 @@ std::string CP1252ToUTF8(const std::string& input) {
265 271
266template <typename T> 272template <typename T>
267static std::string CodeToUTF8(const char* fromcode, const std::basic_string<T>& input) { 273static std::string CodeToUTF8(const char* fromcode, const std::basic_string<T>& input) {
268 std::string result;
269
270 iconv_t const conv_desc = iconv_open("UTF-8", fromcode); 274 iconv_t const conv_desc = iconv_open("UTF-8", fromcode);
271 if ((iconv_t)(-1) == conv_desc) { 275 if ((iconv_t)(-1) == conv_desc) {
272 LOG_ERROR(Common, "Iconv initialization failure [{}]: {}", fromcode, strerror(errno)); 276 LOG_ERROR(Common, "Iconv initialization failure [{}]: {}", fromcode, strerror(errno));
@@ -278,8 +282,7 @@ static std::string CodeToUTF8(const char* fromcode, const std::basic_string<T>&
278 // Multiply by 4, which is the max number of bytes to encode a codepoint 282 // Multiply by 4, which is the max number of bytes to encode a codepoint
279 const size_t out_buffer_size = 4 * in_bytes; 283 const size_t out_buffer_size = 4 * in_bytes;
280 284
281 std::string out_buffer; 285 std::string out_buffer(out_buffer_size, '\0');
282 out_buffer.resize(out_buffer_size);
283 286
284 auto src_buffer = &input[0]; 287 auto src_buffer = &input[0];
285 size_t src_bytes = in_bytes; 288 size_t src_bytes = in_bytes;
@@ -304,6 +307,7 @@ static std::string CodeToUTF8(const char* fromcode, const std::basic_string<T>&
304 } 307 }
305 } 308 }
306 309
310 std::string result;
307 out_buffer.resize(out_buffer_size - dst_bytes); 311 out_buffer.resize(out_buffer_size - dst_bytes);
308 out_buffer.swap(result); 312 out_buffer.swap(result);
309 313
@@ -313,8 +317,6 @@ static std::string CodeToUTF8(const char* fromcode, const std::basic_string<T>&
313} 317}
314 318
315std::u16string UTF8ToUTF16(const std::string& input) { 319std::u16string UTF8ToUTF16(const std::string& input) {
316 std::u16string result;
317
318 iconv_t const conv_desc = iconv_open("UTF-16LE", "UTF-8"); 320 iconv_t const conv_desc = iconv_open("UTF-16LE", "UTF-8");
319 if ((iconv_t)(-1) == conv_desc) { 321 if ((iconv_t)(-1) == conv_desc) {
320 LOG_ERROR(Common, "Iconv initialization failure [UTF-8]: {}", strerror(errno)); 322 LOG_ERROR(Common, "Iconv initialization failure [UTF-8]: {}", strerror(errno));
@@ -326,8 +328,7 @@ std::u16string UTF8ToUTF16(const std::string& input) {
326 // Multiply by 4, which is the max number of bytes to encode a codepoint 328 // Multiply by 4, which is the max number of bytes to encode a codepoint
327 const size_t out_buffer_size = 4 * sizeof(char16_t) * in_bytes; 329 const size_t out_buffer_size = 4 * sizeof(char16_t) * in_bytes;
328 330
329 std::u16string out_buffer; 331 std::u16string out_buffer(out_buffer_size, char16_t{});
330 out_buffer.resize(out_buffer_size);
331 332
332 char* src_buffer = const_cast<char*>(&input[0]); 333 char* src_buffer = const_cast<char*>(&input[0]);
333 size_t src_bytes = in_bytes; 334 size_t src_bytes = in_bytes;
@@ -352,6 +353,7 @@ std::u16string UTF8ToUTF16(const std::string& input) {
352 } 353 }
353 } 354 }
354 355
356 std::u16string result;
355 out_buffer.resize(out_buffer_size - dst_bytes); 357 out_buffer.resize(out_buffer_size - dst_bytes);
356 out_buffer.swap(result); 358 out_buffer.swap(result);
357 359