string_util.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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, std::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. #ifdef _WIN32
  55. std::string UTF16ToUTF8(const std::wstring& input);
  56. std::wstring UTF8ToUTF16W(const std::string& str);
  57. #endif
  58. /**
  59. * Compares the string defined by the range [`begin`, `end`) to the null-terminated C-string
  60. * `other` for equality.
  61. */
  62. template <typename InIt>
  63. bool ComparePartialString(InIt begin, InIt end, const char* other) {
  64. for (; begin != end && *other != '\0'; ++begin, ++other) {
  65. if (*begin != *other) {
  66. return false;
  67. }
  68. }
  69. // Only return true if both strings finished at the same point
  70. return (begin == end) == (*other == '\0');
  71. }
  72. /**
  73. * Creates a std::string from a fixed-size NUL-terminated char buffer. If the buffer isn't
  74. * NUL-terminated then the string ends at max_len characters.
  75. */
  76. std::string StringFromFixedZeroTerminatedBuffer(const char* buffer, std::size_t max_len);
  77. /**
  78. * Attempts to trim an arbitrary prefix from `path`, leaving only the part starting at `root`. It's
  79. * intended to be used to strip a system-specific build directory from the `__FILE__` macro,
  80. * leaving only the path relative to the sources root.
  81. *
  82. * @param path The input file path as a null-terminated string
  83. * @param root The name of the root source directory as a null-terminated string. Path up to and
  84. * including the last occurrence of this name will be stripped
  85. * @return A pointer to the same string passed as `path`, but starting at the trimmed portion
  86. */
  87. const char* TrimSourcePath(const char* path, const char* root = "src");
  88. } // namespace Common