string_util.h 4.1 KB

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