string_util.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // SPDX-FileCopyrightText: 2013 Dolphin Emulator Project
  2. // SPDX-FileCopyrightText: 2014 Citra Emulator Project
  3. // SPDX-License-Identifier: GPL-2.0-or-later
  4. #include <algorithm>
  5. #include <cctype>
  6. #include <codecvt>
  7. #include <locale>
  8. #include <sstream>
  9. #include "common/string_util.h"
  10. #ifdef _WIN32
  11. #include <windows.h>
  12. #endif
  13. #ifdef ANDROID
  14. #include <common/fs/fs_android.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(std::span<const u8> data) {
  30. return std::string(data.begin(), std::find(data.begin(), data.end(), '\0'));
  31. }
  32. std::string StringFromBuffer(std::span<const char> data) {
  33. return std::string(data.begin(), std::find(data.begin(), data.end(), '\0'));
  34. }
  35. // Turns " hej " into "hej". Also handles tabs.
  36. std::string StripSpaces(const std::string& str) {
  37. const std::size_t s = str.find_first_not_of(" \t\r\n");
  38. if (str.npos != s)
  39. return str.substr(s, str.find_last_not_of(" \t\r\n") - s + 1);
  40. else
  41. return "";
  42. }
  43. // "\"hello\"" is turned to "hello"
  44. // This one assumes that the string has already been space stripped in both
  45. // ends, as done by StripSpaces above, for example.
  46. std::string StripQuotes(const std::string& s) {
  47. if (s.size() && '\"' == s[0] && '\"' == *s.rbegin())
  48. return s.substr(1, s.size() - 2);
  49. else
  50. return s;
  51. }
  52. std::string StringFromBool(bool value) {
  53. return value ? "True" : "False";
  54. }
  55. bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename,
  56. std::string* _pExtension) {
  57. if (full_path.empty())
  58. return false;
  59. #ifdef ANDROID
  60. if (full_path[0] != '/') {
  61. *_pPath = Common::FS::Android::GetParentDirectory(full_path);
  62. *_pFilename = Common::FS::Android::GetFilename(full_path);
  63. return true;
  64. }
  65. #endif
  66. std::size_t dir_end = full_path.find_last_of("/"
  67. // windows needs the : included for something like just "C:" to be considered a directory
  68. #ifdef _WIN32
  69. "\\:"
  70. #endif
  71. );
  72. if (std::string::npos == dir_end)
  73. dir_end = 0;
  74. else
  75. dir_end += 1;
  76. std::size_t fname_end = full_path.rfind('.');
  77. if (fname_end < dir_end || std::string::npos == fname_end)
  78. fname_end = full_path.size();
  79. if (_pPath)
  80. *_pPath = full_path.substr(0, dir_end);
  81. if (_pFilename)
  82. *_pFilename = full_path.substr(dir_end, fname_end - dir_end);
  83. if (_pExtension)
  84. *_pExtension = full_path.substr(fname_end);
  85. return true;
  86. }
  87. void SplitString(const std::string& str, const char delim, std::vector<std::string>& output) {
  88. std::istringstream iss(str);
  89. output.resize(1);
  90. while (std::getline(iss, *output.rbegin(), delim)) {
  91. output.emplace_back();
  92. }
  93. output.pop_back();
  94. }
  95. std::string TabsToSpaces(int tab_size, std::string in) {
  96. std::size_t i = 0;
  97. while ((i = in.find('\t')) != std::string::npos) {
  98. in.replace(i, 1, tab_size, ' ');
  99. }
  100. return in;
  101. }
  102. std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest) {
  103. std::size_t pos = 0;
  104. if (src == dest)
  105. return result;
  106. while ((pos = result.find(src, pos)) != std::string::npos) {
  107. result.replace(pos, src.size(), dest);
  108. pos += dest.length();
  109. }
  110. return result;
  111. }
  112. std::string UTF16ToUTF8(std::u16string_view input) {
  113. std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
  114. return convert.to_bytes(input.data(), input.data() + input.size());
  115. }
  116. std::u16string UTF8ToUTF16(std::string_view input) {
  117. std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
  118. return convert.from_bytes(input.data(), input.data() + input.size());
  119. }
  120. std::u32string UTF8ToUTF32(std::string_view input) {
  121. std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> convert;
  122. return convert.from_bytes(input.data(), input.data() + input.size());
  123. }
  124. #ifdef _WIN32
  125. static std::wstring CPToUTF16(u32 code_page, std::string_view input) {
  126. const auto size =
  127. MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()), nullptr, 0);
  128. if (size == 0) {
  129. return {};
  130. }
  131. std::wstring output(size, L'\0');
  132. if (size != MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()),
  133. &output[0], static_cast<int>(output.size()))) {
  134. output.clear();
  135. }
  136. return output;
  137. }
  138. std::string UTF16ToUTF8(std::wstring_view input) {
  139. const auto size = WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()),
  140. nullptr, 0, nullptr, nullptr);
  141. if (size == 0) {
  142. return {};
  143. }
  144. std::string output(size, '\0');
  145. if (size != WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()),
  146. &output[0], static_cast<int>(output.size()), nullptr,
  147. nullptr)) {
  148. output.clear();
  149. }
  150. return output;
  151. }
  152. std::wstring UTF8ToUTF16W(std::string_view input) {
  153. return CPToUTF16(CP_UTF8, input);
  154. }
  155. #endif
  156. std::u16string U16StringFromBuffer(const u16* input, std::size_t length) {
  157. return std::u16string(reinterpret_cast<const char16_t*>(input), length);
  158. }
  159. std::string StringFromFixedZeroTerminatedBuffer(std::string_view buffer, std::size_t max_len) {
  160. std::size_t len = 0;
  161. while (len < buffer.length() && len < max_len && buffer[len] != '\0') {
  162. ++len;
  163. }
  164. return std::string(buffer.begin(), buffer.begin() + len);
  165. }
  166. std::u16string UTF16StringFromFixedZeroTerminatedBuffer(std::u16string_view buffer,
  167. std::size_t max_len) {
  168. std::size_t len = 0;
  169. while (len < buffer.length() && len < max_len && buffer[len] != '\0') {
  170. ++len;
  171. }
  172. return std::u16string(buffer.begin(), buffer.begin() + len);
  173. }
  174. } // namespace Common