Skip to content

Commit

Permalink
Enable clang-tidy readability-magic-numbers check
Browse files Browse the repository at this point in the history
Adds names for integer bases used with details::parse_number.

Signed-off-by: Sean Robinson <[email protected]>
  • Loading branch information
skrobinson committed Feb 7, 2022
1 parent b918763 commit 9eb1fe5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
1 change: 0 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ Checks:
readability-*,
-readability-else-after-return,
-readability-function-cognitive-complexity,
-readability-magic-numbers,
-readability-named-parameter,
-readability-qualified-auto,
-readability-static-accessed-through-instance,
Expand Down
22 changes: 13 additions & 9 deletions include/argparse/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ constexpr bool standard_unsigned_integer<unsigned long long int> = true;

} // namespace

constexpr int radix_8 = 8;
constexpr int radix_10 = 10;
constexpr int radix_16 = 16;

template <typename T>
constexpr bool standard_integer =
standard_signed_integer<T> || standard_unsigned_integer<T>;
Expand Down Expand Up @@ -219,10 +223,10 @@ template <class T, auto Param = 0> struct parse_number {
}
};

template <class T> struct parse_number<T, 16> {
template <class T> struct parse_number<T, radix_16> {
auto operator()(std::string_view s) -> T {
if (auto [ok, rest] = consume_hex_prefix(s); ok) {
return do_from_chars<T, 16>(rest);
return do_from_chars<T, radix_16>(rest);
} else {
throw std::invalid_argument{"pattern not found"};
}
Expand All @@ -232,11 +236,11 @@ template <class T> struct parse_number<T, 16> {
template <class T> struct parse_number<T> {
auto operator()(std::string_view s) -> T {
if (auto [ok, rest] = consume_hex_prefix(s); ok) {
return do_from_chars<T, 16>(rest);
return do_from_chars<T, radix_16>(rest);
} else if (starts_with("0"sv, s)) {
return do_from_chars<T, 8>(rest);
return do_from_chars<T, radix_8>(rest);
} else {
return do_from_chars<T, 10>(rest);
return do_from_chars<T, radix_10>(rest);
}
}
};
Expand Down Expand Up @@ -418,18 +422,18 @@ class Argument {
};

if constexpr (is_one_of(Shape, 'd') && details::standard_integer<T>) {
action(details::parse_number<T, 10>());
action(details::parse_number<T, details::radix_10>());
} else if constexpr (is_one_of(Shape, 'i') && details::standard_integer<T>) {
action(details::parse_number<T>());
} else if constexpr (is_one_of(Shape, 'u') &&
details::standard_unsigned_integer<T>) {
action(details::parse_number<T, 10>());
action(details::parse_number<T, details::radix_10>());
} else if constexpr (is_one_of(Shape, 'o') &&
details::standard_unsigned_integer<T>) {
action(details::parse_number<T, 8>());
action(details::parse_number<T, details::radix_8>());
} else if constexpr (is_one_of(Shape, 'x', 'X') &&
details::standard_unsigned_integer<T>) {
action(details::parse_number<T, 16>());
action(details::parse_number<T, details::radix_16>());
} else if constexpr (is_one_of(Shape, 'a', 'A') &&
std::is_floating_point_v<T>) {
action(details::parse_number<T, details::chars_format::hex>());
Expand Down

0 comments on commit 9eb1fe5

Please sign in to comment.