Serenity Operating System
at hosted 95 lines 3.4 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#pragma once 28 29#include <AK/LogStream.h> 30#include <LibGUI/TextPosition.h> 31 32namespace GUI { 33 34class TextRange { 35public: 36 TextRange() {} 37 TextRange(const TextPosition& start, const TextPosition& end) 38 : m_start(start) 39 , m_end(end) 40 { 41 } 42 43 bool is_valid() const { return m_start.is_valid() && m_end.is_valid(); } 44 void clear() 45 { 46 m_start = {}; 47 m_end = {}; 48 } 49 50 TextPosition& start() { return m_start; } 51 TextPosition& end() { return m_end; } 52 const TextPosition& start() const { return m_start; } 53 const TextPosition& end() const { return m_end; } 54 55 TextRange normalized() const { return TextRange(normalized_start(), normalized_end()); } 56 57 void set_start(const TextPosition& position) { m_start = position; } 58 void set_end(const TextPosition& position) { m_end = position; } 59 60 void set(const TextPosition& start, const TextPosition& end) 61 { 62 m_start = start; 63 m_end = end; 64 } 65 66 bool operator==(const TextRange& other) const 67 { 68 return m_start == other.m_start && m_end == other.m_end; 69 } 70 71 bool contains(const TextPosition& position) const 72 { 73 if (!(position.line() > m_start.line() || (position.line() == m_start.line() && position.column() >= m_start.column()))) 74 return false; 75 if (!(position.line() < m_end.line() || (position.line() == m_end.line() && position.column() <= m_end.column()))) 76 return false; 77 return true; 78 } 79 80private: 81 TextPosition normalized_start() const { return m_start < m_end ? m_start : m_end; } 82 TextPosition normalized_end() const { return m_start < m_end ? m_end : m_start; } 83 84 TextPosition m_start; 85 TextPosition m_end; 86}; 87 88inline const LogStream& operator<<(const LogStream& stream, const TextRange& value) 89{ 90 if (!value.is_valid()) 91 return stream << "GUI::TextRange(Invalid)"; 92 return stream << value.start() << '-' << value.end(); 93} 94 95}