From 514216d54a7ec218d089964dca48fed25b795ea8 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Thu, 4 Jun 2026 21:38:49 +0200 Subject: upgrade to concepts and string views --- include/winapi/utf8.hpp | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'include/winapi') diff --git a/include/winapi/utf8.hpp b/include/winapi/utf8.hpp index 2e87f75..0307507 100644 --- a/include/winapi/utf8.hpp +++ b/include/winapi/utf8.hpp @@ -10,9 +10,11 @@ #pragma once +#include #include #include #include +#include #include namespace winapi { @@ -21,10 +23,8 @@ namespace winapi { static_assert(sizeof(wchar_t) == 2, "This is Windows, right?"); /** Convert UTF-8 string to UTF-16. */ -std::wstring widen(const std::string&); -#ifdef __cpp_lib_char8_t -std::wstring widen(const std::u8string&); -#endif +std::wstring widen(std::string_view); +std::wstring widen(std::u8string_view); /** * Convert UTF-8 string to UTF-16. @@ -33,20 +33,24 @@ std::wstring widen(const std::u8string&); */ std::wstring widen(const void* src, std::size_t nb); +template +concept CanBeWidened = + std::same_as || std::same_as || std::same_as; + /** * Convert UTF-8 string to UTF-16. * \param src Buffer holding a UTF-8 string. */ -template > +template > std::wstring widen(const std::vector& src) { return widen(src.data(), src.size() * sizeof(T)); } /** Convert UTF-16 string to UTF-8. */ -std::string narrow(const std::wstring&); +std::string narrow(std::wstring_view); /** Convert UTF-16 string to UTF-8. */ -std::string narrow(const std::u16string&); +std::string narrow(std::u16string_view); /** * Convert UTF-16 string to UTF-8. @@ -55,11 +59,15 @@ std::string narrow(const std::u16string&); */ std::string narrow(const void* src, std::size_t nb); +template +concept CanBeNarrowed = + std::same_as || std::same_as || std::same_as; + /** * Convert UTF-16 string to UTF-8. * \param src Buffer holding a UTF-16 string. */ -template > +template > std::string narrow(const std::vector& src) { return narrow(src.data(), src.size() * sizeof(T)); } -- cgit v1.3