summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLenny Maiorani <lenny@colorado.edu>2021-04-18 11:12:03 -0600
committerAndreas Kling <kling@serenityos.org>2021-04-18 22:09:25 +0200
commitc1971df4c7d810cf22300036d451690957e2ea4a (patch)
treeb1d3eb2ba13fc462f0b27c36043d17d90bb2b3c3
parentd462a561630f9768d4c346a73689e30c43f3a992 (diff)
downloadserenity-c1971df4c7d810cf22300036d451690957e2ea4a.zip
AK/Hex: Cleanup implementation
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.
-rw-r--r--AK/Complex.h4
-rw-r--r--AK/Hex.cpp8
-rw-r--r--AK/Tests/TestHex.cpp88
3 files changed, 50 insertions, 50 deletions
diff --git a/AK/Complex.h b/AK/Complex.h
index 6fe71d2efe..7b003795c5 100644
--- a/AK/Complex.h
+++ b/AK/Complex.h
@@ -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)) {
diff --git a/AK/Hex.cpp b/AK/Hex.cpp
index f764eda27e..4c691708b1 100644
--- a/AK/Hex.cpp
+++ b/AK/Hex.cpp
@@ -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 {};
@@ -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);
diff --git a/AK/Tests/TestHex.cpp b/AK/Tests/TestHex.cpp
index b971f82115..640755579a 100644
--- a/AK/Tests/TestHex.cpp
+++ b/AK/Tests/TestHex.cpp
@@ -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)