string_util.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 <cerrno>
  7. #include <cstdio>
  8. #include <cstdlib>
  9. #include <cstring>
  10. #include "common/common_paths.h"
  11. #include "common/logging/log.h"
  12. #include "common/string_util.h"
  13. #ifdef _WIN32
  14. #include <codecvt>
  15. #include <windows.h>
  16. #include "common/common_funcs.h"
  17. #else
  18. #include <iconv.h>
  19. #endif
  20. namespace Common {
  21. /// Make a string lowercase
  22. std::string ToLower(std::string str) {
  23. std::transform(str.begin(), str.end(), str.begin(),
  24. [](unsigned char c) { return std::tolower(c); });
  25. return str;
  26. }
  27. /// Make a string uppercase
  28. std::string ToUpper(std::string str) {
  29. std::transform(str.begin(), str.end(), str.begin(),
  30. [](unsigned char c) { return std::toupper(c); });
  31. return str;
  32. }
  33. // For Debugging. Read out an u8 array.
  34. std::string ArrayToString(const u8* data, size_t size, int line_len, bool spaces) {
  35. std::ostringstream oss;
  36. oss << std::setfill('0') << std::hex;
  37. for (int line = 0; size; ++data, --size) {
  38. oss << std::setw(2) << (int)*data;
  39. if (line_len == ++line) {
  40. oss << '\n';
  41. line = 0;
  42. } else if (spaces)
  43. oss << ' ';
  44. }
  45. return oss.str();
  46. }
  47. std::string StringFromBuffer(const std::vector<u8>& data) {
  48. return std::string(data.begin(), std::find(data.begin(), data.end(), '\0'));
  49. }
  50. // Turns " hej " into "hej". Also handles tabs.
  51. std::string StripSpaces(const std::string& str) {
  52. const size_t s = str.find_first_not_of(" \t\r\n");
  53. if (str.npos != s)
  54. return str.substr(s, str.find_last_not_of(" \t\r\n") - s + 1);
  55. else
  56. return "";
  57. }
  58. // "\"hello\"" is turned to "hello"
  59. // This one assumes that the string has already been space stripped in both
  60. // ends, as done by StripSpaces above, for example.
  61. std::string StripQuotes(const std::string& s) {
  62. if (s.size() && '\"' == s[0] && '\"' == *s.rbegin())
  63. return s.substr(1, s.size() - 2);
  64. else
  65. return s;
  66. }
  67. bool TryParse(const std::string& str, u32* const output) {
  68. char* endptr = nullptr;
  69. // Reset errno to a value other than ERANGE
  70. errno = 0;
  71. unsigned long value = strtoul(str.c_str(), &endptr, 0);
  72. if (!endptr || *endptr)
  73. return false;
  74. if (errno == ERANGE)
  75. return false;
  76. #if ULONG_MAX > UINT_MAX
  77. if (value >= 0x100000000ull && value <= 0xFFFFFFFF00000000ull)
  78. return false;
  79. #endif
  80. *output = static_cast<u32>(value);
  81. return true;
  82. }
  83. bool TryParse(const std::string& str, bool* const output) {
  84. if ("1" == str || "true" == ToLower(str))
  85. *output = true;
  86. else if ("0" == str || "false" == ToLower(str))
  87. *output = false;
  88. else
  89. return false;
  90. return true;
  91. }
  92. std::string StringFromBool(bool value) {
  93. return value ? "True" : "False";
  94. }
  95. bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename,
  96. std::string* _pExtension) {
  97. if (full_path.empty())
  98. return false;
  99. size_t dir_end = full_path.find_last_of("/"
  100. // windows needs the : included for something like just "C:" to be considered a directory
  101. #ifdef _WIN32
  102. "\\:"
  103. #endif
  104. );
  105. if (std::string::npos == dir_end)
  106. dir_end = 0;
  107. else
  108. dir_end += 1;
  109. size_t fname_end = full_path.rfind('.');
  110. if (fname_end < dir_end || std::string::npos == fname_end)
  111. fname_end = full_path.size();
  112. if (_pPath)
  113. *_pPath = full_path.substr(0, dir_end);
  114. if (_pFilename)
  115. *_pFilename = full_path.substr(dir_end, fname_end - dir_end);
  116. if (_pExtension)
  117. *_pExtension = full_path.substr(fname_end);
  118. return true;
  119. }
  120. void BuildCompleteFilename(std::string& _CompleteFilename, const std::string& _Path,
  121. const std::string& _Filename) {
  122. _CompleteFilename = _Path;
  123. // check for seperator
  124. if (DIR_SEP_CHR != *_CompleteFilename.rbegin())
  125. _CompleteFilename += DIR_SEP_CHR;
  126. // add the filename
  127. _CompleteFilename += _Filename;
  128. }
  129. void SplitString(const std::string& str, const char delim, std::vector<std::string>& output) {
  130. std::istringstream iss(str);
  131. output.resize(1);
  132. while (std::getline(iss, *output.rbegin(), delim)) {
  133. output.emplace_back();
  134. }
  135. output.pop_back();
  136. }
  137. std::string TabsToSpaces(int tab_size, std::string in) {
  138. size_t i = 0;
  139. while ((i = in.find('\t')) != std::string::npos) {
  140. in.replace(i, 1, tab_size, ' ');
  141. }
  142. return in;
  143. }
  144. std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest) {
  145. size_t pos = 0;
  146. if (src == dest)
  147. return result;
  148. while ((pos = result.find(src, pos)) != std::string::npos) {
  149. result.replace(pos, src.size(), dest);
  150. pos += dest.length();
  151. }
  152. return result;
  153. }
  154. #ifdef _WIN32
  155. std::string UTF16ToUTF8(const std::u16string& input) {
  156. #if _MSC_VER >= 1900
  157. // Workaround for missing char16_t/char32_t instantiations in MSVC2015
  158. std::wstring_convert<std::codecvt_utf8_utf16<__int16>, __int16> convert;
  159. std::basic_string<__int16> tmp_buffer(input.cbegin(), input.cend());
  160. return convert.to_bytes(tmp_buffer);
  161. #else
  162. std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
  163. return convert.to_bytes(input);
  164. #endif
  165. }
  166. std::u16string UTF8ToUTF16(const std::string& input) {
  167. #if _MSC_VER >= 1900
  168. // Workaround for missing char16_t/char32_t instantiations in MSVC2015
  169. std::wstring_convert<std::codecvt_utf8_utf16<__int16>, __int16> convert;
  170. auto tmp_buffer = convert.from_bytes(input);
  171. return std::u16string(tmp_buffer.cbegin(), tmp_buffer.cend());
  172. #else
  173. std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
  174. return convert.from_bytes(input);
  175. #endif
  176. }
  177. static std::wstring CPToUTF16(u32 code_page, const std::string& input) {
  178. const auto size =
  179. MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()), nullptr, 0);
  180. if (size == 0) {
  181. return L"";
  182. }
  183. std::wstring output(size, L'\0');
  184. if (size != MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()),
  185. &output[0], static_cast<int>(output.size()))) {
  186. output.clear();
  187. }
  188. return output;
  189. }
  190. std::string UTF16ToUTF8(const std::wstring& input) {
  191. const auto size = WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()),
  192. nullptr, 0, nullptr, nullptr);
  193. if (size == 0) {
  194. return "";
  195. }
  196. std::string output(size, '\0');
  197. if (size != WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()),
  198. &output[0], static_cast<int>(output.size()), nullptr,
  199. nullptr)) {
  200. output.clear();
  201. }
  202. return output;
  203. }
  204. std::wstring UTF8ToUTF16W(const std::string& input) {
  205. return CPToUTF16(CP_UTF8, input);
  206. }
  207. std::string SHIFTJISToUTF8(const std::string& input) {
  208. return UTF16ToUTF8(CPToUTF16(932, input));
  209. }
  210. std::string CP1252ToUTF8(const std::string& input) {
  211. return UTF16ToUTF8(CPToUTF16(1252, input));
  212. }
  213. #else
  214. template <typename T>
  215. static std::string CodeToUTF8(const char* fromcode, const std::basic_string<T>& input) {
  216. iconv_t const conv_desc = iconv_open("UTF-8", fromcode);
  217. if ((iconv_t)(-1) == conv_desc) {
  218. LOG_ERROR(Common, "Iconv initialization failure [{}]: {}", fromcode, strerror(errno));
  219. iconv_close(conv_desc);
  220. return {};
  221. }
  222. const size_t in_bytes = sizeof(T) * input.size();
  223. // Multiply by 4, which is the max number of bytes to encode a codepoint
  224. const size_t out_buffer_size = 4 * in_bytes;
  225. std::string out_buffer(out_buffer_size, '\0');
  226. auto src_buffer = &input[0];
  227. size_t src_bytes = in_bytes;
  228. auto dst_buffer = &out_buffer[0];
  229. size_t dst_bytes = out_buffer.size();
  230. while (0 != src_bytes) {
  231. size_t const iconv_result =
  232. iconv(conv_desc, (char**)(&src_buffer), &src_bytes, &dst_buffer, &dst_bytes);
  233. if (static_cast<size_t>(-1) == iconv_result) {
  234. if (EILSEQ == errno || EINVAL == errno) {
  235. // Try to skip the bad character
  236. if (0 != src_bytes) {
  237. --src_bytes;
  238. ++src_buffer;
  239. }
  240. } else {
  241. LOG_ERROR(Common, "iconv failure [{}]: {}", fromcode, strerror(errno));
  242. break;
  243. }
  244. }
  245. }
  246. std::string result;
  247. out_buffer.resize(out_buffer_size - dst_bytes);
  248. out_buffer.swap(result);
  249. iconv_close(conv_desc);
  250. return result;
  251. }
  252. std::u16string UTF8ToUTF16(const std::string& input) {
  253. iconv_t const conv_desc = iconv_open("UTF-16LE", "UTF-8");
  254. if ((iconv_t)(-1) == conv_desc) {
  255. LOG_ERROR(Common, "Iconv initialization failure [UTF-8]: {}", strerror(errno));
  256. iconv_close(conv_desc);
  257. return {};
  258. }
  259. const size_t in_bytes = sizeof(char) * input.size();
  260. // Multiply by 4, which is the max number of bytes to encode a codepoint
  261. const size_t out_buffer_size = 4 * sizeof(char16_t) * in_bytes;
  262. std::u16string out_buffer(out_buffer_size, char16_t{});
  263. char* src_buffer = const_cast<char*>(&input[0]);
  264. size_t src_bytes = in_bytes;
  265. char* dst_buffer = (char*)(&out_buffer[0]);
  266. size_t dst_bytes = out_buffer.size();
  267. while (0 != src_bytes) {
  268. size_t const iconv_result =
  269. iconv(conv_desc, &src_buffer, &src_bytes, &dst_buffer, &dst_bytes);
  270. if (static_cast<size_t>(-1) == iconv_result) {
  271. if (EILSEQ == errno || EINVAL == errno) {
  272. // Try to skip the bad character
  273. if (0 != src_bytes) {
  274. --src_bytes;
  275. ++src_buffer;
  276. }
  277. } else {
  278. LOG_ERROR(Common, "iconv failure [UTF-8]: {}", strerror(errno));
  279. break;
  280. }
  281. }
  282. }
  283. std::u16string result;
  284. out_buffer.resize(out_buffer_size - dst_bytes);
  285. out_buffer.swap(result);
  286. iconv_close(conv_desc);
  287. return result;
  288. }
  289. std::string UTF16ToUTF8(const std::u16string& input) {
  290. return CodeToUTF8("UTF-16LE", input);
  291. }
  292. std::string CP1252ToUTF8(const std::string& input) {
  293. // return CodeToUTF8("CP1252//TRANSLIT", input);
  294. // return CodeToUTF8("CP1252//IGNORE", input);
  295. return CodeToUTF8("CP1252", input);
  296. }
  297. std::string SHIFTJISToUTF8(const std::string& input) {
  298. // return CodeToUTF8("CP932", input);
  299. return CodeToUTF8("SJIS", input);
  300. }
  301. #endif
  302. std::string StringFromFixedZeroTerminatedBuffer(const char* buffer, size_t max_len) {
  303. size_t len = 0;
  304. while (len < max_len && buffer[len] != '\0')
  305. ++len;
  306. return std::string(buffer, len);
  307. }
  308. const char* TrimSourcePath(const char* path, const char* root) {
  309. const char* p = path;
  310. while (*p != '\0') {
  311. const char* next_slash = p;
  312. while (*next_slash != '\0' && *next_slash != '/' && *next_slash != '\\') {
  313. ++next_slash;
  314. }
  315. bool is_src = Common::ComparePartialString(p, next_slash, root);
  316. p = next_slash;
  317. if (*p != '\0') {
  318. ++p;
  319. }
  320. if (is_src) {
  321. path = p;
  322. }
  323. }
  324. return path;
  325. }
  326. } // namespace Common