string_util.cpp 5.6 KB

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