mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Extracts a TextRange class with a base and extent, and start(), end(), collapsed(), and length() getters. The possibility of reversed base and extent in selections and composing ranges makes reasoning about them complex and increases the chances of errors in the code. This change migrates most uses of base and extent in the text model to start()/end() or position(). The position() method is intended purely as an aid to readability to indicate that a collapsed selection is expected at the call site; it also enforces a debug-time assertion that this is the case.
54 lines
1.6 KiB
C++
54 lines
1.6 KiB
C++
// Copyright 2013 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#include <algorithm>
|
|
|
|
#include "flutter/fml/logging.h"
|
|
|
|
// A directional range of text.
|
|
//
|
|
// A |TextRange| describes a range of text with |base| and |extent| positions.
|
|
// In the case where |base| == |extent|, the range is said to be collapsed, and
|
|
// when |base| > |extent|, the range is said to be reversed.
|
|
class TextRange {
|
|
public:
|
|
explicit TextRange(size_t position) : base_(position), extent_(position) {}
|
|
explicit TextRange(size_t base, size_t extent)
|
|
: base_(base), extent_(extent) {}
|
|
TextRange(const TextRange&) = default;
|
|
TextRange& operator=(const TextRange&) = default;
|
|
|
|
virtual ~TextRange() = default;
|
|
|
|
// Returns the base position of the range.
|
|
size_t base() const { return base_; }
|
|
|
|
// Returns the extent position of the range.
|
|
size_t extent() const { return extent_; }
|
|
|
|
// Returns the lesser of the base and extent positions.
|
|
size_t start() const { return std::min(base_, extent_); }
|
|
|
|
// Returns the greater of the base and extent positions.
|
|
size_t end() const { return std::max(base_, extent_); }
|
|
|
|
// Returns the position of a collapsed range.
|
|
//
|
|
// Asserts that the range is of length 0.
|
|
size_t position() const {
|
|
FML_DCHECK(base_ == extent_);
|
|
return extent_;
|
|
}
|
|
|
|
// Returns the length of the range.
|
|
size_t length() const { return end() - start(); }
|
|
|
|
// Returns true if the range is of length 0.
|
|
bool collapsed() const { return base_ == extent_; }
|
|
|
|
private:
|
|
size_t base_;
|
|
size_t extent_;
|
|
};
|