string_util.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #pragma once
  5. #include <cstdarg>
  6. #include <cstddef>
  7. #include <iomanip>
  8. #include <sstream>
  9. #include <string>
  10. #include <vector>
  11. #include "common/common_types.h"
  12. namespace Common {
  13. /// Make a string lowercase
  14. std::string ToLower(std::string str);
  15. /// Make a string uppercase
  16. std::string ToUpper(std::string str);
  17. std::string StringFromFormat(const char* format, ...);
  18. // Cheap!
  19. bool CharArrayFromFormatV(char* out, int outsize, const char* format, va_list args);
  20. template <size_t Count>
  21. inline void CharArrayFromFormat(char (&out)[Count], const char* format, ...) {
  22. va_list args;
  23. va_start(args, format);
  24. CharArrayFromFormatV(out, Count, format, args);
  25. va_end(args);
  26. }
  27. // Good
  28. std::string ArrayToString(const u8* data, size_t size, int line_len = 20, bool spaces = true);
  29. std::string StripSpaces(const std::string& s);
  30. std::string StripQuotes(const std::string& s);
  31. // Thousand separator. Turns 12345678 into 12,345,678
  32. template <typename I>
  33. std::string ThousandSeparate(I value, int spaces = 0) {
  34. std::ostringstream oss;
  35. // std::locale("") seems to be broken on many platforms
  36. #if defined _WIN32 || (defined __linux__ && !defined __clang__)
  37. oss.imbue(std::locale(""));
  38. #endif
  39. oss << std::setw(spaces) << value;
  40. return oss.str();
  41. }
  42. std::string StringFromBool(bool value);
  43. bool TryParse(const std::string& str, bool* output);
  44. bool TryParse(const std::string& str, u32* output);
  45. template <typename N>
  46. static bool TryParse(const std::string& str, N* const output) {
  47. std::istringstream iss(str);
  48. N tmp = 0;
  49. if (iss >> tmp) {
  50. *output = tmp;
  51. return true;
  52. } else
  53. return false;
  54. }
  55. // TODO: kill this
  56. bool AsciiToHex(const char* _szValue, u32& result);
  57. std::string TabsToSpaces(int tab_size, const std::string& in);
  58. void SplitString(const std::string& str, char delim, std::vector<std::string>& output);
  59. // "C:/Windows/winhelp.exe" to "C:/Windows/", "winhelp", ".exe"
  60. bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename,
  61. std::string* _pExtension);
  62. void BuildCompleteFilename(std::string& _CompleteFilename, const std::string& _Path,
  63. const std::string& _Filename);
  64. std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest);
  65. std::string UTF16ToUTF8(const std::u16string& input);
  66. std::u16string UTF8ToUTF16(const std::string& input);
  67. std::string CP1252ToUTF8(const std::string& str);
  68. std::string SHIFTJISToUTF8(const std::string& str);
  69. #ifdef _WIN32
  70. std::string UTF16ToUTF8(const std::wstring& input);
  71. std::wstring UTF8ToUTF16W(const std::string& str);
  72. #ifdef _UNICODE
  73. inline std::string TStrToUTF8(const std::wstring& str) {
  74. return UTF16ToUTF8(str);
  75. }
  76. inline std::wstring UTF8ToTStr(const std::string& str) {
  77. return UTF8ToUTF16W(str);
  78. }
  79. #else
  80. inline std::string TStrToUTF8(const std::string& str) {
  81. return str;
  82. }
  83. inline std::string UTF8ToTStr(const std::string& str) {
  84. return str;
  85. }
  86. #endif
  87. #endif
  88. /**
  89. * Compares the string defined by the range [`begin`, `end`) to the null-terminated C-string
  90. * `other` for equality.
  91. */
  92. template <typename InIt>
  93. bool ComparePartialString(InIt begin, InIt end, const char* other) {
  94. for (; begin != end && *other != '\0'; ++begin, ++other) {
  95. if (*begin != *other) {
  96. return false;
  97. }
  98. }
  99. // Only return true if both strings finished at the same point
  100. return (begin == end) == (*other == '\0');
  101. }
  102. /**
  103. * Creates a std::string from a fixed-size NUL-terminated char buffer. If the buffer isn't
  104. * NUL-terminated then the string ends at max_len characters.
  105. */
  106. std::string StringFromFixedZeroTerminatedBuffer(const char* buffer, size_t max_len);
  107. /**
  108. * Attempts to trim an arbitrary prefix from `path`, leaving only the part starting at `root`. It's
  109. * intended to be used to strip a system-specific build directory from the `__FILE__` macro,
  110. * leaving only the path relative to the sources root.
  111. *
  112. * @param path The input file path as a null-terminated string
  113. * @param root The name of the root source directory as a null-terminated string. Path up to and
  114. * including the last occurrence of this name will be stripped
  115. * @return A pointer to the same string passed as `path`, but starting at the trimmed portion
  116. */
  117. const char* TrimSourcePath(const char* path, const char* root = "src");
  118. } // namespace Common