string_util.h 4.1 KB

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