Skip to content

Commit a65da97

Browse files
committed
Factor min/max constants in in_range
Improve readability. This commit should not change behavior.
1 parent 56065f2 commit a65da97

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/quick-lint-js/narrow-cast.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,18 @@ struct numeric_limits<char8_t> {
5252
// TODO(strager): Use std::in_range if supported.
5353
template <class Out, class In>
5454
constexpr bool in_range([[maybe_unused]] In x) noexcept {
55-
using out_limits = numeric_limits<Out>;
55+
[[maybe_unused]] constexpr Out min_out = numeric_limits<Out>::lowest();
56+
[[maybe_unused]] constexpr Out max_out = (numeric_limits<Out>::max)();
5657
using unsigned_in = make_unsigned_t<In>;
5758
using unsigned_out = make_unsigned_t<Out>;
5859
if constexpr (std::is_same_v<In, Out>) {
5960
return true;
6061
} else if constexpr (std::is_signed_v<In> == std::is_signed_v<Out>) {
61-
return out_limits::lowest() <= x && x <= (out_limits::max)();
62+
return min_out <= x && x <= max_out;
6263
} else if constexpr (std::is_signed_v<In> && !std::is_signed_v<Out>) {
63-
return 0 <= x && static_cast<unsigned_in>(x) <= (out_limits::max)();
64+
return 0 <= x && static_cast<unsigned_in>(x) <= max_out;
6465
} else if constexpr (!std::is_signed_v<In> && std::is_signed_v<Out>) {
65-
return x <= unsigned_out{(out_limits::max)()};
66+
return x <= unsigned_out{max_out};
6667
}
6768
}
6869

0 commit comments

Comments
 (0)