string_util.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. // 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. #ifdef ANDROID
  57. if (full_path[0] != '/') {
  58. *_pPath = Common::FS::Android::GetParentDirectory(full_path);
  59. *_pFilename = Common::FS::Android::GetFilename(full_path);
  60. return true;
  61. }
  62. #endif
  63. std::size_t dir_end = full_path.find_last_of("/"
  64. // windows needs the : included for something like just "C:" to be considered a directory
  65. #ifdef _WIN32
  66. "\\:"
  67. #endif
  68. );
  69. if (std::string::npos == dir_end)
  70. dir_end = 0;
  71. else
  72. dir_end += 1;
  73. std::size_t fname_end = full_path.rfind('.');
  74. if (fname_end < dir_end || std::string::npos == fname_end)
  75. fname_end = full_path.size();
  76. if (_pPath)
  77. *_pPath = full_path.substr(0, dir_end);
  78. if (_pFilename)
  79. *_pFilename = full_path.substr(dir_end, fname_end - dir_end);
  80. if (_pExtension)
  81. *_pExtension = full_path.substr(fname_end);
  82. return true;
  83. }
  84. void SplitString(const std::string& str, const char delim, std::vector<std::string>& output) {
  85. std::istringstream iss(str);
  86. output.resize(1);
  87. while (std::getline(iss, *output.rbegin(), delim)) {
  88. output.emplace_back();
  89. }
  90. output.pop_back();
  91. }
  92. std::string TabsToSpaces(int tab_size, std::string in) {
  93. std::size_t i = 0;
  94. while ((i = in.find('\t')) != std::string::npos) {
  95. in.replace(i, 1, tab_size, ' ');
  96. }
  97. return in;
  98. }
  99. std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest) {
  100. std::size_t pos = 0;
  101. if (src == dest)
  102. return result;
  103. while ((pos = result.find(src, pos)) != std::string::npos) {
  104. result.replace(pos, src.size(), dest);
  105. pos += dest.length();
  106. }
  107. return result;
  108. }
  109. std::string UTF16ToUTF8(std::u16string_view input) {
  110. std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
  111. return convert.to_bytes(input.data(), input.data() + input.size());
  112. }
  113. std::u16string UTF8ToUTF16(std::string_view input) {
  114. std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
  115. return convert.from_bytes(input.data(), input.data() + input.size());
  116. }
  117. std::u32string UTF8ToUTF32(std::string_view input) {
  118. std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> convert;
  119. return convert.from_bytes(input.data(), input.data() + input.size());
  120. }
  121. #ifdef _WIN32
  122. static std::wstring CPToUTF16(u32 code_page, std::string_view input) {
  123. const auto size =
  124. MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()), nullptr, 0);
  125. if (size == 0) {
  126. return {};
  127. }
  128. std::wstring output(size, L'\0');
  129. if (size != MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()),
  130. &output[0], static_cast<int>(output.size()))) {
  131. output.clear();
  132. }
  133. return output;
  134. }
  135. std::string UTF16ToUTF8(std::wstring_view input) {
  136. const auto size = WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()),
  137. nullptr, 0, nullptr, nullptr);
  138. if (size == 0) {
  139. return {};
  140. }
  141. std::string output(size, '\0');
  142. if (size != WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()),
  143. &output[0], static_cast<int>(output.size()), nullptr,
  144. nullptr)) {
  145. output.clear();
  146. }
  147. return output;
  148. }
  149. std::wstring UTF8ToUTF16W(std::string_view input) {
  150. return CPToUTF16(CP_UTF8, input);
  151. }
  152. #endif
  153. std::u16string U16StringFromBuffer(const u16* input, std::size_t length) {
  154. return std::u16string(reinterpret_cast<const char16_t*>(input), length);
  155. }
  156. std::string StringFromFixedZeroTerminatedBuffer(std::string_view buffer, std::size_t max_len) {
  157. std::size_t len = 0;
  158. while (len < buffer.length() && len < max_len && buffer[len] != '\0') {
  159. ++len;
  160. }
  161. return std::string(buffer.begin(), buffer.begin() + len);
  162. }
  163. std::u16string UTF16StringFromFixedZeroTerminatedBuffer(std::u16string_view buffer,
  164. std::size_t max_len) {
  165. std::size_t len = 0;
  166. while (len < buffer.length() && len < max_len && buffer[len] != '\0') {
  167. ++len;
  168. }
  169. return std::u16string(buffer.begin(), buffer.begin() + len);
  170. }
  171. } // namespace Common