summaryrefslogtreecommitdiff
path: root/src/common/string_util.cpp
diff options
context:
space:
mode:
authorGravatar Emmanuel Gil Peyrot2016-09-18 09:38:01 +0900
committerGravatar Emmanuel Gil Peyrot2016-09-18 09:38:01 +0900
commitdc8479928c5aee4c6ad6fe4f59006fb604cee701 (patch)
tree569a7f13128450bbab973236615587ff00bced5f /src/common/string_util.cpp
parentTravis: Import Dolphin’s clang-format hook. (diff)
downloadyuzu-dc8479928c5aee4c6ad6fe4f59006fb604cee701.tar.gz
yuzu-dc8479928c5aee4c6ad6fe4f59006fb604cee701.tar.xz
yuzu-dc8479928c5aee4c6ad6fe4f59006fb604cee701.zip
Sources: Run clang-format on everything.
Diffstat (limited to 'src/common/string_util.cpp')
-rw-r--r--src/common/string_util.cpp191
1 files changed, 75 insertions, 116 deletions
diff --git a/src/common/string_util.cpp b/src/common/string_util.cpp
index f0aa072db..9ccd6f135 100644
--- a/src/common/string_util.cpp
+++ b/src/common/string_util.cpp
@@ -14,11 +14,11 @@
14#include "common/string_util.h" 14#include "common/string_util.h"
15 15
16#ifdef _MSC_VER 16#ifdef _MSC_VER
17 #include <Windows.h> 17#include <Windows.h>
18 #include <codecvt> 18#include <codecvt>
19 #include "common/common_funcs.h" 19#include "common/common_funcs.h"
20#else 20#else
21 #include <iconv.h> 21#include <iconv.h>
22#endif 22#endif
23 23
24namespace Common { 24namespace Common {
@@ -36,9 +36,8 @@ std::string ToUpper(std::string str) {
36} 36}
37 37
38// faster than sscanf 38// faster than sscanf
39bool AsciiToHex(const char* _szValue, u32& result) 39bool AsciiToHex(const char* _szValue, u32& result) {
40{ 40 char* endptr = nullptr;
41 char *endptr = nullptr;
42 const u32 value = strtoul(_szValue, &endptr, 16); 41 const u32 value = strtoul(_szValue, &endptr, 16);
43 42
44 if (!endptr || *endptr) 43 if (!endptr || *endptr)
@@ -48,8 +47,7 @@ bool AsciiToHex(const char* _szValue, u32& result)
48 return true; 47 return true;
49} 48}
50 49
51bool CharArrayFromFormatV(char* out, int outsize, const char* format, va_list args) 50bool CharArrayFromFormatV(char* out, int outsize, const char* format, va_list args) {
52{
53 int writtenCount; 51 int writtenCount;
54 52
55#ifdef _MSC_VER 53#ifdef _MSC_VER
@@ -84,22 +82,18 @@ bool CharArrayFromFormatV(char* out, int outsize, const char* format, va_list ar
84 writtenCount = vsnprintf(out, outsize, format, args); 82 writtenCount = vsnprintf(out, outsize, format, args);
85#endif 83#endif
86 84
87 if (writtenCount > 0 && writtenCount < outsize) 85 if (writtenCount > 0 && writtenCount < outsize) {
88 {
89 out[writtenCount] = '\0'; 86 out[writtenCount] = '\0';
90 return true; 87 return true;
91 } 88 } else {
92 else
93 {
94 out[outsize - 1] = '\0'; 89 out[outsize - 1] = '\0';
95 return false; 90 return false;
96 } 91 }
97} 92}
98 93
99std::string StringFromFormat(const char* format, ...) 94std::string StringFromFormat(const char* format, ...) {
100{
101 va_list args; 95 va_list args;
102 char *buf = nullptr; 96 char* buf = nullptr;
103#ifdef _WIN32 97#ifdef _WIN32
104 int required = 0; 98 int required = 0;
105 99
@@ -124,21 +118,17 @@ std::string StringFromFormat(const char* format, ...)
124} 118}
125 119
126// For Debugging. Read out an u8 array. 120// For Debugging. Read out an u8 array.
127std::string ArrayToString(const u8 *data, u32 size, int line_len, bool spaces) 121std::string ArrayToString(const u8* data, u32 size, int line_len, bool spaces) {
128{
129 std::ostringstream oss; 122 std::ostringstream oss;
130 oss << std::setfill('0') << std::hex; 123 oss << std::setfill('0') << std::hex;
131 124
132 for (int line = 0; size; ++data, --size) 125 for (int line = 0; size; ++data, --size) {
133 {
134 oss << std::setw(2) << (int)*data; 126 oss << std::setw(2) << (int)*data;
135 127
136 if (line_len == ++line) 128 if (line_len == ++line) {
137 {
138 oss << '\n'; 129 oss << '\n';
139 line = 0; 130 line = 0;
140 } 131 } else if (spaces)
141 else if (spaces)
142 oss << ' '; 132 oss << ' ';
143 } 133 }
144 134
@@ -146,8 +136,7 @@ std::string ArrayToString(const u8 *data, u32 size, int line_len, bool spaces)
146} 136}
147 137
148// Turns " hej " into "hej". Also handles tabs. 138// Turns " hej " into "hej". Also handles tabs.
149std::string StripSpaces(const std::string &str) 139std::string StripSpaces(const std::string& str) {
150{
151 const size_t s = str.find_first_not_of(" \t\r\n"); 140 const size_t s = str.find_first_not_of(" \t\r\n");
152 141
153 if (str.npos != s) 142 if (str.npos != s)
@@ -159,17 +148,15 @@ std::string StripSpaces(const std::string &str)
159// "\"hello\"" is turned to "hello" 148// "\"hello\"" is turned to "hello"
160// This one assumes that the string has already been space stripped in both 149// This one assumes that the string has already been space stripped in both
161// ends, as done by StripSpaces above, for example. 150// ends, as done by StripSpaces above, for example.
162std::string StripQuotes(const std::string& s) 151std::string StripQuotes(const std::string& s) {
163{
164 if (s.size() && '\"' == s[0] && '\"' == *s.rbegin()) 152 if (s.size() && '\"' == s[0] && '\"' == *s.rbegin())
165 return s.substr(1, s.size() - 2); 153 return s.substr(1, s.size() - 2);
166 else 154 else
167 return s; 155 return s;
168} 156}
169 157
170bool TryParse(const std::string &str, u32 *const output) 158bool TryParse(const std::string& str, u32* const output) {
171{ 159 char* endptr = nullptr;
172 char *endptr = nullptr;
173 160
174 // Reset errno to a value other than ERANGE 161 // Reset errno to a value other than ERANGE
175 errno = 0; 162 errno = 0;
@@ -183,8 +170,7 @@ bool TryParse(const std::string &str, u32 *const output)
183 return false; 170 return false;
184 171
185#if ULONG_MAX > UINT_MAX 172#if ULONG_MAX > UINT_MAX
186 if (value >= 0x100000000ull 173 if (value >= 0x100000000ull && value <= 0xFFFFFFFF00000000ull)
187 && value <= 0xFFFFFFFF00000000ull)
188 return false; 174 return false;
189#endif 175#endif
190 176
@@ -192,8 +178,7 @@ bool TryParse(const std::string &str, u32 *const output)
192 return true; 178 return true;
193} 179}
194 180
195bool TryParse(const std::string &str, bool *const output) 181bool TryParse(const std::string& str, bool* const output) {
196{
197 if ("1" == str || "true" == ToLower(str)) 182 if ("1" == str || "true" == ToLower(str))
198 *output = true; 183 *output = true;
199 else if ("0" == str || "false" == ToLower(str)) 184 else if ("0" == str || "false" == ToLower(str))
@@ -204,22 +189,21 @@ bool TryParse(const std::string &str, bool *const output)
204 return true; 189 return true;
205} 190}
206 191
207std::string StringFromBool(bool value) 192std::string StringFromBool(bool value) {
208{
209 return value ? "True" : "False"; 193 return value ? "True" : "False";
210} 194}
211 195
212bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename, std::string* _pExtension) 196bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename,
213{ 197 std::string* _pExtension) {
214 if (full_path.empty()) 198 if (full_path.empty())
215 return false; 199 return false;
216 200
217 size_t dir_end = full_path.find_last_of("/" 201 size_t dir_end = full_path.find_last_of("/"
218 // windows needs the : included for something like just "C:" to be considered a directory 202// windows needs the : included for something like just "C:" to be considered a directory
219#ifdef _WIN32 203#ifdef _WIN32
220 ":" 204 ":"
221#endif 205#endif
222 ); 206 );
223 if (std::string::npos == dir_end) 207 if (std::string::npos == dir_end)
224 dir_end = 0; 208 dir_end = 0;
225 else 209 else
@@ -241,8 +225,8 @@ bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _
241 return true; 225 return true;
242} 226}
243 227
244void BuildCompleteFilename(std::string& _CompleteFilename, const std::string& _Path, const std::string& _Filename) 228void BuildCompleteFilename(std::string& _CompleteFilename, const std::string& _Path,
245{ 229 const std::string& _Filename) {
246 _CompleteFilename = _Path; 230 _CompleteFilename = _Path;
247 231
248 // check for seperator 232 // check for seperator
@@ -253,8 +237,7 @@ void BuildCompleteFilename(std::string& _CompleteFilename, const std::string& _P
253 _CompleteFilename += _Filename; 237 _CompleteFilename += _Filename;
254} 238}
255 239
256void SplitString(const std::string& str, const char delim, std::vector<std::string>& output) 240void SplitString(const std::string& str, const char delim, std::vector<std::string>& output) {
257{
258 std::istringstream iss(str); 241 std::istringstream iss(str);
259 output.resize(1); 242 output.resize(1);
260 243
@@ -264,8 +247,7 @@ void SplitString(const std::string& str, const char delim, std::vector<std::stri
264 output.pop_back(); 247 output.pop_back();
265} 248}
266 249
267std::string TabsToSpaces(int tab_size, const std::string &in) 250std::string TabsToSpaces(int tab_size, const std::string& in) {
268{
269 const std::string spaces(tab_size, ' '); 251 const std::string spaces(tab_size, ' ');
270 std::string out(in); 252 std::string out(in);
271 253
@@ -276,15 +258,13 @@ std::string TabsToSpaces(int tab_size, const std::string &in)
276 return out; 258 return out;
277} 259}
278 260
279std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest) 261std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest) {
280{
281 size_t pos = 0; 262 size_t pos = 0;
282 263
283 if (src == dest) 264 if (src == dest)
284 return result; 265 return result;
285 266
286 while ((pos = result.find(src, pos)) != std::string::npos) 267 while ((pos = result.find(src, pos)) != std::string::npos) {
287 {
288 result.replace(pos, src.size(), dest); 268 result.replace(pos, src.size(), dest);
289 pos += dest.length(); 269 pos += dest.length();
290 } 270 }
@@ -294,8 +274,7 @@ std::string ReplaceAll(std::string result, const std::string& src, const std::st
294 274
295#ifdef _MSC_VER 275#ifdef _MSC_VER
296 276
297std::string UTF16ToUTF8(const std::u16string& input) 277std::string UTF16ToUTF8(const std::u16string& input) {
298{
299#if _MSC_VER >= 1900 278#if _MSC_VER >= 1900
300 // Workaround for missing char16_t/char32_t instantiations in MSVC2015 279 // Workaround for missing char16_t/char32_t instantiations in MSVC2015
301 std::wstring_convert<std::codecvt_utf8_utf16<__int16>, __int16> convert; 280 std::wstring_convert<std::codecvt_utf8_utf16<__int16>, __int16> convert;
@@ -307,8 +286,7 @@ std::string UTF16ToUTF8(const std::u16string& input)
307#endif 286#endif
308} 287}
309 288
310std::u16string UTF8ToUTF16(const std::string& input) 289std::u16string UTF8ToUTF16(const std::string& input) {
311{
312#if _MSC_VER >= 1900 290#if _MSC_VER >= 1900
313 // Workaround for missing char16_t/char32_t instantiations in MSVC2015 291 // Workaround for missing char16_t/char32_t instantiations in MSVC2015
314 std::wstring_convert<std::codecvt_utf8_utf16<__int16>, __int16> convert; 292 std::wstring_convert<std::codecvt_utf8_utf16<__int16>, __int16> convert;
@@ -320,57 +298,56 @@ std::u16string UTF8ToUTF16(const std::string& input)
320#endif 298#endif
321} 299}
322 300
323static std::wstring CPToUTF16(u32 code_page, const std::string& input) 301static std::wstring CPToUTF16(u32 code_page, const std::string& input) {
324{ 302 auto const size =
325 auto const size = MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()), nullptr, 0); 303 MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()), nullptr, 0);
326 304
327 std::wstring output; 305 std::wstring output;
328 output.resize(size); 306 output.resize(size);
329 307
330 if (size == 0 || size != MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()), &output[0], static_cast<int>(output.size()))) 308 if (size == 0 ||
309 size != MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()),
310 &output[0], static_cast<int>(output.size())))
331 output.clear(); 311 output.clear();
332 312
333 return output; 313 return output;
334} 314}
335 315
336std::string UTF16ToUTF8(const std::wstring& input) 316std::string UTF16ToUTF8(const std::wstring& input) {
337{ 317 auto const size = WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()),
338 auto const size = WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()), nullptr, 0, nullptr, nullptr); 318 nullptr, 0, nullptr, nullptr);
339 319
340 std::string output; 320 std::string output;
341 output.resize(size); 321 output.resize(size);
342 322
343 if (size == 0 || size != WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()), &output[0], static_cast<int>(output.size()), nullptr, nullptr)) 323 if (size == 0 ||
324 size != WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()),
325 &output[0], static_cast<int>(output.size()), nullptr, nullptr))
344 output.clear(); 326 output.clear();
345 327
346 return output; 328 return output;
347} 329}
348 330
349std::wstring UTF8ToUTF16W(const std::string &input) 331std::wstring UTF8ToUTF16W(const std::string& input) {
350{
351 return CPToUTF16(CP_UTF8, input); 332 return CPToUTF16(CP_UTF8, input);
352} 333}
353 334
354std::string SHIFTJISToUTF8(const std::string& input) 335std::string SHIFTJISToUTF8(const std::string& input) {
355{
356 return UTF16ToUTF8(CPToUTF16(932, input)); 336 return UTF16ToUTF8(CPToUTF16(932, input));
357} 337}
358 338
359std::string CP1252ToUTF8(const std::string& input) 339std::string CP1252ToUTF8(const std::string& input) {
360{
361 return UTF16ToUTF8(CPToUTF16(1252, input)); 340 return UTF16ToUTF8(CPToUTF16(1252, input));
362} 341}
363 342
364#else 343#else
365 344
366template <typename T> 345template <typename T>
367static std::string CodeToUTF8(const char* fromcode, const std::basic_string<T>& input) 346static std::string CodeToUTF8(const char* fromcode, const std::basic_string<T>& input) {
368{
369 std::string result; 347 std::string result;
370 348
371 iconv_t const conv_desc = iconv_open("UTF-8", fromcode); 349 iconv_t const conv_desc = iconv_open("UTF-8", fromcode);
372 if ((iconv_t)(-1) == conv_desc) 350 if ((iconv_t)(-1) == conv_desc) {
373 {
374 LOG_ERROR(Common, "Iconv initialization failure [%s]: %s", fromcode, strerror(errno)); 351 LOG_ERROR(Common, "Iconv initialization failure [%s]: %s", fromcode, strerror(errno));
375 iconv_close(conv_desc); 352 iconv_close(conv_desc);
376 return {}; 353 return {};
@@ -388,24 +365,18 @@ static std::string CodeToUTF8(const char* fromcode, const std::basic_string<T>&
388 auto dst_buffer = &out_buffer[0]; 365 auto dst_buffer = &out_buffer[0];
389 size_t dst_bytes = out_buffer.size(); 366 size_t dst_bytes = out_buffer.size();
390 367
391 while (0 != src_bytes) 368 while (0 != src_bytes) {
392 { 369 size_t const iconv_result =
393 size_t const iconv_result = iconv(conv_desc, (char**)(&src_buffer), &src_bytes, 370 iconv(conv_desc, (char**)(&src_buffer), &src_bytes, &dst_buffer, &dst_bytes);
394 &dst_buffer, &dst_bytes);
395 371
396 if (static_cast<size_t>(-1) == iconv_result) 372 if (static_cast<size_t>(-1) == iconv_result) {
397 { 373 if (EILSEQ == errno || EINVAL == errno) {
398 if (EILSEQ == errno || EINVAL == errno)
399 {
400 // Try to skip the bad character 374 // Try to skip the bad character
401 if (0 != src_bytes) 375 if (0 != src_bytes) {
402 {
403 --src_bytes; 376 --src_bytes;
404 ++src_buffer; 377 ++src_buffer;
405 } 378 }
406 } 379 } else {
407 else
408 {
409 LOG_ERROR(Common, "iconv failure [%s]: %s", fromcode, strerror(errno)); 380 LOG_ERROR(Common, "iconv failure [%s]: %s", fromcode, strerror(errno));
410 break; 381 break;
411 } 382 }
@@ -420,13 +391,11 @@ static std::string CodeToUTF8(const char* fromcode, const std::basic_string<T>&
420 return result; 391 return result;
421} 392}
422 393
423std::u16string UTF8ToUTF16(const std::string& input) 394std::u16string UTF8ToUTF16(const std::string& input) {
424{
425 std::u16string result; 395 std::u16string result;
426 396
427 iconv_t const conv_desc = iconv_open("UTF-16LE", "UTF-8"); 397 iconv_t const conv_desc = iconv_open("UTF-16LE", "UTF-8");
428 if ((iconv_t)(-1) == conv_desc) 398 if ((iconv_t)(-1) == conv_desc) {
429 {
430 LOG_ERROR(Common, "Iconv initialization failure [UTF-8]: %s", strerror(errno)); 399 LOG_ERROR(Common, "Iconv initialization failure [UTF-8]: %s", strerror(errno));
431 iconv_close(conv_desc); 400 iconv_close(conv_desc);
432 return {}; 401 return {};
@@ -444,24 +413,18 @@ std::u16string UTF8ToUTF16(const std::string& input)
444 char* dst_buffer = (char*)(&out_buffer[0]); 413 char* dst_buffer = (char*)(&out_buffer[0]);
445 size_t dst_bytes = out_buffer.size(); 414 size_t dst_bytes = out_buffer.size();
446 415
447 while (0 != src_bytes) 416 while (0 != src_bytes) {
448 { 417 size_t const iconv_result =
449 size_t const iconv_result = iconv(conv_desc, &src_buffer, &src_bytes, 418 iconv(conv_desc, &src_buffer, &src_bytes, &dst_buffer, &dst_bytes);
450 &dst_buffer, &dst_bytes);
451 419
452 if (static_cast<size_t>(-1) == iconv_result) 420 if (static_cast<size_t>(-1) == iconv_result) {
453 { 421 if (EILSEQ == errno || EINVAL == errno) {
454 if (EILSEQ == errno || EINVAL == errno)
455 {
456 // Try to skip the bad character 422 // Try to skip the bad character
457 if (0 != src_bytes) 423 if (0 != src_bytes) {
458 {
459 --src_bytes; 424 --src_bytes;
460 ++src_buffer; 425 ++src_buffer;
461 } 426 }
462 } 427 } else {
463 else
464 {
465 LOG_ERROR(Common, "iconv failure [UTF-8]: %s", strerror(errno)); 428 LOG_ERROR(Common, "iconv failure [UTF-8]: %s", strerror(errno));
466 break; 429 break;
467 } 430 }
@@ -476,32 +439,28 @@ std::u16string UTF8ToUTF16(const std::string& input)
476 return result; 439 return result;
477} 440}
478 441
479std::string UTF16ToUTF8(const std::u16string& input) 442std::string UTF16ToUTF8(const std::u16string& input) {
480{
481 return CodeToUTF8("UTF-16LE", input); 443 return CodeToUTF8("UTF-16LE", input);
482} 444}
483 445
484std::string CP1252ToUTF8(const std::string& input) 446std::string CP1252ToUTF8(const std::string& input) {
485{ 447 // return CodeToUTF8("CP1252//TRANSLIT", input);
486 //return CodeToUTF8("CP1252//TRANSLIT", input); 448 // return CodeToUTF8("CP1252//IGNORE", input);
487 //return CodeToUTF8("CP1252//IGNORE", input);
488 return CodeToUTF8("CP1252", input); 449 return CodeToUTF8("CP1252", input);
489} 450}
490 451
491std::string SHIFTJISToUTF8(const std::string& input) 452std::string SHIFTJISToUTF8(const std::string& input) {
492{ 453 // return CodeToUTF8("CP932", input);
493 //return CodeToUTF8("CP932", input);
494 return CodeToUTF8("SJIS", input); 454 return CodeToUTF8("SJIS", input);
495} 455}
496 456
497#endif 457#endif
498 458
499std::string StringFromFixedZeroTerminatedBuffer(const char * buffer, size_t max_len) { 459std::string StringFromFixedZeroTerminatedBuffer(const char* buffer, size_t max_len) {
500 size_t len = 0; 460 size_t len = 0;
501 while (len < max_len && buffer[len] != '\0') 461 while (len < max_len && buffer[len] != '\0')
502 ++len; 462 ++len;
503 463
504 return std::string(buffer, len); 464 return std::string(buffer, len);
505} 465}
506
507} 466}