string_util.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. [[nodiscard]] std::string ToLower(std::string str);
  12. /// Make a string uppercase
  13. [[nodiscard]] std::string ToUpper(std::string str);
  14. [[nodiscard]] std::string StringFromBuffer(const std::vector<u8>& data);
  15. [[nodiscard]] std::string StripSpaces(const std::string& s);
  16. [[nodiscard]] std::string StripQuotes(const std::string& s);
  17. [[nodiscard]] std::string StringFromBool(bool value);
  18. [[nodiscard]] 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. [[nodiscard]] std::string ReplaceAll(std::string result, const std::string& src,
  26. const std::string& dest);
  27. [[nodiscard]] std::string UTF16ToUTF8(const std::u16string& input);
  28. [[nodiscard]] std::u16string UTF8ToUTF16(const std::string& input);
  29. #ifdef _WIN32
  30. [[nodiscard]] std::string UTF16ToUTF8(const std::wstring& input);
  31. [[nodiscard]] std::wstring UTF8ToUTF16W(const std::string& str);
  32. #endif
  33. /**
  34. * Compares the string defined by the range [`begin`, `end`) to the null-terminated C-string
  35. * `other` for equality.
  36. */
  37. template <typename InIt>
  38. [[nodiscard]] bool ComparePartialString(InIt begin, InIt end, const char* other) {
  39. for (; begin != end && *other != '\0'; ++begin, ++other) {
  40. if (*begin != *other) {
  41. return false;
  42. }
  43. }
  44. // Only return true if both strings finished at the same point
  45. return (begin == end) == (*other == '\0');
  46. }
  47. /**
  48. * Creates a std::string from a fixed-size NUL-terminated char buffer. If the buffer isn't
  49. * NUL-terminated then the string ends at max_len characters.
  50. */
  51. [[nodiscard]] std::string StringFromFixedZeroTerminatedBuffer(const char* buffer,
  52. std::size_t max_len);
  53. /**
  54. * Creates a UTF-16 std::u16string from a fixed-size NUL-terminated char buffer. If the buffer isn't
  55. * null-terminated, then the string ends at the greatest multiple of two less then or equal to
  56. * max_len_bytes.
  57. */
  58. [[nodiscard]] std::u16string UTF16StringFromFixedZeroTerminatedBuffer(std::u16string_view buffer,
  59. std::size_t max_len);
  60. } // namespace Common