fs_util.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <concepts>
  5. #include <filesystem>
  6. #include <span>
  7. #include <string>
  8. #include "common/common_types.h"
  9. namespace Common::FS {
  10. template <typename T>
  11. concept IsChar = std::same_as<T, char>;
  12. /**
  13. * Converts a UTF-8 encoded std::string or std::string_view to a std::u8string.
  14. *
  15. * @param utf8_string UTF-8 encoded string
  16. *
  17. * @returns UTF-8 encoded std::u8string.
  18. */
  19. [[nodiscard]] std::u8string ToU8String(std::string_view utf8_string);
  20. /**
  21. * Converts a buffer of bytes to a UTF8-encoded std::u8string.
  22. * This converts from the start of the buffer until the first encountered null-terminator.
  23. * If no null-terminator is found, this converts the entire buffer instead.
  24. *
  25. * @param buffer Buffer of bytes
  26. *
  27. * @returns UTF-8 encoded std::u8string.
  28. */
  29. [[nodiscard]] std::u8string BufferToU8String(std::span<const u8> buffer);
  30. /**
  31. * Same as BufferToU8String, but returns a string view of the buffer.
  32. *
  33. * @param buffer Buffer of bytes
  34. *
  35. * @returns UTF-8 encoded std::u8string_view.
  36. */
  37. [[nodiscard]] std::u8string_view BufferToU8StringView(std::span<const u8> buffer);
  38. /**
  39. * Converts a std::u8string or std::u8string_view to a UTF-8 encoded std::string.
  40. *
  41. * @param u8_string UTF-8 encoded u8string
  42. *
  43. * @returns UTF-8 encoded std::string.
  44. */
  45. [[nodiscard]] std::string ToUTF8String(std::u8string_view u8_string);
  46. /**
  47. * Converts a buffer of bytes to a UTF8-encoded std::string.
  48. * This converts from the start of the buffer until the first encountered null-terminator.
  49. * If no null-terminator is found, this converts the entire buffer instead.
  50. *
  51. * @param buffer Buffer of bytes
  52. *
  53. * @returns UTF-8 encoded std::string.
  54. */
  55. [[nodiscard]] std::string BufferToUTF8String(std::span<const u8> buffer);
  56. /**
  57. * Same as BufferToUTF8String, but returns a string view of the buffer.
  58. *
  59. * @param buffer Buffer of bytes
  60. *
  61. * @returns UTF-8 encoded std::string_view.
  62. */
  63. [[nodiscard]] std::string_view BufferToUTF8StringView(std::span<const u8> buffer);
  64. /**
  65. * Converts a filesystem path to a UTF-8 encoded std::string.
  66. *
  67. * @param path Filesystem path
  68. *
  69. * @returns UTF-8 encoded std::string.
  70. */
  71. [[nodiscard]] std::string PathToUTF8String(const std::filesystem::path& path);
  72. } // namespace Common::FS