| Flutter Linux Embedder |

text_range.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef FLUTTER_SHELL_PLATFORM_COMMON_TEXT_RANGE_H_
6 #define FLUTTER_SHELL_PLATFORM_COMMON_TEXT_RANGE_H_
7
8 #include <algorithm>
9
10 #include "flutter/fml/logging.h"
11
12 namespace flutter {
13
14 // A directional range of text.
15 //
16 // A |TextRange| describes a range of text with |base| and |extent| positions.
17 // In the case where |base| == |extent|, the range is said to be collapsed, and
18 // when |base| > |extent|, the range is said to be reversed.
19 class TextRange {
20 public:
21 explicit TextRange(size_t position) : base_(position), extent_(position) {}
22 explicit TextRange(size_t base, size_t extent)
23 : base_(base), extent_(extent) {}
24 TextRange(const TextRange&) = default;
25 TextRange& operator=(const TextRange&) = default;
26
27 virtual ~TextRange() = default;
28
29 // The base position of the range.
30 size_t base() const { return base_; }
31
32 // Sets the base position of the range.
33 void set_base(size_t pos) { base_ = pos; }
34
35 // The extent position of the range.
36 size_t extent() const { return extent_; }
37
38 // Sets the extent position of the range.
39 void set_extent(size_t pos) { extent_ = pos; }
40
41 // The lesser of the base and extent positions.
42 size_t start() const { return std::min(base_, extent_); }
43
44 // Sets the start position of the range.
45 void set_start(size_t pos) {
46 if (base_ <= extent_) {
47 base_ = pos;
48 } else {
49 extent_ = pos;
50 }
51 }
52
53 // The greater of the base and extent positions.
54 size_t end() const { return std::max(base_, extent_); }
55
56 // Sets the end position of the range.
57 void set_end(size_t pos) {
58 if (base_ <= extent_) {
59 extent_ = pos;
60 } else {
61 base_ = pos;
62 }
63 }
64
65 // The position of a collapsed range.
66 //
67 // Asserts that the range is of length 0.
68 size_t position() const {
69 FML_DCHECK(base_ == extent_);
70 return extent_;
71 }
72
73 // The length of the range.
74 size_t length() const { return end() - start(); }
75
76 // Returns true if the range is of length 0.
77 bool collapsed() const { return base_ == extent_; }
78
79 // Returns true if the base is greater than the extent.
80 bool reversed() const { return base_ > extent_; }
81
82 // Returns true if |position| is contained within the range.
83 bool Contains(size_t position) const {
84 return position >= start() && position <= end();
85 }
86
87 // Returns true if |range| is contained within the range.
88 bool Contains(const TextRange& range) const {
89 return range.start() >= start() && range.end() <= end();
90 }
91
92 bool operator==(const TextRange& other) const {
93 return base_ == other.base_ && extent_ == other.extent_;
94 }
95
96 private:
97 size_t base_;
98 size_t extent_;
99 };
100
101 } // namespace flutter
102
103 #endif // FLUTTER_SHELL_PLATFORM_COMMON_TEXT_RANGE_H_
flutter::TextRange::end
size_t end() const
Definition: text_range.h:54
flutter::TextRange::reversed
bool reversed() const
Definition: text_range.h:80
flutter::TextRange::position
size_t position() const
Definition: text_range.h:68
flutter::TextRange::set_base
void set_base(size_t pos)
Definition: text_range.h:33
flutter::TextRange::TextRange
TextRange(size_t position)
Definition: text_range.h:21
flutter::TextRange
Definition: text_range.h:19
flutter::TextRange::base
size_t base() const
Definition: text_range.h:30
flutter::TextRange::set_start
void set_start(size_t pos)
Definition: text_range.h:45
flutter::TextRange::set_extent
void set_extent(size_t pos)
Definition: text_range.h:39
flutter::TextRange::TextRange
TextRange(size_t base, size_t extent)
Definition: text_range.h:22
flutter
Definition: accessibility_bridge.cc:14
flutter::TextRange::collapsed
bool collapsed() const
Definition: text_range.h:77
flutter::TextRange::Contains
bool Contains(size_t position) const
Definition: text_range.h:83
flutter::TextRange::operator==
bool operator==(const TextRange &other) const
Definition: text_range.h:92
flutter::TextRange::extent
size_t extent() const
Definition: text_range.h:36
flutter::TextRange::start
size_t start() const
Definition: text_range.h:42
flutter::TextRange::length
size_t length() const
Definition: text_range.h:74
flutter::TextRange::~TextRange
virtual ~TextRange()=default
flutter::TextRange::set_end
void set_end(size_t pos)
Definition: text_range.h:57
flutter::TextRange::Contains
bool Contains(const TextRange &range) const
Definition: text_range.h:88
flutter::TextRange::operator=
TextRange & operator=(const TextRange &)=default