common: fs: fs_util: Add more string conversion functions

This commit is contained in:
Morph 2021-06-01 09:19:08 -04:00
parent 91f559a71f
commit a1eeb9908d
2 changed files with 33 additions and 0 deletions

View file

@ -2,6 +2,8 @@
// Licensed under GPLv2 or any later version // Licensed under GPLv2 or any later version
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <algorithm>
#include "common/fs/fs_util.h" #include "common/fs/fs_util.h"
namespace Common::FS { namespace Common::FS {
@ -10,4 +12,12 @@ std::u8string ToU8String(std::string_view utf8_string) {
return std::u8string{utf8_string.begin(), utf8_string.end()}; return std::u8string{utf8_string.begin(), utf8_string.end()};
} }
std::u8string BufferToU8String(std::span<const u8> buffer) {
return std::u8string{buffer.begin(), std::ranges::find(buffer, u8{0})};
}
std::string ToUTF8String(std::u8string_view u8_string) {
return std::string{u8_string.begin(), u8_string.end()};
}
} // namespace Common::FS } // namespace Common::FS

View file

@ -5,9 +5,12 @@
#pragma once #pragma once
#include <concepts> #include <concepts>
#include <span>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include "common/common_types.h"
namespace Common::FS { namespace Common::FS {
template <typename T> template <typename T>
@ -22,4 +25,24 @@ concept IsChar = std::same_as<T, char>;
*/ */
[[nodiscard]] std::u8string ToU8String(std::string_view utf8_string); [[nodiscard]] std::u8string ToU8String(std::string_view utf8_string);
/**
* Converts a buffer of bytes to a UTF8-encoded std::u8string.
* This converts from the start of the buffer until the first encountered null-terminator.
* If no null-terminator is found, this converts the entire buffer instead.
*
* @param buffer Buffer of bytes
*
* @returns UTF-8 encoded std::u8string.
*/
[[nodiscard]] std::u8string BufferToU8String(std::span<const u8> buffer);
/**
* Converts a std::u8string or std::u8string_view to a UTF-8 encoded std::string.
*
* @param u8_string UTF-8 encoded u8string
*
* @returns UTF-8 encoded std::string.
*/
[[nodiscard]] std::string ToUTF8String(std::u8string_view u8_string);
} // namespace Common::FS } // namespace Common::FS