Flutter Engine
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 #include <algorithm>
6 
7 #include "flutter/fml/logging.h"
8 
9 // A directional range of text.
10 //
11 // A |TextRange| describes a range of text with |base| and |extent| positions.
12 // In the case where |base| == |extent|, the range is said to be collapsed, and
13 // when |base| > |extent|, the range is said to be reversed.
14 class TextRange {
15  public:
16  explicit TextRange(size_t position) : base_(position), extent_(position) {}
17  explicit TextRange(size_t base, size_t extent)
18  : base_(base), extent_(extent) {}
19  TextRange(const TextRange&) = default;
20  TextRange& operator=(const TextRange&) = default;
21 
22  virtual ~TextRange() = default;
23 
24  // The base position of the range.
25  size_t base() const { return base_; }
26 
27  // Sets the base position of the range.
28  void set_base(size_t pos) { base_ = pos; }
29 
30  // The extent position of the range.
31  size_t extent() const { return extent_; }
32 
33  // Sets the extent position of the range.
34  void set_extent(size_t pos) { extent_ = pos; }
35 
36  // The lesser of the base and extent positions.
37  size_t start() const { return std::min(base_, extent_); }
38 
39  // Sets the start position of the range.
40  void set_start(size_t pos) {
41  if (base_ <= extent_) {
42  base_ = pos;
43  } else {
44  extent_ = pos;
45  }
46  }
47 
48  // The greater of the base and extent positions.
49  size_t end() const { return std::max(base_, extent_); }
50 
51  // Sets the end position of the range.
52  void set_end(size_t pos) {
53  if (base_ <= extent_) {
54  extent_ = pos;
55  } else {
56  base_ = pos;
57  }
58  }
59 
60  // The position of a collapsed range.
61  //
62  // Asserts that the range is of length 0.
63  size_t position() const {
64  FML_DCHECK(base_ == extent_);
65  return extent_;
66  }
67 
68  // The length of the range.
69  size_t length() const { return end() - start(); }
70 
71  // Returns true if the range is of length 0.
72  bool collapsed() const { return base_ == extent_; }
73 
74  // Returns true if the base is greater than the extent.
75  bool reversed() const { return base_ > extent_; }
76 
77  // Returns true if |position| is contained within the range.
78  bool Contains(size_t position) const {
79  return position >= start() && position <= end();
80  }
81 
82  // Returns true if |range| is contained within the range.
83  bool Contains(const TextRange& range) const {
84  return range.start() >= start() && range.end() <= end();
85  }
86 
87  bool operator==(const TextRange& other) const {
88  return base_ == other.base_ && extent_ == other.extent_;
89  }
90 
91  private:
92  size_t base_;
93  size_t extent_;
94 };
bool Contains(const TextRange &range) const
Definition: text_range.h:83
size_t base() const
Definition: text_range.h:25
void set_start(size_t pos)
Definition: text_range.h:40
#define FML_DCHECK(condition)
Definition: logging.h:86
void set_extent(size_t pos)
Definition: text_range.h:34
TextRange(size_t position)
Definition: text_range.h:16
bool reversed() const
Definition: text_range.h:75
void set_base(size_t pos)
Definition: text_range.h:28
TextRange(size_t base, size_t extent)
Definition: text_range.h:17
TextRange & operator=(const TextRange &)=default
virtual ~TextRange()=default
size_t end() const
Definition: text_range.h:49
size_t extent() const
Definition: text_range.h:31
size_t length() const
Definition: text_range.h:69
size_t position() const
Definition: text_range.h:63
bool operator==(const TextRange &other) const
Definition: text_range.h:87
bool Contains(size_t position) const
Definition: text_range.h:78
void set_end(size_t pos)
Definition: text_range.h:52
size_t start() const
Definition: text_range.h:37
bool collapsed() const
Definition: text_range.h:72