1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-11 18:20:43 +09:00

LibSQL: Implement converting float and tuple values to a boolean

This commit is contained in:
Timothy Flynn 2022-02-11 09:43:02 -05:00 committed by Linus Groh
parent 8fab99e920
commit e13c96157c
Notes: sideshowbarker 2024-07-17 18:53:26 +09:00
3 changed files with 32 additions and 2 deletions

View file

@ -169,7 +169,18 @@ TEST_CASE(float_value)
EXPECT(v.to_int().has_value());
EXPECT_EQ(v.to_int().value(), 3);
EXPECT_EQ(v.to_string(), "3.14");
EXPECT(!v.to_bool().has_value());
EXPECT(v.to_bool().has_value());
EXPECT(v.to_bool().value());
v = 0.0;
EXPECT(!v.is_null());
EXPECT(v.to_double().has_value());
EXPECT(v.to_double().value() < NumericLimits<double>().epsilon());
EXPECT(v.to_int().has_value());
EXPECT_EQ(v.to_int().value(), 0);
EXPECT_EQ(v.to_string(), "0");
EXPECT(v.to_bool().has_value());
EXPECT(!v.to_bool().value());
}
{
SQL::Value v(3.14);

View file

@ -728,6 +728,11 @@ Optional<int> FloatImpl::to_int() const
return static_cast<int>(round(value()));
}
Optional<bool> FloatImpl::to_bool() const
{
return fabs(value()) > NumericLimits<double>::epsilon();
}
Optional<double> FloatImpl::to_double() const
{
return value();
@ -999,6 +1004,19 @@ int TupleImpl::compare(Value const& other) const
return 0;
}
Optional<bool> TupleImpl::to_bool() const
{
for (auto const& value : value()) {
auto as_bool = Value(value).to_bool();
if (!as_bool.has_value())
return {};
if (!as_bool.value())
return false;
}
return true;
}
void TupleImpl::serialize(Serializer& serializer) const
{
serializer.serialize<TupleDescriptor>(*m_descriptor);

View file

@ -159,7 +159,7 @@ public:
[[nodiscard]] String to_string() const;
[[nodiscard]] Optional<int> to_int() const;
[[nodiscard]] Optional<double> to_double() const;
[[nodiscard]] static Optional<bool> to_bool() { return {}; }
[[nodiscard]] Optional<bool> to_bool() const;
[[nodiscard]] static bool to_vector(Vector<Value>&) { return false; }
void assign(Value const&);
void assign_string(String const&);
@ -250,6 +250,7 @@ public:
[[nodiscard]] size_t length() const;
[[nodiscard]] bool can_cast(Value const&) const;
[[nodiscard]] int compare(Value const& other) const;
[[nodiscard]] Optional<bool> to_bool() const;
virtual bool validate_before_assignment(Vector<Value> const&) override;
virtual bool validate(BaseTypeImpl const&) override;