Number.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/String.h>
  8. #include <AK/Types.h>
  9. #include <math.h>
  10. namespace Web::CSS {
  11. class Number {
  12. public:
  13. enum class Type {
  14. Number,
  15. IntegerWithExplicitSign, // This only exists for the nightmarish An+B parsing algorithm
  16. Integer
  17. };
  18. Number()
  19. : m_value(0)
  20. , m_type(Type::Number)
  21. {
  22. }
  23. Number(Type type, double value)
  24. : m_value(value)
  25. , m_type(type)
  26. {
  27. }
  28. Type type() const { return m_type; }
  29. double 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 llround(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. String to_string() const
  63. {
  64. if (m_type == Type::IntegerWithExplicitSign)
  65. return MUST(String::formatted("{:+}", m_value));
  66. return MUST(String::number(m_value));
  67. }
  68. bool operator==(Number const& other) const
  69. {
  70. return m_type == other.m_type && m_value == other.m_value;
  71. }
  72. int operator<=>(Number const& other) const
  73. {
  74. if (m_value < other.m_value)
  75. return -1;
  76. if (m_value > other.m_value)
  77. return 1;
  78. return 0;
  79. }
  80. private:
  81. double m_value { 0 };
  82. Type m_type;
  83. };
  84. }
  85. template<>
  86. struct AK::Formatter<Web::CSS::Number> : Formatter<StringView> {
  87. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Number const& number)
  88. {
  89. return Formatter<StringView>::format(builder, number.to_string());
  90. }
  91. };