diff options
| author | 2022-02-13 18:53:21 -0500 | |
|---|---|---|
| committer | 2022-02-13 18:53:21 -0500 | |
| commit | 4390370a19190ba2223739bd4d98b460d0e680a8 (patch) | |
| tree | e4249df78ef8c542cea1d51e8febc1f0e9800c7a /src/common/fs/fs_util.cpp | |
| parent | Merge pull request #7887 from lat9nq/stub-is-usb-full-key (diff) | |
| download | yuzu-4390370a19190ba2223739bd4d98b460d0e680a8.tar.gz yuzu-4390370a19190ba2223739bd4d98b460d0e680a8.tar.xz yuzu-4390370a19190ba2223739bd4d98b460d0e680a8.zip | |
common: fs_util: Add buffer to string view utility functions
These functions allow to construct a string view from an input buffer, avoiding the copy done by the non string view counterparts. However, callers must be cognizant of the viewed buffer's lifetime to avoid a use-after-free.
Diffstat (limited to 'src/common/fs/fs_util.cpp')
| -rw-r--r-- | src/common/fs/fs_util.cpp | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/src/common/fs/fs_util.cpp b/src/common/fs/fs_util.cpp index 9f8671982..0068112e6 100644 --- a/src/common/fs/fs_util.cpp +++ b/src/common/fs/fs_util.cpp | |||
| @@ -16,6 +16,10 @@ std::u8string BufferToU8String(std::span<const u8> buffer) { | |||
| 16 | return std::u8string{buffer.begin(), std::ranges::find(buffer, u8{0})}; | 16 | return std::u8string{buffer.begin(), std::ranges::find(buffer, u8{0})}; |
| 17 | } | 17 | } |
| 18 | 18 | ||
| 19 | std::u8string_view BufferToU8StringView(std::span<const u8> buffer) { | ||
| 20 | return std::u8string_view{reinterpret_cast<const char8_t*>(buffer.data())}; | ||
| 21 | } | ||
| 22 | |||
| 19 | std::string ToUTF8String(std::u8string_view u8_string) { | 23 | std::string ToUTF8String(std::u8string_view u8_string) { |
| 20 | return std::string{u8_string.begin(), u8_string.end()}; | 24 | return std::string{u8_string.begin(), u8_string.end()}; |
| 21 | } | 25 | } |
| @@ -24,6 +28,10 @@ std::string BufferToUTF8String(std::span<const u8> buffer) { | |||
| 24 | return std::string{buffer.begin(), std::ranges::find(buffer, u8{0})}; | 28 | return std::string{buffer.begin(), std::ranges::find(buffer, u8{0})}; |
| 25 | } | 29 | } |
| 26 | 30 | ||
| 31 | std::string_view BufferToUTF8StringView(std::span<const u8> buffer) { | ||
| 32 | return std::string_view{reinterpret_cast<const char*>(buffer.data())}; | ||
| 33 | } | ||
| 34 | |||
| 27 | std::string PathToUTF8String(const std::filesystem::path& path) { | 35 | std::string PathToUTF8String(const std::filesystem::path& path) { |
| 28 | return ToUTF8String(path.u8string()); | 36 | return ToUTF8String(path.u8string()); |
| 29 | } | 37 | } |