string_util.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <cstdarg>
  6. #include <cstddef>
  7. #include <iomanip>
  8. #include <sstream>
  9. #include <string>
  10. #include <vector>
  11. #include "common/common_types.h"
  12. namespace Common {
  13. /// Make a string lowercase
  14. std::string ToLower(std::string str);
  15. /// Make a string uppercase
  16. std::string ToUpper(std::string str);
  17. std::string StringFromFormat(const char* format, ...);
  18. // Cheap!
  19. bool CharArrayFromFormatV(char* out, int outsize, const char* format, va_list args);
  20. template<size_t Count>
  21. inline void CharArrayFromFormat(char (& out)[Count], const char* format, ...)
  22. {
  23. va_list args;
  24. va_start(args, format);
  25. CharArrayFromFormatV(out, Count, format, args);
  26. va_end(args);
  27. }
  28. // Good
  29. std::string ArrayToString(const u8 *data, u32 size, int line_len = 20, bool spaces = true);
  30. std::string StripSpaces(const std::string &s);
  31. std::string StripQuotes(const std::string &s);
  32. // Thousand separator. Turns 12345678 into 12,345,678
  33. template <typename I>
  34. std::string ThousandSeparate(I value, int spaces = 0)
  35. {
  36. std::ostringstream oss;
  37. // std::locale("") seems to be broken on many platforms
  38. #if defined _WIN32 || (defined __linux__ && !defined __clang__)
  39. oss.imbue(std::locale(""));
  40. #endif
  41. oss << std::setw(spaces) << value;
  42. return oss.str();
  43. }
  44. std::string StringFromBool(bool value);
  45. bool TryParse(const std::string &str, bool *output);
  46. bool TryParse(const std::string &str, u32 *output);
  47. template <typename N>
  48. static bool TryParse(const std::string &str, N *const output)
  49. {
  50. std::istringstream iss(str);
  51. N tmp = 0;
  52. if (iss >> tmp)
  53. {
  54. *output = tmp;
  55. return true;
  56. }
  57. else
  58. return false;
  59. }
  60. // TODO: kill this
  61. bool AsciiToHex(const char* _szValue, u32& result);
  62. std::string TabsToSpaces(int tab_size, const std::string &in);
  63. void SplitString(const std::string& str, char delim, std::vector<std::string>& output);
  64. // "C:/Windows/winhelp.exe" to "C:/Windows/", "winhelp", ".exe"
  65. bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename, std::string* _pExtension);
  66. void BuildCompleteFilename(std::string& _CompleteFilename, const std::string& _Path, const std::string& _Filename);
  67. std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest);
  68. std::string UTF16ToUTF8(const std::u16string& input);
  69. std::u16string UTF8ToUTF16(const std::string& input);
  70. std::string CP1252ToUTF8(const std::string& str);
  71. std::string SHIFTJISToUTF8(const std::string& str);
  72. #ifdef _WIN32
  73. std::string UTF16ToUTF8(const std::wstring& input);
  74. std::wstring UTF8ToUTF16W(const std::string& str);
  75. #ifdef _UNICODE
  76. inline std::string TStrToUTF8(const std::wstring& str)
  77. { return UTF16ToUTF8(str); }
  78. inline std::wstring UTF8ToTStr(const std::string& str)
  79. { return UTF8ToUTF16W(str); }
  80. #else
  81. inline std::string TStrToUTF8(const std::string& str)
  82. { return str; }
  83. inline std::string UTF8ToTStr(const std::string& str)
  84. { return str; }
  85. #endif
  86. #endif
  87. /**
  88. * Compares the string defined by the range [`begin`, `end`) to the null-terminated C-string
  89. * `other` for equality.
  90. */
  91. template <typename InIt>
  92. bool ComparePartialString(InIt begin, InIt end, const char* other) {
  93. for (; begin != end && *other != '\0'; ++begin, ++other) {
  94. if (*begin != *other) {
  95. return false;
  96. }
  97. }
  98. // Only return true if both strings finished at the same point
  99. return (begin == end) == (*other == '\0');
  100. }
  101. /**
  102. * Creates a std::string from a fixed-size NUL-terminated char buffer. If the buffer isn't
  103. * NUL-terminated then the string ends at max_len characters.
  104. */
  105. std::string StringFromFixedZeroTerminatedBuffer(const char* buffer, size_t max_len);
  106. }