aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorNeil <nyamatongwe@gmail.com>2025-01-31 12:36:49 +1100
committerNeil <nyamatongwe@gmail.com>2025-01-31 12:36:49 +1100
commit6017737861df5f3799ec2538e5fb62fe378a309e (patch)
tree5ac8c50182793e2b2a79e9feeecf28bbed0856b6 /src
parente2d7f790b130234829cde227a1439812fb510e50 (diff)
downloadscintilla-mirror-6017737861df5f3799ec2538e5fb62fe378a309e.tar.gz
Add default basic constructor and operator!= to SelectionPosition to ease use.
Use default member initializer, constexpr for constructors and equality operators and <, [[nodiscard]] for comparison operators. Add simple unit tests for Selection. Not trying to be exhaustive here, just start the process of adding tests.
Diffstat (limited to 'src')
-rw-r--r--src/Selection.cxx7
-rw-r--r--src/Selection.h44
2 files changed, 25 insertions, 26 deletions
diff --git a/src/Selection.cxx b/src/Selection.cxx
index 5b99b7c3a..ad63afd70 100644
--- a/src/Selection.cxx
+++ b/src/Selection.cxx
@@ -52,13 +52,6 @@ void SelectionPosition::MoveForInsertDelete(bool insertion, Sci::Position startC
}
}
-bool SelectionPosition::operator <(const SelectionPosition &other) const noexcept {
- if (position == other.position)
- return virtualSpace < other.virtualSpace;
- else
- return position < other.position;
-}
-
bool SelectionPosition::operator >(const SelectionPosition &other) const noexcept {
if (position == other.position)
return virtualSpace > other.virtualSpace;
diff --git a/src/Selection.h b/src/Selection.h
index c0d785870..7f3fd937b 100644
--- a/src/Selection.h
+++ b/src/Selection.h
@@ -11,10 +11,11 @@
namespace Scintilla::Internal {
class SelectionPosition {
- Sci::Position position;
- Sci::Position virtualSpace;
+ Sci::Position position = Sci::invalidPosition;
+ Sci::Position virtualSpace = 0;
public:
- explicit SelectionPosition(Sci::Position position_= Sci::invalidPosition, Sci::Position virtualSpace_=0) noexcept : position(position_), virtualSpace(virtualSpace_) {
+ constexpr SelectionPosition() noexcept = default;
+ constexpr explicit SelectionPosition(Sci::Position position_, Sci::Position virtualSpace_=0) noexcept : position(position_), virtualSpace(virtualSpace_) {
PLATFORM_ASSERT(virtualSpace < 800000000);
if (virtualSpace < 0)
virtualSpace = 0;
@@ -24,13 +25,20 @@ public:
virtualSpace = 0;
}
void MoveForInsertDelete(bool insertion, Sci::Position startChange, Sci::Position length, bool moveForEqual) noexcept;
- bool operator ==(const SelectionPosition &other) const noexcept {
- return position == other.position && virtualSpace == other.virtualSpace;
+ [[nodiscard]] constexpr bool operator ==(const SelectionPosition &other) const noexcept {
+ return (position == other.position) && (virtualSpace == other.virtualSpace);
}
- bool operator <(const SelectionPosition &other) const noexcept;
- bool operator >(const SelectionPosition &other) const noexcept;
- bool operator <=(const SelectionPosition &other) const noexcept;
- bool operator >=(const SelectionPosition &other) const noexcept;
+ [[nodiscard]] constexpr bool operator !=(const SelectionPosition &other) const noexcept {
+ return (position != other.position) || (virtualSpace != other.virtualSpace);
+ }
+ [[nodiscard]] constexpr bool operator <(const SelectionPosition &other) const noexcept {
+ if (position == other.position)
+ return virtualSpace < other.virtualSpace;
+ return position < other.position;
+ }
+ [[nodiscard]] bool operator >(const SelectionPosition &other) const noexcept;
+ [[nodiscard]] bool operator <=(const SelectionPosition &other) const noexcept;
+ [[nodiscard]] bool operator >=(const SelectionPosition &other) const noexcept;
Sci::Position Position() const noexcept {
return position;
}
@@ -61,9 +69,8 @@ public:
struct SelectionSegment {
SelectionPosition start;
SelectionPosition end;
- SelectionSegment() noexcept : start(), end() {
- }
- SelectionSegment(SelectionPosition a, SelectionPosition b) noexcept {
+ constexpr SelectionSegment() noexcept = default;
+ constexpr SelectionSegment(SelectionPosition a, SelectionPosition b) noexcept {
if (a < b) {
start = a;
end = b;
@@ -96,15 +103,14 @@ struct SelectionRange {
SelectionPosition caret;
SelectionPosition anchor;
- SelectionRange() noexcept : caret(), anchor() {
- }
- explicit SelectionRange(SelectionPosition single) noexcept : caret(single), anchor(single) {
+ constexpr SelectionRange() noexcept = default;
+ constexpr explicit SelectionRange(SelectionPosition single) noexcept : caret(single), anchor(single) {
}
- explicit SelectionRange(Sci::Position single) noexcept : caret(single), anchor(single) {
+ constexpr explicit SelectionRange(Sci::Position single) noexcept : caret(single), anchor(single) {
}
- SelectionRange(SelectionPosition caret_, SelectionPosition anchor_) noexcept : caret(caret_), anchor(anchor_) {
+ constexpr SelectionRange(SelectionPosition caret_, SelectionPosition anchor_) noexcept : caret(caret_), anchor(anchor_) {
}
- SelectionRange(Sci::Position caret_, Sci::Position anchor_) noexcept : caret(caret_), anchor(anchor_) {
+ constexpr SelectionRange(Sci::Position caret_, Sci::Position anchor_) noexcept : caret(caret_), anchor(anchor_) {
}
bool Empty() const noexcept {
return anchor == caret;
@@ -158,7 +164,7 @@ public:
enum class SelTypes { none, stream, rectangle, lines, thin };
SelTypes selType;
- Selection();
+ Selection(); // Allocates so may throw.
bool IsRectangular() const noexcept;
Sci::Position MainCaret() const noexcept;
Sci::Position MainAnchor() const noexcept;