@@ -57,28 +57,43 @@ class base32 : public CodecVariant::template codec_impl<base32<CodecVariant>>
5757 : throw std::domain_error (" invalid number of bytes in a tail block" );
5858 }
5959
60- template <uint8_t I> CPPCODEC_ALWAYS_INLINE static constexpr uint8_t index (
61- const uint8_t * b /* binary block*/ )
60+ template <uint8_t I>
61+ CPPCODEC_ALWAYS_INLINE static constexpr uint8_t index (
62+ const uint8_t * b /* binary block*/ ) noexcept
6263 {
64+ static_assert (I >= 0 && I < encoded_block_size (),
65+ " invalid encoding symbol index in a block" );
66+
6367 return (I == 0 ) ? ((b[0 ] >> 3 ) & 0x1F ) // first 5 bits
6468 : (I == 1 ) ? (((b[0 ] << 2 ) & 0x1C ) | ((b[1 ] >> 6 ) & 0x3 ))
6569 : (I == 2 ) ? ((b[1 ] >> 1 ) & 0x1F )
6670 : (I == 3 ) ? (((b[1 ] << 4 ) & 0x10 ) | ((b[2 ] >> 4 ) & 0xF ))
6771 : (I == 4 ) ? (((b[2 ] << 1 ) & 0x1E ) | ((b[3 ] >> 7 ) & 0x1 ))
6872 : (I == 5 ) ? ((b[3 ] >> 2 ) & 0x1F )
6973 : (I == 6 ) ? (((b[3 ] << 3 ) & 0x18 ) | ((b[4 ] >> 5 ) & 0x7 ))
70- : (I == 7 ) ? (b[4 ] & 0x1F ) // last 5 bits
71- : throw std::domain_error (" invalid encoding symbol index in a block" );
74+ : /* I == 7*/ (b[4 ] & 0x1F ); // last 5 bits;
75+ }
76+
77+ template <bool B>
78+ using uint8_if = typename std::enable_if<B, uint8_t >::type;
79+
80+ template <uint8_t I>
81+ CPPCODEC_ALWAYS_INLINE static constexpr
82+ uint8_if<I == 1 || I == 3 || I == 4 || I == 6 > index_last (
83+ const uint8_t * b /* binary block*/ ) noexcept
84+ {
85+ return (I == 1 ) ? ((b[0 ] << 2 ) & 0x1C ) // abbreviated 2nd symbol
86+ : (I == 3 ) ? ((b[1 ] << 4 ) & 0x10 ) // abbreviated 4th symbol
87+ : (I == 4 ) ? ((b[2 ] << 1 ) & 0x1E ) // abbreviated 5th symbol
88+ : /* I == 6*/ ((b[3 ] << 3 ) & 0x18 ); // abbreviated 7th symbol
7289 }
7390
74- template <uint8_t I> CPPCODEC_ALWAYS_INLINE static constexpr uint8_t index_last (
91+ template <uint8_t I>
92+ CPPCODEC_ALWAYS_INLINE static
93+ uint8_if<I != 1 && I != 3 && I != 4 && I != 6 > index_last (
7594 const uint8_t * b /* binary block*/ )
7695 {
77- return (I == 1 ) ? ((b[0 ] << 2 ) & 0x1C ) // abbreviated 2nd symbol
78- : (I == 3 ) ? ((b[1 ] << 4 ) & 0x10 ) // abbreviated 4th symbol
79- : (I == 4 ) ? ((b[2 ] << 1 ) & 0x1E ) // abbreviated 5th symbol
80- : (I == 6 ) ? ((b[3 ] << 3 ) & 0x18 ) // abbreviated 7th symbol
81- : throw std::domain_error (" invalid last encoding symbol index in a tail" );
96+ throw std::domain_error (" invalid last encoding symbol index in a tail" );
8297 }
8398
8499 template <typename Result, typename ResultState>
0 commit comments