string_util.cpp 5.5 KB

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