Skip to content

Commit 56fccf7

Browse files
authored
Merge pull request #38 from jpetso/master
Fix remaining VC14 warnings
2 parents dd99af9 + 839bbca commit 56fccf7

File tree

3 files changed

+72
-25
lines changed

3 files changed

+72
-25
lines changed

cppcodec/detail/base32.hpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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>

cppcodec/detail/base64.hpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,35 @@ class base64 : public CodecVariant::template codec_impl<base64<CodecVariant>>
5353
: throw std::domain_error("invalid number of bytes in a tail block");
5454
}
5555

56-
template <uint8_t I> CPPCODEC_ALWAYS_INLINE static constexpr uint8_t index(
57-
const uint8_t* b /*binary block*/)
56+
template <uint8_t I>
57+
CPPCODEC_ALWAYS_INLINE static constexpr uint8_t index(
58+
const uint8_t* b /*binary block*/) noexcept
5859
{
60+
static_assert(I >= 0 && I < encoded_block_size(),
61+
"invalid encoding symbol index in a block");
62+
5963
return (I == 0) ? (b[0] >> 2) // first 6 bits
6064
: (I == 1) ? (((b[0] & 0x3) << 4) | (b[1] >> 4))
6165
: (I == 2) ? (((b[1] & 0xF) << 2) | (b[2] >> 6))
62-
: (I == 3) ? (b[2] & 0x3F) // last 6 bits
63-
: throw std::domain_error("invalid encoding symbol index in a block");
66+
: /*I == 3*/ (b[2] & 0x3F); // last 6 bits
67+
}
68+
69+
template <bool B>
70+
using uint8_if = typename std::enable_if<B, uint8_t>::type;
71+
72+
template <uint8_t I>
73+
CPPCODEC_ALWAYS_INLINE static constexpr uint8_if<I == 1 || I == 2> index_last(
74+
const uint8_t* b /*binary block*/) noexcept
75+
{
76+
return (I == 1) ? ((b[0] & 0x3) << 4) // abbreviated 2nd symbol
77+
: /*I == 2*/ ((b[1] & 0xF) << 2); // abbreviated 3rd symbol
6478
}
6579

66-
template <uint8_t I> CPPCODEC_ALWAYS_INLINE static constexpr uint8_t index_last(
80+
template <uint8_t I>
81+
CPPCODEC_ALWAYS_INLINE static uint8_if<I != 1 && I != 2> index_last(
6782
const uint8_t* b /*binary block*/)
6883
{
69-
return (I == 1) ? ((b[0] & 0x3) << 4) // abbreviated 2nd symbol
70-
: (I == 2) ? ((b[1] & 0xF) << 2) // abbreviated 3rd symbol
71-
: throw std::domain_error("invalid last encoding symbol index in a tail");
84+
throw std::domain_error("invalid last encoding symbol index in a tail");
7285
}
7386

7487
template <typename Result, typename ResultState>

cppcodec/detail/hex.hpp

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,40 @@ class hex : public CodecVariant::template codec_impl<hex<CodecVariant>>
6868

6969
static CPPCODEC_ALWAYS_INLINE constexpr uint8_t num_encoded_tail_symbols(uint8_t /*num_bytes*/) noexcept
7070
{
71-
return true ? 0 : throw std::domain_error("no tails in hex encoding, should never be called");
71+
// Hex encoding only works on full bytes so there are no tails,
72+
// no padding characters, and this function should (must) never be called.
73+
return 0;
7274
}
7375

74-
template <uint8_t I> CPPCODEC_ALWAYS_INLINE static constexpr uint8_t index(
76+
template <uint8_t I>
77+
CPPCODEC_ALWAYS_INLINE static constexpr uint8_t index(
7578
const uint8_t* b /*binary block*/) noexcept
7679
{
80+
static_assert(I >= 0 && I < encoded_block_size(),
81+
"invalid encoding symbol index in a block");
82+
7783
return (I == 0) ? (b[0] >> 4) // first 4 bits
78-
: (I == 1) ? (b[0] & 0xF) // last 4 bits
79-
: throw std::domain_error("invalid encoding symbol index in a block");
84+
: /*I == 1*/ (b[0] & 0xF); // last 4 bits
85+
}
86+
87+
// With only 2 bytes, enc<1> will always result in a full index() call and
88+
// enc<0> will be protected by a not-reached assertion, so we don't actually
89+
// care about index_last() except optimizing it out as good as possible.
90+
template <bool B>
91+
using uint8_if = typename std::enable_if<B, uint8_t>::type;
92+
93+
template <uint8_t I>
94+
CPPCODEC_ALWAYS_INLINE static constexpr uint8_if<I == 0> index_last(
95+
const uint8_t* b /*binary block*/) noexcept
96+
{
97+
return 0;
8098
}
8199

82-
template <uint8_t I> CPPCODEC_ALWAYS_INLINE static constexpr uint8_t index_last(
83-
const uint8_t* b /*binary block*/) noexcept
100+
template <uint8_t I>
101+
CPPCODEC_ALWAYS_INLINE static uint8_if<I != 0> index_last(
102+
const uint8_t* b /*binary block*/)
84103
{
85-
return true ? 0 : throw std::domain_error("invalid last encoding symbol index in a tail");
104+
throw std::domain_error("invalid last encoding symbol index in a tail");
86105
}
87106

88107
template <typename Result, typename ResultState>

0 commit comments

Comments
 (0)