string_util.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 <string>
  7. #include <vector>
  8. #include "common/common_types.h"
  9. namespace Common {
  10. /// Make a string lowercase
  11. std::string ToLower(std::string str);
  12. /// Make a string uppercase
  13. std::string ToUpper(std::string str);
  14. std::string StringFromBuffer(const std::vector<u8>& data);
  15. std::string StripSpaces(const std::string& s);
  16. std::string StripQuotes(const std::string& s);
  17. std::string StringFromBool(bool value);
  18. std::string TabsToSpaces(int tab_size, std::string in);
  19. void SplitString(const std::string& str, char delim, std::vector<std::string>& output);
  20. // "C:/Windows/winhelp.exe" to "C:/Windows/", "winhelp", ".exe"
  21. bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename,
  22. std::string* _pExtension);
  23. void BuildCompleteFilename(std::string& _CompleteFilename, const std::string& _Path,
  24. const std::string& _Filename);
  25. std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest);
  26. std::string UTF16ToUTF8(const std::u16string& input);
  27. std::u16string UTF8ToUTF16(const std::string& input);
  28. #ifdef _WIN32
  29. std::string UTF16ToUTF8(const std::wstring& input);
  30. std::wstring UTF8ToUTF16W(const std::string& str);
  31. #endif
  32. /**
  33. * Compares the string defined by the range [`begin`, `end`) to the null-terminated C-string
  34. * `other` for equality.
  35. */
  36. template <typename InIt>
  37. bool ComparePartialString(InIt begin, InIt end, const char* other) {
  38. for (; begin != end && *other != '\0'; ++begin, ++other) {
  39. if (*begin != *other) {
  40. return false;
  41. }
  42. }
  43. // Only return true if both strings finished at the same point
  44. return (begin == end) == (*other == '\0');
  45. }
  46. /**
  47. * Creates a std::string from a fixed-size NUL-terminated char buffer. If the buffer isn't
  48. * NUL-terminated then the string ends at max_len characters.
  49. */
  50. std::string StringFromFixedZeroTerminatedBuffer(const char* buffer, std::size_t max_len);
  51. /**
  52. * Creates a UTF-16 std::u16string from a fixed-size NUL-terminated char buffer. If the buffer isn't
  53. * null-terminated, then the string ends at the greatest multiple of two less then or equal to
  54. * max_len_bytes.
  55. */
  56. std::u16string UTF16StringFromFixedZeroTerminatedBuffer(std::u16string_view buffer,
  57. std::size_t max_len);
  58. /**
  59. * Attempts to trim an arbitrary prefix from `path`, leaving only the part starting at `root`. It's
  60. * intended to be used to strip a system-specific build directory from the `__FILE__` macro,
  61. * leaving only the path relative to the sources root.
  62. *
  63. * @param path The input file path as a null-terminated string
  64. * @param root The name of the root source directory as a null-terminated string. Path up to and
  65. * including the last occurrence of this name will be stripped
  66. * @return A pointer to the same string passed as `path`, but starting at the trimmed portion
  67. */
  68. const char* TrimSourcePath(const char* path, const char* root = "src");
  69. } // namespace Common