This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Extract textrange #21722
Merged
Merged
Extract textrange #21722
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// 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_; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// 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 "flutter/shell/platform/common/cpp/text_range.h" | ||
|
||
#include "gtest/gtest.h" | ||
|
||
namespace flutter { | ||
|
||
TEST(TextRange, TextRangeFromPositionZero) { | ||
TextRange range(0); | ||
EXPECT_EQ(range.base(), size_t(0)); | ||
EXPECT_EQ(range.extent(), size_t(0)); | ||
EXPECT_EQ(range.start(), size_t(0)); | ||
EXPECT_EQ(range.end(), size_t(0)); | ||
EXPECT_EQ(range.length(), size_t(0)); | ||
EXPECT_EQ(range.position(), size_t(0)); | ||
EXPECT_TRUE(range.collapsed()); | ||
} | ||
|
||
TEST(TextRange, TextRangeFromPositionNonZero) { | ||
TextRange range(3); | ||
EXPECT_EQ(range.base(), size_t(3)); | ||
EXPECT_EQ(range.extent(), size_t(3)); | ||
EXPECT_EQ(range.start(), size_t(3)); | ||
EXPECT_EQ(range.end(), size_t(3)); | ||
EXPECT_EQ(range.length(), size_t(0)); | ||
EXPECT_EQ(range.position(), size_t(3)); | ||
EXPECT_TRUE(range.collapsed()); | ||
} | ||
|
||
TEST(TextRange, TextRangeFromRange) { | ||
TextRange range(3, 7); | ||
EXPECT_EQ(range.base(), size_t(3)); | ||
EXPECT_EQ(range.extent(), size_t(7)); | ||
EXPECT_EQ(range.start(), size_t(3)); | ||
EXPECT_EQ(range.end(), size_t(7)); | ||
EXPECT_EQ(range.length(), size_t(4)); | ||
EXPECT_FALSE(range.collapsed()); | ||
} | ||
|
||
TEST(TextRange, TextRangeFromReversedRange) { | ||
TextRange range(7, 3); | ||
EXPECT_EQ(range.base(), size_t(7)); | ||
EXPECT_EQ(range.extent(), size_t(3)); | ||
EXPECT_EQ(range.start(), size_t(3)); | ||
EXPECT_EQ(range.end(), size_t(7)); | ||
EXPECT_EQ(range.length(), size_t(4)); | ||
EXPECT_FALSE(range.collapsed()); | ||
} | ||
|
||
} // namespace flutter |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.