HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
text_cursor.hpp
1// Copyright Take Vos 2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "../utility/module.hpp"
8#include "../unicode/module.hpp"
9#include <tuple>
10#include <cstdlib>
11#include <algorithm>
12
13namespace hi::inline v1 {
14
23public:
24 constexpr text_cursor() noexcept = default;
25 constexpr text_cursor(text_cursor const&) noexcept = default;
26 constexpr text_cursor(text_cursor&&) noexcept = default;
27 constexpr text_cursor& operator=(text_cursor const&) noexcept = default;
28 constexpr text_cursor& operator=(text_cursor&&) noexcept = default;
29 [[nodiscard]] constexpr friend bool operator==(text_cursor const&, text_cursor const&) = default;
30 [[nodiscard]] constexpr friend auto operator<=>(text_cursor const&, text_cursor const&) = default;
31
45 constexpr text_cursor& resize(size_t size) & noexcept
46 {
47 inplace_min(_value, max_value(size));
48 return *this;
49 }
50
51 constexpr text_cursor resize(size_t size) && noexcept
52 {
53 inplace_min(_value, max_value(size));
54 return *this;
55 }
56
57
63 constexpr text_cursor(size_t index, bool after) noexcept
64 {
65 _value = (index << 1) | static_cast<size_t>(after);
66 }
67
74 [[nodiscard]] constexpr text_cursor neighbor(size_t size) const noexcept
75 {
76 auto r = before() ? text_cursor{index() - 1, true} : text_cursor{index() + 1, false};
77 return r.resize(size);
78 }
79
80 [[nodiscard]] constexpr text_cursor after_neighbor(size_t size) const noexcept
81 {
82 return before() ? neighbor(size) : *this;
83 }
84
85 [[nodiscard]] constexpr text_cursor before_neighbor(size_t size) const noexcept
86 {
87 return after() ? neighbor(size) : *this;
88 }
89
90 [[nodiscard]] constexpr bool start_of_text() const noexcept
91 {
92 return _value == 0;
93 }
94
95 [[nodiscard]] constexpr bool end_of_text(size_t size) const noexcept
96 {
97 return _value >= max_value(size);
98 }
99
100 [[nodiscard]] constexpr size_t index() const noexcept
101 {
102 return _value >> 1;
103 }
104
105 [[nodiscard]] constexpr bool after() const noexcept
106 {
107 return to_bool(_value & 1);
108 }
109
110 [[nodiscard]] constexpr bool before() const noexcept
111 {
112 return not after();
113 }
114
115private:
121 [[nodiscard]] constexpr static size_t max_value(size_t size) noexcept
122 {
123 if (size) {
124 size <<= 1;
125 --size;
126 }
127 return size;
128 }
129
140 size_t _value = 0;
141};
142
143} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm.hpp:13
A cursor-position in text.
Definition text_cursor.hpp:22
constexpr text_cursor & resize(size_t size) &noexcept
Set the text size.
Definition text_cursor.hpp:45
constexpr text_cursor(size_t index, bool after) noexcept
Create a new text cursor.
Definition text_cursor.hpp:63
constexpr text_cursor neighbor(size_t size) const noexcept
Return the neighbor cursor.
Definition text_cursor.hpp:74