Skip to content

Commit

Permalink
Use rval for AttributedString::Fragment changes (#47494)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #47494

Changelog: [General][Changed] AttributedString `appendFragment` and `prependFragment` take an rval instead of a const ref; append/prependAttributedString have been removed

Reviewed By: mdvacca

Differential Revision: D65603864

fbshipit-source-id: 1160a9e2064470f826bea66736b4fce13caa3a73
  • Loading branch information
javache authored and facebook-github-bot committed Nov 11, 2024
1 parent 3a4493f commit 2c31fe9
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,42 +53,18 @@ bool Fragment::operator!=(const Fragment& rhs) const {

#pragma mark - AttributedString

void AttributedString::appendFragment(const Fragment& fragment) {
void AttributedString::appendFragment(Fragment&& fragment) {
ensureUnsealed();

if (fragment.string.empty()) {
return;
if (!fragment.string.empty()) {
fragments_.push_back(std::move(fragment));
}

fragments_.push_back(fragment);
}

void AttributedString::prependFragment(const Fragment& fragment) {
void AttributedString::prependFragment(Fragment&& fragment) {
ensureUnsealed();

if (fragment.string.empty()) {
return;
if (!fragment.string.empty()) {
fragments_.insert(fragments_.begin(), std::move(fragment));
}

fragments_.insert(fragments_.begin(), fragment);
}

void AttributedString::appendAttributedString(
const AttributedString& attributedString) {
ensureUnsealed();
fragments_.insert(
fragments_.end(),
attributedString.fragments_.begin(),
attributedString.fragments_.end());
}

void AttributedString::prependAttributedString(
const AttributedString& attributedString) {
ensureUnsealed();
fragments_.insert(
fragments_.begin(),
attributedString.fragments_.begin(),
attributedString.fragments_.end());
}

void AttributedString::setBaseTextAttributes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,8 @@ class AttributedString : public Sealable, public DebugStringConvertible {
/*
* Appends and prepends a `fragment` to the string.
*/
void appendFragment(const Fragment& fragment);
void prependFragment(const Fragment& fragment);

/*
* Appends and prepends an `attributedString` (all its fragments) to
* the string.
*/
void appendAttributedString(const AttributedString& attributedString);
void prependAttributedString(const AttributedString& attributedString);
void appendFragment(Fragment&& fragment);
void prependFragment(Fragment&& fragment);

/*
* Sets attributes which would apply to hypothetical text not included in the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ TEST(AttributedStringBoxTest, testValueConstructor) {
auto attributedString = AttributedString{};
auto fragment = AttributedString::Fragment{};
fragment.string = "test string";
attributedString.appendFragment(fragment);
attributedString.appendFragment(std::move(fragment));
auto attributedStringBox = AttributedStringBox{attributedString};

EXPECT_EQ(attributedStringBox.getMode(), AttributedStringBox::Mode::Value);
Expand Down Expand Up @@ -58,7 +58,7 @@ TEST(AttributedStringBoxTest, testMoveConstructor) {
auto attributedString = AttributedString{};
auto fragment = AttributedString::Fragment{};
fragment.string = "test string";
attributedString.appendFragment(fragment);
attributedString.appendFragment(std::move(fragment));
auto movedFromAttributedStringBox = AttributedStringBox{attributedString};

auto moveToAttributedStringBox =
Expand Down Expand Up @@ -88,7 +88,7 @@ TEST(AttributedStringBoxTest, testMoveAssignment) {
auto attributedString = AttributedString{};
auto fragment = AttributedString::Fragment{};
fragment.string = "test string";
attributedString.appendFragment(fragment);
attributedString.appendFragment(std::move(fragment));
auto movedFromAttributedStringBox = AttributedStringBox{attributedString};

auto moveToAttributedStringBox = AttributedStringBox{};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void BaseTextShadowNode::buildAttributedString(
// don't need it at all). Storing a `ShadowView` instance instead of
// `ShadowNode` should properly fix this problem.
fragment.parentShadowView = shadowViewFromShadowNode(parentNode);
outAttributedString.appendFragment(fragment);
outAttributedString.appendFragment(std::move(fragment));
lastFragmentWasRawText = true;
}
continue;
Expand All @@ -75,7 +75,7 @@ void BaseTextShadowNode::buildAttributedString(
fragment.string = AttributedString::Fragment::AttachmentCharacter();
fragment.parentShadowView = shadowViewFromShadowNode(*childNode);
fragment.textAttributes = baseTextAttributes;
outAttributedString.appendFragment(fragment);
outAttributedString.appendFragment(std::move(fragment));
outAttachments.push_back(Attachment{
childNode.get(), outAttributedString.getFragments().size() - 1});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ AttributedString AndroidTextInputShadowNode::getAttributedString() const {
// that effect.
fragment.textAttributes.backgroundColor = clearColor();
fragment.parentShadowView = ShadowView(*this);
attributedString.prependFragment(fragment);
attributedString.prependFragment(std::move(fragment));
}

return attributedString;
Expand Down Expand Up @@ -91,7 +91,7 @@ AttributedString AndroidTextInputShadowNode::getPlaceholderAttributedString()
// appended to the AttributedString (see implementation of appendFragment)
fragment.textAttributes = textAttributes;
fragment.parentShadowView = ShadowView(*this);
textAttributedString.appendFragment(fragment);
textAttributedString.appendFragment(std::move(fragment));

return textAttributedString;
}
Expand Down

0 comments on commit 2c31fe9

Please sign in to comment.