diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/fs/fs_util.cpp | 10 | ||||
| -rw-r--r-- | src/common/fs/fs_util.h | 23 |
2 files changed, 33 insertions, 0 deletions
diff --git a/src/common/fs/fs_util.cpp b/src/common/fs/fs_util.cpp index 0ddfc3131..9db746c4b 100644 --- a/src/common/fs/fs_util.cpp +++ b/src/common/fs/fs_util.cpp | |||
| @@ -2,6 +2,8 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <algorithm> | ||
| 6 | |||
| 5 | #include "common/fs/fs_util.h" | 7 | #include "common/fs/fs_util.h" |
| 6 | 8 | ||
| 7 | namespace Common::FS { | 9 | namespace Common::FS { |
| @@ -10,4 +12,12 @@ std::u8string ToU8String(std::string_view utf8_string) { | |||
| 10 | return std::u8string{utf8_string.begin(), utf8_string.end()}; | 12 | return std::u8string{utf8_string.begin(), utf8_string.end()}; |
| 11 | } | 13 | } |
| 12 | 14 | ||
| 15 | std::u8string BufferToU8String(std::span<const u8> buffer) { | ||
| 16 | return std::u8string{buffer.begin(), std::ranges::find(buffer, u8{0})}; | ||
| 17 | } | ||
| 18 | |||
| 19 | std::string ToUTF8String(std::u8string_view u8_string) { | ||
| 20 | return std::string{u8_string.begin(), u8_string.end()}; | ||
| 21 | } | ||
| 22 | |||
| 13 | } // namespace Common::FS | 23 | } // namespace Common::FS |
diff --git a/src/common/fs/fs_util.h b/src/common/fs/fs_util.h index 951df53b6..de452c0f6 100644 --- a/src/common/fs/fs_util.h +++ b/src/common/fs/fs_util.h | |||
| @@ -5,9 +5,12 @@ | |||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <concepts> | 7 | #include <concepts> |
| 8 | #include <span> | ||
| 8 | #include <string> | 9 | #include <string> |
| 9 | #include <string_view> | 10 | #include <string_view> |
| 10 | 11 | ||
| 12 | #include "common/common_types.h" | ||
| 13 | |||
| 11 | namespace Common::FS { | 14 | namespace Common::FS { |
| 12 | 15 | ||
| 13 | template <typename T> | 16 | template <typename T> |
| @@ -22,4 +25,24 @@ concept IsChar = std::same_as<T, char>; | |||
| 22 | */ | 25 | */ |
| 23 | [[nodiscard]] std::u8string ToU8String(std::string_view utf8_string); | 26 | [[nodiscard]] std::u8string ToU8String(std::string_view utf8_string); |
| 24 | 27 | ||
| 28 | /** | ||
| 29 | * Converts a buffer of bytes to a UTF8-encoded std::u8string. | ||
| 30 | * This converts from the start of the buffer until the first encountered null-terminator. | ||
| 31 | * If no null-terminator is found, this converts the entire buffer instead. | ||
| 32 | * | ||
| 33 | * @param buffer Buffer of bytes | ||
| 34 | * | ||
| 35 | * @returns UTF-8 encoded std::u8string. | ||
| 36 | */ | ||
| 37 | [[nodiscard]] std::u8string BufferToU8String(std::span<const u8> buffer); | ||
| 38 | |||
| 39 | /** | ||
| 40 | * Converts a std::u8string or std::u8string_view to a UTF-8 encoded std::string. | ||
| 41 | * | ||
| 42 | * @param u8_string UTF-8 encoded u8string | ||
| 43 | * | ||
| 44 | * @returns UTF-8 encoded std::string. | ||
| 45 | */ | ||
| 46 | [[nodiscard]] std::string ToUTF8String(std::u8string_view u8_string); | ||
| 47 | |||
| 25 | } // namespace Common::FS | 48 | } // namespace Common::FS |