@@ -576,32 +576,35 @@ geom::geometry_t ewkb_to_geom(std::string_view wkb)
576576 return geom;
577577}
578578
579- unsigned char decode_hex_char (char c)
580- {
581- if (c >= ' 0' && c <= ' 9' ) {
582- return c - ' 0' ;
583- }
584- if (c >= ' A' && c <= ' F' ) {
585- return c - ' A' + 10 ;
586- }
587- if (c >= ' a' && c <= ' f' ) {
588- return c - ' a' + 10 ;
589- }
579+ static constexpr std::array<char , 256 > const hex_table = {
580+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
581+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
582+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
583+ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 , 0 , 0 , 0 , 0 , 0 ,
584+
585+ 0 , 10 , 11 , 12 , 13 , 14 , 15 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
586+ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
587+ 0 , 10 , 11 , 12 , 13 , 14 , 15 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
588+ };
590589
591- throw std::runtime_error{" Invalid wkb: Not a hex character" };
590+ unsigned char decode_hex_char (char c) noexcept
591+ {
592+ // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-constant-array-index)
593+ return hex_table[static_cast <std::size_t >(static_cast <unsigned char >(c))];
592594}
593595
594- std::string decode_hex (char const *hex )
596+ std::string decode_hex (std::string_view hex_string )
595597{
598+ if (hex_string.size () % 2 != 0 ) {
599+ throw std::runtime_error{" Invalid wkb: Not a valid hex string" };
600+ }
601+
596602 std::string wkb;
603+ wkb.reserve (hex_string.size () / 2 );
597604
598- while (*hex != ' \0 ' ) {
605+ // NOLINTNEXTLINE(llvm-qualified-auto, readability-qualified-auto)
606+ for (auto hex = hex_string.begin (); hex != hex_string.end ();) {
599607 unsigned int const c = decode_hex_char (*hex++);
600-
601- if (*hex == ' \0 ' ) {
602- throw std::runtime_error{" Invalid wkb: Not a valid hex string" };
603- }
604-
605608 wkb += static_cast <char >((c << 4U ) | decode_hex_char (*hex++));
606609 }
607610
0 commit comments