Use rval for AttributedString::Fragment changes (#47494)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/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
This commit is contained in:
Pieter De Baets 2024-11-11 07:35:20 -08:00 committed by Facebook GitHub Bot
parent 3a4493f4c4
commit 2c31fe99e1
5 changed files with 15 additions and 46 deletions

View File

@ -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(

View File

@ -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

View File

@ -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);
@ -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 =
@ -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{};

View File

@ -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;
@ -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});
}

View File

@ -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;
@ -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;
}