Created
April 7, 2026 06:49
-
-
Save jonwis/3e94ad58a6cec4067630b188aa005ba5 to your computer and use it in GitHub Desktop.
SIDs as user defined literals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <cstdint> | |
| #include <string_view> | |
| #include <algorithm> | |
| #include <ranges> | |
| #include <array> | |
| template<size_t N> | |
| struct string_literal | |
| { | |
| std::array<wchar_t, N> arr_{}; | |
| constexpr string_literal(const char(&in)[N]) | |
| { | |
| std::copy(in, in + N, arr_.begin()); | |
| } | |
| }; | |
| namespace hstrings | |
| { | |
| struct hstring_header | |
| { | |
| uint32_t flags; | |
| uint32_t length; | |
| uint32_t padding[2]; | |
| wchar_t const* c_str; | |
| }; | |
| template<string_literal t> constexpr auto operator ""_hs() | |
| { | |
| return hstring_header { 1, t.arr_.size(), 0, 0, t.arr_.data() }; | |
| } | |
| int use_hstring(hstring_header const &); | |
| constexpr auto whynowork = "barbarbar"_hs; | |
| int j = use_hstring(whynowork); | |
| } | |
| namespace sids | |
| { | |
| template<size_t N> struct sid_storage | |
| { | |
| uint8_t revision; | |
| uint8_t count; | |
| uint32_t authority; | |
| uint32_t subauthorities[N]{}; | |
| }; | |
| template<string_literal t> constexpr auto operator ""_sid() | |
| { | |
| // SIDs we support look like this - "S-1-NNN-YYY-ZZZ-AAA" | |
| constexpr std::wstring_view str{t.arr_.begin(), t.arr_.end()}; | |
| static_assert(str.starts_with(L"S-1-")); | |
| constexpr size_t dashcount = std::count_if(t.arr_.begin(), t.arr_.end(), [](wchar_t x) { return x == '-'; }); | |
| constexpr std::wstring_view remainder{str.substr(4)}; | |
| sid_storage<dashcount - 2> sid; | |
| sid.revision = 1; | |
| sid.count = dashcount; | |
| sid.authority = 5; | |
| sid.subauthorities[0] = 18; | |
| return sid; | |
| } | |
| int use_sid(void const*p); | |
| constexpr auto sid_1 = "S-1-5-18"_sid; | |
| int a = use_sid(std::addressof(sid_1)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment