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/utility.hpp"
8#include "../unicode/unicode.hpp"
9#include "../macros.hpp"
10#include <tuple>
11#include <cstdlib>
12#include <algorithm>
13
14
15
16namespace hi::inline v1 {
17
26public:
27 constexpr text_cursor() noexcept = default;
28 constexpr text_cursor(text_cursor const&) noexcept = default;
29 constexpr text_cursor(text_cursor&&) noexcept = default;
30 constexpr text_cursor& operator=(text_cursor const&) noexcept = default;
31 constexpr text_cursor& operator=(text_cursor&&) noexcept = default;
32 [[nodiscard]] constexpr friend bool operator==(text_cursor const&, text_cursor const&) = default;
33 [[nodiscard]] constexpr friend auto operator<=>(text_cursor const&, text_cursor const&) = default;
34
48 constexpr text_cursor& resize(size_t size) & noexcept
49 {
50 inplace_min(_value, max_value(size));
51 return *this;
52 }
53
54 constexpr text_cursor resize(size_t size) && noexcept
55 {
56 inplace_min(_value, max_value(size));
57 return *this;
58 }
59
60
66 constexpr text_cursor(size_t index, bool after) noexcept
67 {
68 _value = (index << 1) | static_cast<size_t>(after);
69 }
70
77 [[nodiscard]] constexpr text_cursor neighbor(size_t size) const noexcept
78 {
79 auto r = before() ? text_cursor{index() - 1, true} : text_cursor{index() + 1, false};
80 return r.resize(size);
81 }
82
83 [[nodiscard]] constexpr text_cursor after_neighbor(size_t size) const noexcept
84 {
85 return before() ? neighbor(size) : *this;
86 }
87
88 [[nodiscard]] constexpr text_cursor before_neighbor(size_t size) const noexcept
89 {
90 return after() ? neighbor(size) : *this;
91 }
92
93 [[nodiscard]] constexpr bool start_of_text() const noexcept
94 {
95 return _value == 0;
96 }
97
98 [[nodiscard]] constexpr bool end_of_text(size_t size) const noexcept
99 {
100 return _value >= max_value(size);
101 }
102
103 [[nodiscard]] constexpr size_t index() const noexcept
104 {
105 return _value >> 1;
106 }
107
108 [[nodiscard]] constexpr bool after() const noexcept
109 {
110 return to_bool(_value & 1);
111 }
112
113 [[nodiscard]] constexpr bool before() const noexcept
114 {
115 return not after();
116 }
117
118private:
124 [[nodiscard]] constexpr static size_t max_value(size_t size) noexcept
125 {
126 if (size) {
127 size <<= 1;
128 --size;
129 }
130 return size;
131 }
132
143 size_t _value = 0;
144};
145
146} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm.hpp:16
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
A cursor-position in text.
Definition text_cursor.hpp:25
constexpr text_cursor & resize(size_t size) &noexcept
Set the text size.
Definition text_cursor.hpp:48
constexpr text_cursor(size_t index, bool after) noexcept
Create a new text cursor.
Definition text_cursor.hpp:66
constexpr text_cursor neighbor(size_t size) const noexcept
Return the neighbor cursor.
Definition text_cursor.hpp:77