Range.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2020-2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibVT/Position.h>
  8. namespace VT {
  9. class Range {
  10. public:
  11. Range() = default;
  12. Range(const VT::Position& start, const VT::Position& end)
  13. : m_start(start)
  14. , m_end(end)
  15. {
  16. }
  17. bool is_valid() const { return m_start.is_valid() && m_end.is_valid(); }
  18. void clear()
  19. {
  20. m_start = {};
  21. m_end = {};
  22. }
  23. VT::Position& start() { return m_start; }
  24. VT::Position& end() { return m_end; }
  25. const VT::Position& start() const { return m_start; }
  26. const VT::Position& end() const { return m_end; }
  27. Range normalized() const { return Range(normalized_start(), normalized_end()); }
  28. void set_start(const VT::Position& position) { m_start = position; }
  29. void set_end(const VT::Position& position) { m_end = position; }
  30. void set(const VT::Position& start, const VT::Position& end)
  31. {
  32. m_start = start;
  33. m_end = end;
  34. }
  35. void offset_row(int delta)
  36. {
  37. m_start = Position(m_start.row() + delta, m_start.column());
  38. m_end = Position(m_end.row() + delta, m_end.column());
  39. }
  40. bool operator==(Range const& other) const
  41. {
  42. return m_start == other.m_start && m_end == other.m_end;
  43. }
  44. bool contains(const VT::Position& position) const
  45. {
  46. if (!(position.row() > m_start.row() || (position.row() == m_start.row() && position.column() >= m_start.column())))
  47. return false;
  48. if (!(position.row() < m_end.row() || (position.row() == m_end.row() && position.column() <= m_end.column())))
  49. return false;
  50. return true;
  51. }
  52. private:
  53. VT::Position normalized_start() const { return m_start < m_end ? m_start : m_end; }
  54. VT::Position normalized_end() const { return m_start < m_end ? m_end : m_start; }
  55. VT::Position m_start;
  56. VT::Position m_end;
  57. };
  58. };