Fabric: Small optimization in RCTNSAttributedStringFromAttributedString

Summary:
With new logic we save one attributed string copy operation.

Changelog: [Internal] Fabric-specific internal change.

Reviewed By: sammy-SC

Differential Revision: D19844371

fbshipit-source-id: 62f6b0f9c8514a1b55224ccaa52c8fa89c06c9e7
This commit is contained in:
Valentin Shergin
2020-02-13 11:37:55 -08:00
committed by Facebook Github Bot
parent 6dfcc09986
commit e737535f18
@@ -228,7 +228,7 @@ NSAttributedString *RCTNSAttributedStringFromAttributedString(const AttributedSt
[nsAttributedString beginEditing];
for (auto fragment : attributedString.getFragments()) {
NSAttributedString *nsAttributedStringFragment;
NSMutableAttributedString *nsAttributedStringFragment;
if (fragment.isAttachment()) {
auto layoutMetrics = fragment.parentShadowView.layoutMetrics;
@@ -239,18 +239,15 @@ NSAttributedString *RCTNSAttributedStringFromAttributedString(const AttributedSt
attachment.image = placeholderImage;
attachment.bounds = bounds;
nsAttributedStringFragment = [NSAttributedString attributedStringWithAttachment:attachment];
nsAttributedStringFragment = [[NSMutableAttributedString attributedStringWithAttachment:attachment] mutableCopy];
} else {
NSString *string = [NSString stringWithCString:fragment.string.c_str() encoding:NSUTF8StringEncoding];
nsAttributedStringFragment =
[[NSAttributedString alloc] initWithString:string
attributes:RCTNSTextAttributesFromTextAttributes(fragment.textAttributes)];
nsAttributedStringFragment = [[NSMutableAttributedString alloc]
initWithString:string
attributes:RCTNSTextAttributesFromTextAttributes(fragment.textAttributes)];
}
NSMutableAttributedString *nsMutableAttributedStringFragment =
[[NSMutableAttributedString alloc] initWithAttributedString:nsAttributedStringFragment];
if (fragment.parentShadowView.componentHandle) {
RCTWeakEventEmitterWrapper *eventEmitterWrapper = [RCTWeakEventEmitterWrapper new];
eventEmitterWrapper.eventEmitter = fragment.parentShadowView.eventEmitter;
@@ -258,11 +255,11 @@ NSAttributedString *RCTNSAttributedStringFromAttributedString(const AttributedSt
NSDictionary<NSAttributedStringKey, id> *additionalTextAttributes =
@{RCTAttributedStringEventEmitterKey : eventEmitterWrapper};
[nsMutableAttributedStringFragment addAttributes:additionalTextAttributes
range:NSMakeRange(0, nsMutableAttributedStringFragment.length)];
[nsAttributedStringFragment addAttributes:additionalTextAttributes
range:NSMakeRange(0, nsAttributedStringFragment.length)];
}
[nsAttributedString appendAttributedString:nsMutableAttributedStringFragment];
[nsAttributedString appendAttributedString:nsAttributedStringFragment];
}
[nsAttributedString endEditing];