| 1 | // Copyright (C) 2021 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
| 3 | |
| 4 | #ifndef TEXT_H |
| 5 | #define TEXT_H |
| 6 | |
| 7 | #include "atom.h" |
| 8 | |
| 9 | QT_BEGIN_NAMESPACE |
| 10 | |
| 11 | class Text |
| 12 | { |
| 13 | public: |
| 14 | Text(); |
| 15 | explicit Text(const QString &str); |
| 16 | Text(const Text &text); |
| 17 | ~Text(); |
| 18 | |
| 19 | Text &operator=(const Text &text); |
| 20 | |
| 21 | Atom *firstAtom() { return m_first; } |
| 22 | Atom *lastAtom() { return m_last; } |
| 23 | Text &operator<<(Atom::AtomType atomType); |
| 24 | Text &operator<<(const QString &string); |
| 25 | Text &operator<<(const Atom &atom); |
| 26 | Text &operator<<(const LinkAtom &atom); |
| 27 | Text &operator<<(const Text &text); |
| 28 | void stripFirstAtom(); |
| 29 | void stripLastAtom(); |
| 30 | |
| 31 | [[nodiscard]] bool isEmpty() const { return m_first == nullptr; } |
| 32 | [[nodiscard]] bool contains(const QString &str) const; |
| 33 | [[nodiscard]] QString toString() const; |
| 34 | [[nodiscard]] const Atom *firstAtom() const { return m_first; } |
| 35 | [[nodiscard]] const Atom *lastAtom() const { return m_last; } |
| 36 | Text subText(Atom::AtomType left, Atom::AtomType right, const Atom *from = nullptr, |
| 37 | bool inclusive = false) const; |
| 38 | void dump() const; |
| 39 | void clear(); |
| 40 | |
| 41 | static Text subText(const Atom *begin, const Atom *end = nullptr); |
| 42 | static Text sectionHeading(const Atom *sectionBegin); |
| 43 | static int compare(const Text &text1, const Text &text2); |
| 44 | |
| 45 | [[nodiscard]] Text splitAtFirst(Atom::AtomType start); |
| 46 | |
| 47 | private: |
| 48 | Text(Atom *start, Atom *end) : m_first(start), m_last(end) |
| 49 | { |
| 50 | Q_ASSERT(m_first != nullptr); |
| 51 | Q_ASSERT(m_last != nullptr); |
| 52 | |
| 53 | m_last->setNext(nullptr); |
| 54 | } |
| 55 | |
| 56 | Atom *m_first { nullptr }; |
| 57 | Atom *m_last { nullptr }; |
| 58 | }; |
| 59 | |
| 60 | inline bool operator==(const Text &text1, const Text &text2) |
| 61 | { |
| 62 | return Text::compare(text1, text2) == 0; |
| 63 | } |
| 64 | inline bool operator!=(const Text &text1, const Text &text2) |
| 65 | { |
| 66 | return Text::compare(text1, text2) != 0; |
| 67 | } |
| 68 | inline bool operator<(const Text &text1, const Text &text2) |
| 69 | { |
| 70 | return Text::compare(text1, text2) < 0; |
| 71 | } |
| 72 | inline bool operator<=(const Text &text1, const Text &text2) |
| 73 | { |
| 74 | return Text::compare(text1, text2) <= 0; |
| 75 | } |
| 76 | inline bool operator>(const Text &text1, const Text &text2) |
| 77 | { |
| 78 | return Text::compare(text1, text2) > 0; |
| 79 | } |
| 80 | inline bool operator>=(const Text &text1, const Text &text2) |
| 81 | { |
| 82 | return Text::compare(text1, text2) >= 0; |
| 83 | } |
| 84 | |
| 85 | QT_END_NAMESPACE |
| 86 | |
| 87 | #endif |
| 88 | |