Number.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/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, float value)
  24. : m_value(value)
  25. , m_type(type)
  26. {
  27. }
  28. float value() const { return m_value; }
  29. i64 integer_value() const
  30. {
  31. // https://www.w3.org/TR/css-values-4/#numeric-types
  32. // When a value cannot be explicitly supported due to range/precision limitations, it must be converted
  33. // to the closest value supported by the implementation, but how the implementation defines "closest"
  34. // is explicitly undefined as well.
  35. return llroundf(m_value);
  36. }
  37. bool is_integer() const { return m_type == Type::Integer || m_type == Type::IntegerWithExplicitSign; }
  38. bool is_integer_with_explicit_sign() const { return m_type == Type::IntegerWithExplicitSign; }
  39. Number operator+(Number const& other) const
  40. {
  41. if (is_integer() && other.is_integer())
  42. return { Type::Integer, m_value + other.m_value };
  43. return { Type::Number, m_value + other.m_value };
  44. }
  45. Number operator-(Number const& other) const
  46. {
  47. if (is_integer() && other.is_integer())
  48. return { Type::Integer, m_value - other.m_value };
  49. return { Type::Number, m_value - other.m_value };
  50. }
  51. Number operator*(Number const& other) const
  52. {
  53. if (is_integer() && other.is_integer())
  54. return { Type::Integer, m_value * other.m_value };
  55. return { Type::Number, m_value * other.m_value };
  56. }
  57. Number operator/(Number const& other) const
  58. {
  59. return { Type::Number, m_value / other.m_value };
  60. }
  61. ErrorOr<String> to_string() const
  62. {
  63. if (m_type == Type::IntegerWithExplicitSign)
  64. return String::formatted("{:+}", m_value);
  65. return String::number(m_value);
  66. }
  67. bool operator==(Number const& other) const
  68. {
  69. return m_type == other.m_type && m_value == other.m_value;
  70. }
  71. private:
  72. float m_value { 0 };
  73. Type m_type;
  74. };
  75. }
  76. template<>
  77. struct AK::Formatter<Web::CSS::Number> : Formatter<StringView> {
  78. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Number const& number)
  79. {
  80. return Formatter<StringView>::format(builder, TRY(number.to_string()));
  81. }
  82. };