From 4390370a19190ba2223739bd4d98b460d0e680a8 Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Sun, 13 Feb 2022 18:53:21 -0500 Subject: 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. --- src/common/fs/fs_util.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/common/fs/fs_util.h') diff --git a/src/common/fs/fs_util.h b/src/common/fs/fs_util.h index 1ec82eb35..1620d38c9 100644 --- a/src/common/fs/fs_util.h +++ b/src/common/fs/fs_util.h @@ -37,6 +37,15 @@ concept IsChar = std::same_as; */ [[nodiscard]] std::u8string BufferToU8String(std::span buffer); +/** + * Same as BufferToU8String, but returns a string view of the buffer. + * + * @param buffer Buffer of bytes + * + * @returns UTF-8 encoded std::u8string_view. + */ +[[nodiscard]] std::u8string_view BufferToU8StringView(std::span buffer); + /** * Converts a std::u8string or std::u8string_view to a UTF-8 encoded std::string. * @@ -57,6 +66,15 @@ concept IsChar = std::same_as; */ [[nodiscard]] std::string BufferToUTF8String(std::span buffer); +/** + * Same as BufferToUTF8String, but returns a string view of the buffer. + * + * @param buffer Buffer of bytes + * + * @returns UTF-8 encoded std::string_view. + */ +[[nodiscard]] std::string_view BufferToUTF8StringView(std::span buffer); + /** * Converts a filesystem path to a UTF-8 encoded std::string. * -- cgit v1.2.3