string_util.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // SPDX-FileCopyrightText: 2013 Dolphin Emulator Project
  2. // SPDX-FileCopyrightText: 2014 Citra Emulator Project
  3. // SPDX-License-Identifier: GPL-2.0-or-later
  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. [[nodiscard]] std::string ReplaceAll(std::string result, const std::string& src,
  24. const std::string& dest);
  25. [[nodiscard]] std::string UTF16ToUTF8(const std::u16string& input);
  26. [[nodiscard]] std::u16string UTF8ToUTF16(const std::string& input);
  27. #ifdef _WIN32
  28. [[nodiscard]] std::string UTF16ToUTF8(const std::wstring& input);
  29. [[nodiscard]] std::wstring UTF8ToUTF16W(const std::string& str);
  30. #endif
  31. [[nodiscard]] std::u16string U16StringFromBuffer(const u16* input, std::size_t length);
  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. [[nodiscard]] 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. [[nodiscard]] std::string StringFromFixedZeroTerminatedBuffer(std::string_view buffer,
  51. std::size_t max_len);
  52. /**
  53. * Creates a UTF-16 std::u16string from a fixed-size NUL-terminated char buffer. If the buffer isn't
  54. * null-terminated, then the string ends at the greatest multiple of two less then or equal to
  55. * max_len_bytes.
  56. */
  57. [[nodiscard]] std::u16string UTF16StringFromFixedZeroTerminatedBuffer(std::u16string_view buffer,
  58. std::size_t max_len);
  59. } // namespace Common