string_util.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #include <algorithm>
  5. #include <cctype>
  6. #include <codecvt>
  7. #include <cstdlib>
  8. #include <locale>
  9. #include <sstream>
  10. #include "common/common_paths.h"
  11. #include "common/logging/log.h"
  12. #include "common/string_util.h"
  13. #ifdef _WIN32
  14. #include <windows.h>
  15. #endif
  16. namespace Common {
  17. /// Make a string lowercase
  18. std::string ToLower(std::string str) {
  19. std::transform(str.begin(), str.end(), str.begin(),
  20. [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
  21. return str;
  22. }
  23. /// Make a string uppercase
  24. std::string ToUpper(std::string str) {
  25. std::transform(str.begin(), str.end(), str.begin(),
  26. [](unsigned char c) { return static_cast<char>(std::toupper(c)); });
  27. return str;
  28. }
  29. std::string StringFromBuffer(const std::vector<u8>& data) {
  30. return std::string(data.begin(), std::find(data.begin(), data.end(), '\0'));
  31. }
  32. // Turns " hej " into "hej". Also handles tabs.
  33. std::string StripSpaces(const std::string& str) {
  34. const std::size_t s = str.find_first_not_of(" \t\r\n");
  35. if (str.npos != s)
  36. return str.substr(s, str.find_last_not_of(" \t\r\n") - s + 1);
  37. else
  38. return "";
  39. }
  40. // "\"hello\"" is turned to "hello"
  41. // This one assumes that the string has already been space stripped in both
  42. // ends, as done by StripSpaces above, for example.
  43. std::string StripQuotes(const std::string& s) {
  44. if (s.size() && '\"' == s[0] && '\"' == *s.rbegin())
  45. return s.substr(1, s.size() - 2);
  46. else
  47. return s;
  48. }
  49. std::string StringFromBool(bool value) {
  50. return value ? "True" : "False";
  51. }
  52. bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename,
  53. std::string* _pExtension) {
  54. if (full_path.empty())
  55. return false;
  56. std::size_t dir_end = full_path.find_last_of("/"
  57. // windows needs the : included for something like just "C:" to be considered a directory
  58. #ifdef _WIN32
  59. "\\:"
  60. #endif
  61. );
  62. if (std::string::npos == dir_end)
  63. dir_end = 0;
  64. else
  65. dir_end += 1;
  66. std::size_t fname_end = full_path.rfind('.');
  67. if (fname_end < dir_end || std::string::npos == fname_end)
  68. fname_end = full_path.size();
  69. if (_pPath)
  70. *_pPath = full_path.substr(0, dir_end);
  71. if (_pFilename)
  72. *_pFilename = full_path.substr(dir_end, fname_end - dir_end);
  73. if (_pExtension)
  74. *_pExtension = full_path.substr(fname_end);
  75. return true;
  76. }
  77. void BuildCompleteFilename(std::string& _CompleteFilename, const std::string& _Path,
  78. const std::string& _Filename) {
  79. _CompleteFilename = _Path;
  80. // check for seperator
  81. if (DIR_SEP_CHR != *_CompleteFilename.rbegin())
  82. _CompleteFilename += DIR_SEP_CHR;
  83. // add the filename
  84. _CompleteFilename += _Filename;
  85. }
  86. void SplitString(const std::string& str, const char delim, std::vector<std::string>& output) {
  87. std::istringstream iss(str);
  88. output.resize(1);
  89. while (std::getline(iss, *output.rbegin(), delim)) {
  90. output.emplace_back();
  91. }
  92. output.pop_back();
  93. }
  94. std::string TabsToSpaces(int tab_size, std::string in) {
  95. std::size_t i = 0;
  96. while ((i = in.find('\t')) != std::string::npos) {
  97. in.replace(i, 1, tab_size, ' ');
  98. }
  99. return in;
  100. }
  101. std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest) {
  102. std::size_t pos = 0;
  103. if (src == dest)
  104. return result;
  105. while ((pos = result.find(src, pos)) != std::string::npos) {
  106. result.replace(pos, src.size(), dest);
  107. pos += dest.length();
  108. }
  109. return result;
  110. }
  111. std::string UTF16ToUTF8(const std::u16string& input) {
  112. #ifdef _MSC_VER
  113. // Workaround for missing char16_t/char32_t instantiations in MSVC2017
  114. std::wstring_convert<std::codecvt_utf8_utf16<__int16>, __int16> convert;
  115. std::basic_string<__int16> tmp_buffer(input.cbegin(), input.cend());
  116. return convert.to_bytes(tmp_buffer);
  117. #else
  118. std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
  119. return convert.to_bytes(input);
  120. #endif
  121. }
  122. std::u16string UTF8ToUTF16(const std::string& input) {
  123. #ifdef _MSC_VER
  124. // Workaround for missing char16_t/char32_t instantiations in MSVC2017
  125. std::wstring_convert<std::codecvt_utf8_utf16<__int16>, __int16> convert;
  126. auto tmp_buffer = convert.from_bytes(input);
  127. return std::u16string(tmp_buffer.cbegin(), tmp_buffer.cend());
  128. #else
  129. std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
  130. return convert.from_bytes(input);
  131. #endif
  132. }
  133. #ifdef _WIN32
  134. static std::wstring CPToUTF16(u32 code_page, const std::string& input) {
  135. const auto size =
  136. MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()), nullptr, 0);
  137. if (size == 0) {
  138. return L"";
  139. }
  140. std::wstring output(size, L'\0');
  141. if (size != MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()),
  142. &output[0], static_cast<int>(output.size()))) {
  143. output.clear();
  144. }
  145. return output;
  146. }
  147. std::string UTF16ToUTF8(const std::wstring& input) {
  148. const auto size = WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()),
  149. nullptr, 0, nullptr, nullptr);
  150. if (size == 0) {
  151. return "";
  152. }
  153. std::string output(size, '\0');
  154. if (size != WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()),
  155. &output[0], static_cast<int>(output.size()), nullptr,
  156. nullptr)) {
  157. output.clear();
  158. }
  159. return output;
  160. }
  161. std::wstring UTF8ToUTF16W(const std::string& input) {
  162. return CPToUTF16(CP_UTF8, input);
  163. }
  164. #endif
  165. std::string StringFromFixedZeroTerminatedBuffer(const char* buffer, std::size_t max_len) {
  166. std::size_t len = 0;
  167. while (len < max_len && buffer[len] != '\0')
  168. ++len;
  169. return std::string(buffer, len);
  170. }
  171. std::u16string UTF16StringFromFixedZeroTerminatedBuffer(std::u16string_view buffer,
  172. std::size_t max_len) {
  173. std::size_t len = 0;
  174. while (len < max_len && buffer[len] != '\0')
  175. ++len;
  176. return std::u16string(buffer.begin(), buffer.begin() + len);
  177. }
  178. } // namespace Common