From 8a6462a40c0612d06771f4fbb4a60061d0cad5e8 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Tue, 10 Oct 2023 16:38:57 +0200 Subject: [PATCH] string: add uuidtostr Signed-off-by: Simon Rozman --- include/stdex/string.hpp | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/include/stdex/string.hpp b/include/stdex/string.hpp index 133fee7b7..89f36e732 100644 --- a/include/stdex/string.hpp +++ b/include/stdex/string.hpp @@ -16,6 +16,9 @@ #ifdef __APPLE__ #include #endif +#ifndef _WIN32 +#include +#endif #include #include #include @@ -1394,6 +1397,56 @@ namespace stdex return str; } + /// + /// Formats GUID to a registry string {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}. + /// + /// \param[out] str String to write GUID. Must point to at least 39 code points to write complete GUID including zero terminator. + /// \param[in ] id GUID to write. + /// + inline void uuidtostr(_Out_writes_z_(39) char str[39], _In_ const uuid_t& id) + { + _Assume_(str); + _snprintf_s_l(str, 39, _TRUNCATE, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}", NULL, +#ifdef _WIN32 + id.Data1, + static_cast(id.Data2), + static_cast(id.Data3), + static_cast(id.Data4[0]), static_cast(id.Data4[1]), + static_cast(id.Data4[2]), static_cast(id.Data4[3]), static_cast(id.Data4[4]), static_cast(id.Data4[5]), static_cast(id.Data4[6]), static_cast(id.Data4[7])); +#else + *reinterpret_cast(&id[0]), + static_cast(*reinterpret_cast(&id[4])), + static_cast(*reinterpret_cast(&id[6])), + static_cast(id[8]), static_cast(id[9]), + static_cast(id[10])), static_cast(id[11]), static_cast(id[12]), static_cast(id)), static_cast(id[14]), static_cast(id[15])); +#endif + } + + /// + /// Formats GUID to a registry string {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}. + /// + /// \param[out] str String to write GUID. Must point to at least 39 code points to write complete GUID including zero terminator. + /// \param[in ] id GUID to write. + /// + inline void uuidtostr(_Out_writes_z_(39) wchar_t str[39], _In_ const uuid_t& id) + { + _Assume_(str); + _snwprintf_s_l(str, 39, _TRUNCATE, L"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}", NULL, +#ifdef _WIN32 + id.Data1, + static_cast(id.Data2), + static_cast(id.Data3), + static_cast(id.Data4[0]), static_cast(id.Data4[1]), + static_cast(id.Data4[2]), static_cast(id.Data4[3]), static_cast(id.Data4[4]), static_cast(id.Data4[5]), static_cast(id.Data4[6]), static_cast(id.Data4[7])); +#else + *reinterpret_cast(&id[0]), + static_cast(*reinterpret_cast(&id[4])), + static_cast(*reinterpret_cast(&id[6])), + static_cast(id[8]), static_cast(id[9]), + static_cast(id[10])), static_cast(id[11]), static_cast(id[12]), static_cast(id)), static_cast(id[14]), static_cast(id[15])); +#endif + } + /// /// Convert string to lower-case character-by-character ///