@@ -52,78 +52,82 @@ struct fmt::formatter<sdl::GUID> : formatter<std::string> {
5252
5353namespace { // NOLINT(cert-dcl59-cpp,google-build-namespaces)
5454
55- // decode a single_hex_number
56- [[nodiscard]] constexpr const_utils::Expected<u8 , std::string> single_hex_number (char input) {
57- if (input >= ' 0' && input <= ' 9' ) {
58- return const_utils::Expected<u8 , std::string>::good_result (static_cast <u8 >(input - ' 0' ));
59- }
55+ namespace guid {
6056
61- if (input >= ' A' && input <= ' F' ) {
62- return const_utils::Expected<u8 , std::string>::good_result (static_cast <u8 >(input - ' A' + 10 ));
63- }
57+ // decode a single_hex_number
58+ [[nodiscard]] constexpr const_utils::Expected<u8 , std::string> single_hex_number (char input) {
59+ if (input >= ' 0' && input <= ' 9' ) {
60+ return const_utils::Expected<u8 , std::string>::good_result (static_cast <u8 >(input - ' 0' ));
61+ }
62+
63+ if (input >= ' A' && input <= ' F' ) {
64+ return const_utils::Expected<u8 , std::string>::good_result (static_cast <u8 >(input - ' A' + 10 ));
65+ }
6466
65- if (input >= ' a' && input <= ' f' ) {
66- return const_utils::Expected<u8 , std::string>::good_result (static_cast <u8 >(input - ' a' + 10 ));
67+ if (input >= ' a' && input <= ' f' ) {
68+ return const_utils::Expected<u8 , std::string>::good_result (static_cast <u8 >(input - ' a' + 10 ));
69+ }
70+
71+ return const_utils::Expected<u8 , std::string>::error_result (" the input must be a valid hex character" );
6772 }
6873
69- return const_utils::Expected< u8 , std::string>:: error_result ( " the input must be a valid hex character " );
70- }
74+ // decode a single 2 digit color value in hex
75+ [[nodiscard]] constexpr const_utils::Expected< u8 , std::string> single_hex_color_value ( const char * input) {
7176
72- // decode a single 2 digit color value in hex
73- [[nodiscard]] constexpr const_utils::Expected<u8 , std::string> single_hex_color_value (const char * input) {
77+ const auto first = single_hex_number (input[0 ]); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
7478
75- const auto first = single_hex_number (input[ 0 ]); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
79+ PROPAGATE (first, u8 , std::string);
7680
77- PROPAGATE (first, u8 , std::string);
81+ const auto second = single_hex_number (input[ 1 ]); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
7882
79- const auto second = single_hex_number (input[ 1 ]); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
83+ PROPAGATE (second, u8 , std::string);
8084
81- PROPAGATE (second, u8 , std::string);
85+ return const_utils::Expected<u8 , std::string>::good_result ((first.value () << 4 ) | second.value ());
86+ }
8287
83- return const_utils::Expected<u8 , std::string>:: good_result ((first. value () << 4 ) | second. value ());
84- }
88+ [[nodiscard]] constexpr const_utils::Expected<sdl::GUID , std::string>
89+ get_guid_from_string_impl ( const char * input, std:: size_t size) {
8590
86- [[nodiscard]] constexpr const_utils::Expected<sdl::GUID, std::string>
87- get_guid_from_string_impl (const char * input, std::size_t size) {
91+ if (size == 0 ) {
92+ return const_utils::Expected<sdl::GUID, std::string>::error_result (
93+ " not enough data to determine the literal type"
94+ );
95+ }
8896
89- if (size == 0 ) {
90- return const_utils::Expected<sdl::GUID, std::string>::error_result (
91- " not enough data to determine the literal type"
92- );
93- }
97+ constexpr std::size_t amount = 16 ;
9498
95- constexpr std:: size_t amount = 16 ;
99+ size_t width = 2 ;
96100
97- size_t width = 2 ;
101+ if (size == amount * 2 ) {
102+ width = 2 ;
103+ } else if (size == (amount * 2 + (amount - 1 ))) {
104+ width = 3 ;
105+ } else {
98106
99- if (size == amount * 2 ) {
100- width = 2 ;
101- } else if (size == (amount * 2 + (amount - 1 ))) {
102- width = 3 ;
103- } else {
107+ return const_utils::Expected<sdl::GUID, std::string>::error_result (" Unrecognized guid literal" );
108+ }
104109
105- return const_utils::Expected<sdl::GUID, std::string>::error_result (" Unrecognized guid literal" );
106- }
107110
111+ sdl::GUID::ArrayType result{};
108112
109- sdl::GUID::ArrayType result{};
113+ for (size_t i = 0 ; i < amount; ++i) {
114+ const size_t offset = i * width;
110115
111- for (size_t i = 0 ; i < amount; ++i) {
112- const size_t offset = i * width;
113116
117+ const auto temp_result = single_hex_color_value (
118+ input + offset
119+ ); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
114120
115- const auto temp_result =
116- single_hex_color_value (input + offset); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
121+ PROPAGATE (temp_result, sdl::GUID, std::string);
117122
118- PROPAGATE (temp_result, sdl::GUID, std::string );
123+ const auto value = temp_result. value ( );
119124
120- const auto value = temp_result.value ();
125+ result.at (i) = value;
126+ }
121127
122- result. at (i) = value ;
128+ return const_utils::Expected<sdl::GUID, std::string>:: good_result (sdl::GUID{ result }) ;
123129 }
124-
125- return const_utils::Expected<sdl::GUID, std::string>::good_result (sdl::GUID{ result });
126- }
130+ } // namespace guid
127131
128132} // namespace
129133
@@ -132,14 +136,14 @@ namespace detail {
132136
133137 [[nodiscard]] constexpr const_utils::Expected<sdl::GUID, std::string> get_guid_from_string (const std::string& input
134138 ) {
135- return get_guid_from_string_impl (input.c_str (), input.size ());
139+ return guid:: get_guid_from_string_impl (input.c_str (), input.size ());
136140 }
137141
138142} // namespace detail
139143
140144
141145consteval sdl::GUID operator " " _guid(const char * input, std::size_t size) {
142- const auto result = get_guid_from_string_impl (input, size);
146+ const auto result = guid:: get_guid_from_string_impl (input, size);
143147
144148 CONSTEVAL_STATIC_ASSERT (result.has_value (), " incorrect guid literal" );
145149
0 commit comments