@@ -39,46 +39,36 @@ static constexpr const char base32_crockford_alphabet[] = {
3939 ' P' , ' Q' , ' R' , ' S' , ' T' , // 27 - no U
4040 ' V' , ' W' , ' X' , ' Y' , ' Z' // 32
4141};
42- static_assert (sizeof (base32_crockford_alphabet) == 32 , " base32 alphabet must have 32 values" );
4342
4443class base32_crockford_base
4544{
4645public:
47- static CPPCODEC_ALWAYS_INLINE constexpr bool generates_padding () { return false ; }
48- static CPPCODEC_ALWAYS_INLINE constexpr bool requires_padding () { return false ; }
49- static CPPCODEC_ALWAYS_INLINE constexpr bool is_padding_symbol ( char /* c */ ) { return false ; }
50-
51- static CPPCODEC_ALWAYS_INLINE constexpr char symbol (uint8_t index )
46+ static CPPCODEC_ALWAYS_INLINE constexpr size_t alphabet_size () {
47+ static_assert ( sizeof (base32_crockford_alphabet) == 32 , " base32 alphabet must have 32 values " );
48+ return sizeof (base32_crockford_alphabet);
49+ }
50+ static CPPCODEC_ALWAYS_INLINE constexpr char symbol (alphabet_index_t idx )
5251 {
53- return base32_crockford_alphabet[index ];
52+ return base32_crockford_alphabet[idx ];
5453 }
55-
56- static CPPCODEC_ALWAYS_INLINE constexpr uint8_t index_of (char c)
54+ static CPPCODEC_ALWAYS_INLINE constexpr char normalized_symbol (char c)
5755 {
58- return (c >= ' 0' && c <= ' 9' ) ? (c - ' 0' )
59- // upper-case letters
60- : (c >= ' A' && c <= ' H' ) ? (c - ' A' + 10 ) // no I
61- : (c >= ' J' && c <= ' K' ) ? (c - ' J' + 18 ) // no L
62- : (c >= ' M' && c <= ' N' ) ? (c - ' M' + 20 ) // no O
63- : (c >= ' P' && c <= ' T' ) ? (c - ' P' + 22 ) // no U
64- : (c >= ' V' && c <= ' Z' ) ? (c - ' V' + 27 )
65- // lower-case letters
66- : (c >= ' a' && c <= ' h' ) ? (c - ' a' + 10 ) // no I
67- : (c >= ' j' && c <= ' k' ) ? (c - ' j' + 18 ) // no L
68- : (c >= ' m' && c <= ' n' ) ? (c - ' m' + 20 ) // no O
69- : (c >= ' p' && c <= ' t' ) ? (c - ' p' + 22 ) // no U
70- : (c >= ' v' && c <= ' z' ) ? (c - ' v' + 27 )
71- : (c == ' -' ) ? 253 // "Hyphens (-) can be inserted into strings [for readability]."
72- : (c == ' \0 ' ) ? 255 // stop at end of string
73- // special cases
74- : (c == ' O' || c == ' o' ) ? 0
75- : (c == ' I' || c == ' i' || c == ' L' || c == ' l' ) ? 1
76- : throw symbol_error (c);
56+ // Hex decoding is always case-insensitive (even in RFC 4648), the question
57+ // is only for encoding whether to use upper-case or lower-case letters.
58+ return (c == ' O' || c == ' o' ) ? ' 0'
59+ : (c == ' I' || c == ' i' || c == ' L' || c == ' l' ) ? ' 1'
60+ : (c >= ' a' && c <= ' z' ) ? (c - ' a' + ' A' )
61+ : c;
7762 }
7863
79- static CPPCODEC_ALWAYS_INLINE constexpr bool should_ignore (uint8_t index) { return index == 253 ; }
80- static CPPCODEC_ALWAYS_INLINE constexpr bool is_special_character (uint8_t index) { return index > 32 ; }
81- static CPPCODEC_ALWAYS_INLINE constexpr bool is_eof (uint8_t index) { return index == 255 ; }
64+ static CPPCODEC_ALWAYS_INLINE constexpr bool generates_padding () { return false ; }
65+ static CPPCODEC_ALWAYS_INLINE constexpr bool requires_padding () { return false ; }
66+ static CPPCODEC_ALWAYS_INLINE constexpr bool is_padding_symbol (char c) { return false ; }
67+ static CPPCODEC_ALWAYS_INLINE constexpr bool is_eof_symbol (char c) { return c == ' \0 ' ; }
68+
69+ static CPPCODEC_ALWAYS_INLINE constexpr bool should_ignore (char c) {
70+ return c == ' -' ; // "Hyphens (-) can be inserted into strings [for readability]."
71+ }
8272};
8373
8474// base32_crockford is a concatenative iterative (i.e. streaming) interpretation of Crockford base32.
0 commit comments