Skip to content

Commit

Permalink
AK/Hex: Cleanup implementation
Browse files Browse the repository at this point in the history
Problem:
- Post-increment of loop index.
- `const` variables are not marked `const`.
- Incorrect type for loop index.

Solution:
- Pre-increment loop index.
- Mark all possible variables `const`.
- Corret type for loop index.
  • Loading branch information
ldm5180 authored and awesomekling committed Apr 18, 2021
1 parent d462a56 commit c1971df
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 50 deletions.
4 changes: 2 additions & 2 deletions AK/Complex.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,11 @@ static constexpr bool approx_eq(const Complex<T>& a, const Complex<U>& b, const
return x.magnitude() <= margin;
}

//complex version of exp()
// complex version of exp()
template<AK::Concepts::Arithmetic T>
static constexpr Complex<T> cexp(const Complex<T>& a)
{
//FIXME: this can probably be faster and not use so many expensive trigonometric functions
// FIXME: this can probably be faster and not use so many expensive trigonometric functions
if constexpr (sizeof(T) <= sizeof(float)) {
return expf(a.real()) * Complex<T>(cosf(a.imag()), sinf(a.imag()));
} else if constexpr (sizeof(T) <= sizeof(double)) {
Expand Down
8 changes: 4 additions & 4 deletions AK/Hex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ Optional<ByteBuffer> decode_hex(const StringView& input)

auto output = ByteBuffer::create_zeroed(input.length() / 2);

for (long unsigned int i = 0; i < input.length() / 2; i++) {
auto c1 = decode_hex_digit(input[i * 2]);
for (size_t i = 0; i < input.length() / 2; ++i) {
const auto c1 = decode_hex_digit(input[i * 2]);
if (c1 >= 16)
return {};

auto c2 = decode_hex_digit(input[i * 2 + 1]);
const auto c2 = decode_hex_digit(input[i * 2 + 1]);
if (c2 >= 16)
return {};

Expand All @@ -57,7 +57,7 @@ Optional<ByteBuffer> decode_hex(const StringView& input)
return output;
}

String encode_hex(ReadonlyBytes input)
String encode_hex(const ReadonlyBytes input)
{
StringBuilder output(input.size() * 2);

Expand Down
88 changes: 44 additions & 44 deletions AK/Tests/TestHex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,54 +30,54 @@

TEST_CASE(should_decode_hex_digit)
{
EXPECT_EQ(0u, decode_hex_digit('0'));
EXPECT_EQ(1u, decode_hex_digit('1'));
EXPECT_EQ(2u, decode_hex_digit('2'));
EXPECT_EQ(3u, decode_hex_digit('3'));
EXPECT_EQ(4u, decode_hex_digit('4'));
EXPECT_EQ(5u, decode_hex_digit('5'));
EXPECT_EQ(6u, decode_hex_digit('6'));
EXPECT_EQ(7u, decode_hex_digit('7'));
EXPECT_EQ(8u, decode_hex_digit('8'));
EXPECT_EQ(9u, decode_hex_digit('9'));
EXPECT_EQ(10u, decode_hex_digit('a'));
EXPECT_EQ(11u, decode_hex_digit('b'));
EXPECT_EQ(12u, decode_hex_digit('c'));
EXPECT_EQ(13u, decode_hex_digit('d'));
EXPECT_EQ(14u, decode_hex_digit('e'));
EXPECT_EQ(15u, decode_hex_digit('f'));
EXPECT_EQ(10u, decode_hex_digit('A'));
EXPECT_EQ(11u, decode_hex_digit('B'));
EXPECT_EQ(12u, decode_hex_digit('C'));
EXPECT_EQ(13u, decode_hex_digit('D'));
EXPECT_EQ(14u, decode_hex_digit('E'));
EXPECT_EQ(15u, decode_hex_digit('F'));
EXPECT_EQ(0u, decode_hex_digit('0'));
EXPECT_EQ(1u, decode_hex_digit('1'));
EXPECT_EQ(2u, decode_hex_digit('2'));
EXPECT_EQ(3u, decode_hex_digit('3'));
EXPECT_EQ(4u, decode_hex_digit('4'));
EXPECT_EQ(5u, decode_hex_digit('5'));
EXPECT_EQ(6u, decode_hex_digit('6'));
EXPECT_EQ(7u, decode_hex_digit('7'));
EXPECT_EQ(8u, decode_hex_digit('8'));
EXPECT_EQ(9u, decode_hex_digit('9'));
EXPECT_EQ(10u, decode_hex_digit('a'));
EXPECT_EQ(11u, decode_hex_digit('b'));
EXPECT_EQ(12u, decode_hex_digit('c'));
EXPECT_EQ(13u, decode_hex_digit('d'));
EXPECT_EQ(14u, decode_hex_digit('e'));
EXPECT_EQ(15u, decode_hex_digit('f'));
EXPECT_EQ(10u, decode_hex_digit('A'));
EXPECT_EQ(11u, decode_hex_digit('B'));
EXPECT_EQ(12u, decode_hex_digit('C'));
EXPECT_EQ(13u, decode_hex_digit('D'));
EXPECT_EQ(14u, decode_hex_digit('E'));
EXPECT_EQ(15u, decode_hex_digit('F'));
}

TEST_CASE(should_constexpr_decode_hex_digit)
{
static_assert(0u == decode_hex_digit('0'));
static_assert(1u == decode_hex_digit('1'));
static_assert(2u == decode_hex_digit('2'));
static_assert(3u == decode_hex_digit('3'));
static_assert(4u == decode_hex_digit('4'));
static_assert(5u == decode_hex_digit('5'));
static_assert(6u == decode_hex_digit('6'));
static_assert(7u == decode_hex_digit('7'));
static_assert(8u == decode_hex_digit('8'));
static_assert(9u == decode_hex_digit('9'));
static_assert(10u == decode_hex_digit('a'));
static_assert(11u == decode_hex_digit('b'));
static_assert(12u == decode_hex_digit('c'));
static_assert(13u == decode_hex_digit('d'));
static_assert(14u == decode_hex_digit('e'));
static_assert(15u == decode_hex_digit('f'));
static_assert(10u == decode_hex_digit('A'));
static_assert(11u == decode_hex_digit('B'));
static_assert(12u == decode_hex_digit('C'));
static_assert(13u == decode_hex_digit('D'));
static_assert(14u == decode_hex_digit('E'));
static_assert(15u == decode_hex_digit('F'));
static_assert(0u == decode_hex_digit('0'));
static_assert(1u == decode_hex_digit('1'));
static_assert(2u == decode_hex_digit('2'));
static_assert(3u == decode_hex_digit('3'));
static_assert(4u == decode_hex_digit('4'));
static_assert(5u == decode_hex_digit('5'));
static_assert(6u == decode_hex_digit('6'));
static_assert(7u == decode_hex_digit('7'));
static_assert(8u == decode_hex_digit('8'));
static_assert(9u == decode_hex_digit('9'));
static_assert(10u == decode_hex_digit('a'));
static_assert(11u == decode_hex_digit('b'));
static_assert(12u == decode_hex_digit('c'));
static_assert(13u == decode_hex_digit('d'));
static_assert(14u == decode_hex_digit('e'));
static_assert(15u == decode_hex_digit('f'));
static_assert(10u == decode_hex_digit('A'));
static_assert(11u == decode_hex_digit('B'));
static_assert(12u == decode_hex_digit('C'));
static_assert(13u == decode_hex_digit('D'));
static_assert(14u == decode_hex_digit('E'));
static_assert(15u == decode_hex_digit('F'));
}

TEST_MAIN(Hex)

0 comments on commit c1971df

Please sign in to comment.