mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-10 13:00:29 +03:00
LibWeb: Use Checked::saturating_[add|sub] where applicable in PixelUnits
This commit is contained in:
parent
618f889486
commit
4896ba11dc
Notes:
sideshowbarker
2024-07-17 05:18:58 +09:00
Author: https://github.com/Hendiadyoin1 Commit: https://github.com/SerenityOS/serenity/commit/4896ba11dc Pull-request: https://github.com/SerenityOS/serenity/pull/20210 Reviewed-by: https://github.com/kalenikaliaksandr ✅
@ -66,12 +66,12 @@ bool CSSPixels::operator==(CSSPixels const& other) const
|
||||
|
||||
CSSPixels& CSSPixels::operator++()
|
||||
{
|
||||
m_value += fixed_point_denominator;
|
||||
m_value = Checked<int>::saturating_add(m_value, fixed_point_denominator);
|
||||
return *this;
|
||||
}
|
||||
CSSPixels& CSSPixels::operator--()
|
||||
{
|
||||
m_value -= fixed_point_denominator;
|
||||
m_value = Checked<int>::saturating_sub(m_value, fixed_point_denominator);
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -91,25 +91,14 @@ CSSPixels CSSPixels::operator-() const
|
||||
return from_raw(-raw_value());
|
||||
}
|
||||
|
||||
static inline int saturated_addition(int a, int b)
|
||||
{
|
||||
i32 overflow = (b > 0 && a > NumericLimits<i32>::max() - b);
|
||||
i32 underflow = (b < 0 && a < NumericLimits<i32>::min() - b);
|
||||
return overflow ? NumericLimits<i32>::max() : (underflow ? NumericLimits<i32>::min() : a + b);
|
||||
}
|
||||
|
||||
CSSPixels CSSPixels::operator+(CSSPixels const& other) const
|
||||
{
|
||||
CSSPixels result;
|
||||
result.set_raw_value(saturated_addition(raw_value(), other.raw_value()));
|
||||
return result;
|
||||
return from_raw(Checked<int>::saturating_add(raw_value(), other.raw_value()));
|
||||
}
|
||||
|
||||
CSSPixels CSSPixels::operator-(CSSPixels const& other) const
|
||||
{
|
||||
CSSPixels result;
|
||||
result.set_raw_value(saturated_addition(raw_value(), -other.raw_value()));
|
||||
return result;
|
||||
return from_raw(Checked<int>::saturating_sub(raw_value(), other.raw_value()));
|
||||
}
|
||||
|
||||
CSSPixels CSSPixels::operator*(CSSPixels const& other) const
|
||||
|
Loading…
Reference in New Issue
Block a user