Skip to content

Commit 334ed44

Browse files
committed
Allow digit separators between digit pairs
1 parent a86f3ce commit 334ed44

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

include/tao/json/binary.hpp

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,45 @@ namespace tao
3434
}
3535
};
3636

37+
template< typename T, typename V, V... Vs, char C >
38+
struct unhex_helper< T, vlist< V, Vs... >, C >
39+
: unhex_helper< T, vlist< V > >
40+
{
41+
static_assert( sizeof( T ) == 0, "digits must occur in pairs" );
42+
};
43+
44+
template< typename T, typename V, V... Vs, char C1, char... Cs >
45+
struct unhex_helper< T, vlist< V, Vs... >, '\'', C1, Cs... >
46+
: unhex_helper< T, vlist< V, Vs... >, C1, Cs... >
47+
{
48+
};
49+
50+
template< typename T, typename V, V... Vs, char C0, char... Cs >
51+
struct unhex_helper< T, vlist< V, Vs... >, C0, '\'', Cs... >
52+
: unhex_helper< T, vlist< V > >
53+
{
54+
static_assert( sizeof( T ) == 0, "digit separator only allowed between pairs of digits" );
55+
};
56+
3757
template< typename T, typename V, V... Vs, char C0, char C1, char... Cs >
3858
struct unhex_helper< T, vlist< V, Vs... >, C0, C1, Cs... >
39-
: unhex_helper< T, vlist< V, Vs..., V( ( C0 << 4 ) + C1 ) >, Cs... >
59+
: unhex_helper< T, vlist< V, Vs..., V( ( unhex_char( C0 ) << 4 ) + unhex_char( C1 ) ) >, Cs... >
4060
{
4161
};
4262

63+
template< typename T, typename V, char C >
64+
constexpr T unhex()
65+
{
66+
static_assert( sizeof( T ) == 0, "not a hex literal" );
67+
return T{};
68+
}
69+
4370
template< typename T, typename V, char C0, char C1, char... Cs >
4471
constexpr T unhex()
4572
{
4673
static_assert( C0 == '0', "not a hex literal" );
4774
static_assert( C1 == 'x' || C1 == 'X', "not a hex literal" );
48-
static_assert( sizeof...( Cs ) % 2 == 0, "invalid number of hexadecimal digits" );
49-
return unhex_helper< T, vlist< V >, unhex_char( Cs )... >::unhex();
75+
return unhex_helper< T, vlist< V >, Cs... >::unhex();
5076
}
5177

5278
template< typename T, char... Cs >

0 commit comments

Comments
 (0)