TextPosition.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/String.h>
  8. namespace GUI {
  9. class TextPosition {
  10. public:
  11. TextPosition() = default;
  12. TextPosition(size_t line, size_t column)
  13. : m_line(line)
  14. , m_column(column)
  15. {
  16. }
  17. bool is_valid() const { return m_line != 0xffffffffu && m_column != 0xffffffffu; }
  18. size_t line() const { return m_line; }
  19. size_t column() const { return m_column; }
  20. void set_line(size_t line) { m_line = line; }
  21. void set_column(size_t column) { m_column = column; }
  22. bool operator==(TextPosition const& other) const { return m_line == other.m_line && m_column == other.m_column; }
  23. bool operator!=(TextPosition const& other) const { return m_line != other.m_line || m_column != other.m_column; }
  24. bool operator<(TextPosition const& other) const { return m_line < other.m_line || (m_line == other.m_line && m_column < other.m_column); }
  25. bool operator>(TextPosition const& other) const { return *this != other && !(*this < other); }
  26. bool operator>=(TextPosition const& other) const { return *this > other || (*this == other); }
  27. private:
  28. size_t m_line { 0xffffffff };
  29. size_t m_column { 0xffffffff };
  30. };
  31. }
  32. template<>
  33. struct AK::Formatter<GUI::TextPosition> : AK::Formatter<FormatString> {
  34. ErrorOr<void> format(FormatBuilder& builder, GUI::TextPosition const& value)
  35. {
  36. if (value.is_valid())
  37. return Formatter<FormatString>::format(builder, "({},{})", value.line(), value.column());
  38. return Formatter<FormatString>::format(builder, "GUI::TextPosition(Invalid)");
  39. }
  40. };