StringUtil.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // Copyright 2008 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <charconv>
  5. #include <cstdarg>
  6. #include <cstddef>
  7. #include <cstdlib>
  8. #include <filesystem>
  9. #include <iomanip>
  10. #include <limits>
  11. #include <locale>
  12. #include <span>
  13. #include <sstream>
  14. #include <string>
  15. #include <type_traits>
  16. #include <vector>
  17. #include "Common/CommonTypes.h"
  18. namespace detail
  19. {
  20. template <typename T>
  21. constexpr bool IsBooleanEnum()
  22. {
  23. if constexpr (std::is_enum_v<T>)
  24. {
  25. return std::is_same_v<std::underlying_type_t<T>, bool>;
  26. }
  27. else
  28. {
  29. return false;
  30. }
  31. }
  32. } // namespace detail
  33. std::string StringFromFormatV(const char* format, va_list args);
  34. std::string StringFromFormat(const char* format, ...)
  35. #if !defined _WIN32
  36. // On compilers that support function attributes, this gives StringFromFormat
  37. // the same errors and warnings that printf would give.
  38. __attribute__((__format__(printf, 1, 2)))
  39. #endif
  40. ;
  41. // Cheap!
  42. bool CharArrayFromFormatV(char* out, int outsize, const char* format, va_list args);
  43. template <size_t Count>
  44. inline void CharArrayFromFormat(char (&out)[Count], const char* format, ...)
  45. {
  46. va_list args;
  47. va_start(args, format);
  48. CharArrayFromFormatV(out, Count, format, args);
  49. va_end(args);
  50. }
  51. // Good
  52. std::string ArrayToString(const u8* data, u32 size, int line_len = 20, bool spaces = true);
  53. std::string_view StripWhitespace(std::string_view s);
  54. std::string_view StripSpaces(std::string_view s);
  55. std::string_view StripQuotes(std::string_view s);
  56. std::string ReplaceAll(std::string result, std::string_view src, std::string_view dest);
  57. void ReplaceBreaksWithSpaces(std::string& str);
  58. void TruncateToCString(std::string* s);
  59. bool TryParse(const std::string& str, bool* output);
  60. template <typename T>
  61. requires(std::is_integral_v<T> ||
  62. (std::is_enum_v<T> && !detail::IsBooleanEnum<T>())) bool TryParse(const std::string& str,
  63. T* output, int base = 0)
  64. {
  65. char* end_ptr = nullptr;
  66. // Set errno to a clean slate.
  67. errno = 0;
  68. // Read a u64 for unsigned types and s64 otherwise.
  69. using ReadType = std::conditional_t<std::is_unsigned_v<T>, u64, s64>;
  70. ReadType value;
  71. if constexpr (std::is_unsigned_v<T>)
  72. value = std::strtoull(str.c_str(), &end_ptr, base);
  73. else
  74. value = std::strtoll(str.c_str(), &end_ptr, base);
  75. // Fail if the end of the string wasn't reached.
  76. if (end_ptr == nullptr || *end_ptr != '\0')
  77. return false;
  78. // Fail if the value was out of 64-bit range.
  79. if (errno == ERANGE)
  80. return false;
  81. using LimitsType = typename std::conditional_t<std::is_enum_v<T>, std::underlying_type<T>,
  82. std::common_type<T>>::type;
  83. // Fail if outside numeric limits.
  84. if (value < std::numeric_limits<LimitsType>::min() ||
  85. value > std::numeric_limits<LimitsType>::max())
  86. {
  87. return false;
  88. }
  89. *output = static_cast<T>(value);
  90. return true;
  91. }
  92. template <typename T>
  93. requires(detail::IsBooleanEnum<T>()) bool TryParse(const std::string& str, T* output)
  94. {
  95. bool value;
  96. if (!TryParse(str, &value))
  97. return false;
  98. *output = static_cast<T>(value);
  99. return true;
  100. }
  101. template <typename T, std::enable_if_t<std::is_floating_point_v<T>>* = nullptr>
  102. bool TryParse(std::string str, T* const output)
  103. {
  104. // Replace commas with dots.
  105. std::istringstream iss(ReplaceAll(std::move(str), ",", "."));
  106. // Use "classic" locale to force a "dot" decimal separator.
  107. iss.imbue(std::locale::classic());
  108. T tmp;
  109. // Succeed if a value was read and the entire string was used.
  110. if (iss >> tmp && iss.eof())
  111. {
  112. *output = tmp;
  113. return true;
  114. }
  115. return false;
  116. }
  117. template <typename N>
  118. bool TryParseVector(const std::string& str, std::vector<N>* output, const char delimiter = ',')
  119. {
  120. output->clear();
  121. std::istringstream buffer(str);
  122. std::string variable;
  123. while (std::getline(buffer, variable, delimiter))
  124. {
  125. N tmp = 0;
  126. if (!TryParse(variable, &tmp))
  127. return false;
  128. output->push_back(tmp);
  129. }
  130. return true;
  131. }
  132. std::string ValueToString(u16 value);
  133. std::string ValueToString(u32 value);
  134. std::string ValueToString(u64 value);
  135. std::string ValueToString(float value);
  136. std::string ValueToString(double value);
  137. std::string ValueToString(int value);
  138. std::string ValueToString(s64 value);
  139. std::string ValueToString(bool value);
  140. template <typename T, std::enable_if_t<std::is_enum<T>::value>* = nullptr>
  141. std::string ValueToString(T value)
  142. {
  143. return ValueToString(static_cast<std::underlying_type_t<T>>(value));
  144. }
  145. // Generates an hexdump-like representation of a binary data blob.
  146. std::string HexDump(const u8* data, size_t size);
  147. namespace Common
  148. {
  149. template <typename T, typename std::enable_if_t<std::is_integral_v<T>>* = nullptr>
  150. std::from_chars_result FromChars(std::string_view sv, T& value, int base = 10)
  151. {
  152. const char* const first = sv.data();
  153. const char* const last = first + sv.size();
  154. return std::from_chars(first, last, value, base);
  155. }
  156. template <typename T, typename std::enable_if_t<std::is_floating_point_v<T>>* = nullptr>
  157. std::from_chars_result FromChars(std::string_view sv, T& value,
  158. std::chars_format fmt = std::chars_format::general)
  159. {
  160. const char* const first = sv.data();
  161. const char* const last = first + sv.size();
  162. return std::from_chars(first, last, value, fmt);
  163. }
  164. } // namespace Common
  165. std::string TabsToSpaces(int tab_size, std::string str);
  166. std::vector<std::string> SplitString(const std::string& str, char delim);
  167. // "C:/Windows/winhelp.exe" to "C:/Windows/", "winhelp", ".exe"
  168. // This requires forward slashes to be used for the path separators, even on Windows.
  169. bool SplitPath(std::string_view full_path, std::string* path, std::string* filename,
  170. std::string* extension);
  171. // Converts the path separators of a path into forward slashes on Windows, which is assumed to be
  172. // true for paths at various places in the codebase.
  173. void UnifyPathSeparators(std::string& path);
  174. std::string WithUnifiedPathSeparators(std::string path);
  175. // Extracts just the filename (including extension) from a full path.
  176. // This requires forward slashes to be used for the path separators, even on Windows.
  177. std::string PathToFileName(std::string_view path);
  178. void StringPopBackIf(std::string* s, char c);
  179. size_t StringUTF8CodePointCount(std::string_view str);
  180. std::string CP1252ToUTF8(std::string_view str);
  181. std::string SHIFTJISToUTF8(std::string_view str);
  182. std::string UTF8ToSHIFTJIS(std::string_view str);
  183. std::string WStringToUTF8(std::wstring_view str);
  184. std::string UTF16BEToUTF8(const char16_t* str, size_t max_size); // Stops at \0
  185. std::string UTF16ToUTF8(std::u16string_view str);
  186. std::u16string UTF8ToUTF16(std::string_view str);
  187. #ifdef _WIN32
  188. std::wstring UTF8ToWString(std::string_view str);
  189. #ifdef _UNICODE
  190. inline std::string TStrToUTF8(std::wstring_view str)
  191. {
  192. return WStringToUTF8(str);
  193. }
  194. inline std::wstring UTF8ToTStr(std::string_view str)
  195. {
  196. return UTF8ToWString(str);
  197. }
  198. #else
  199. inline std::string TStrToUTF8(std::string_view str)
  200. {
  201. return str;
  202. }
  203. inline std::string UTF8ToTStr(std::string_view str)
  204. {
  205. return str;
  206. }
  207. #endif
  208. #endif
  209. std::filesystem::path StringToPath(std::string_view path);
  210. std::string PathToString(const std::filesystem::path& path);
  211. namespace Common
  212. {
  213. /// Returns whether a character is printable, i.e. whether 0x20 <= c <= 0x7e is true.
  214. /// Use this instead of calling std::isprint directly to ensure
  215. /// the C locale is being used and to avoid possibly undefined behaviour.
  216. inline bool IsPrintableCharacter(char c)
  217. {
  218. return std::isprint(c, std::locale::classic());
  219. }
  220. /// Returns whether a character is a letter, i.e. whether 'a' <= c <= 'z' || 'A' <= c <= 'Z'
  221. /// is true. Use this instead of calling std::isalpha directly to ensure
  222. /// the C locale is being used and to avoid possibly undefined behaviour.
  223. inline bool IsAlpha(char c)
  224. {
  225. return std::isalpha(c, std::locale::classic());
  226. }
  227. inline bool IsAlnum(char c)
  228. {
  229. return std::isalnum(c, std::locale::classic());
  230. }
  231. inline bool IsUpper(char c)
  232. {
  233. return std::isupper(c, std::locale::classic());
  234. }
  235. inline bool IsXDigit(char c)
  236. {
  237. return std::isxdigit(c /* no locale needed */) != 0;
  238. }
  239. inline char ToLower(char ch)
  240. {
  241. return std::tolower(ch, std::locale::classic());
  242. }
  243. inline char ToUpper(char ch)
  244. {
  245. return std::toupper(ch, std::locale::classic());
  246. }
  247. // Thousand separator. Turns 12345678 into 12,345,678
  248. template <typename I>
  249. std::string ThousandSeparate(I value, int spaces = 0)
  250. {
  251. #ifdef _WIN32
  252. std::wostringstream stream;
  253. #else
  254. std::ostringstream stream;
  255. #endif
  256. stream << std::setw(spaces) << value;
  257. #ifdef _WIN32
  258. return WStringToUTF8(stream.str());
  259. #else
  260. return stream.str();
  261. #endif
  262. }
  263. #ifdef _WIN32
  264. std::vector<std::string> CommandLineToUtf8Argv(const wchar_t* command_line);
  265. #endif
  266. std::string GetEscapedHtml(std::string html);
  267. void ToLower(std::string* str);
  268. void ToUpper(std::string* str);
  269. bool CaseInsensitiveEquals(std::string_view a, std::string_view b);
  270. // 'std::less'-like comparison function object type for case-insensitive strings.
  271. struct CaseInsensitiveLess
  272. {
  273. using is_transparent = void; // Allow heterogenous lookup.
  274. bool operator()(std::string_view a, std::string_view b) const;
  275. };
  276. std::string BytesToHexString(std::span<const u8> bytes);
  277. } // namespace Common