Number.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Types.h>
  8. #include <math.h>
  9. namespace Web::CSS {
  10. class Tokenizer;
  11. class Number {
  12. friend class Tokenizer;
  13. public:
  14. enum class Type {
  15. Number,
  16. IntegerWithExplicitSign, // This only exists for the nightmarish An+B parsing algorithm
  17. Integer
  18. };
  19. Number()
  20. : m_value(0)
  21. , m_type(Type::Number)
  22. {
  23. }
  24. Number(Type type, float value)
  25. : m_value(value)
  26. , m_type(type)
  27. {
  28. }
  29. float value() const { return m_value; }
  30. i64 integer_value() const
  31. {
  32. // https://www.w3.org/TR/css-values-4/#numeric-types
  33. // When a value cannot be explicitly supported due to range/precision limitations, it must be converted
  34. // to the closest value supported by the implementation, but how the implementation defines "closest"
  35. // is explicitly undefined as well.
  36. return llroundf(m_value);
  37. }
  38. bool is_integer() const { return m_type == Type::Integer || m_type == Type::IntegerWithExplicitSign; }
  39. bool is_integer_with_explicit_sign() const { return m_type == Type::IntegerWithExplicitSign; }
  40. Number operator+(Number const& other) const
  41. {
  42. if (is_integer() && other.is_integer())
  43. return { Type::Integer, m_value + other.m_value };
  44. return { Type::Number, m_value + other.m_value };
  45. }
  46. Number operator-(Number const& other) const
  47. {
  48. if (is_integer() && other.is_integer())
  49. return { Type::Integer, m_value - other.m_value };
  50. return { Type::Number, m_value - other.m_value };
  51. }
  52. Number operator*(Number const& other) const
  53. {
  54. if (is_integer() && other.is_integer())
  55. return { Type::Integer, m_value * other.m_value };
  56. return { Type::Number, m_value * other.m_value };
  57. }
  58. Number operator/(Number const& other) const
  59. {
  60. return { Type::Number, m_value / other.m_value };
  61. }
  62. auto operator<=>(Number const& other) const
  63. {
  64. return m_value - other.m_value;
  65. }
  66. private:
  67. float m_value { 0 };
  68. Type m_type;
  69. };
  70. }