De-duplicate the logic for counting attachments (#42596)

Summary:
De-duplicate the logic for counting attachments.

This is a minor improvement in the context of my multi-PR work on https://github.com/react-native-community/discussions-and-proposals/issues/695.

## Changelog:

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

[INTERNAL] [CHANGE] - De-duplicate the logic for counting attachments

Pull Request resolved: https://github.com/facebook/react-native/pull/42596

Reviewed By: rshest

Differential Revision: D53917281

Pulled By: cipolleschi

fbshipit-source-id: cdb9bc834bddd7deffc60f33578464733982fedf
This commit is contained in:
Jakub Trzebiatowski authored and Facebook GitHub Bot committed 2024-02-20 04:04:01 -08:00
1 parent 10edb9f545
commit 69977d05f0
1 file changed
+18 -16
@@ -22,6 +22,18 @@ using namespace facebook::jni;
namespace facebook::react {
static int countAttachments(const AttributedString& attributedString) {
int count = 0;
for (const auto& fragment : attributedString.getFragments()) {
if (fragment.isAttachment()) {
count++;
}
}
return count;
}
Size measureAndroidComponent(
const ContextContainer::Shared& contextContainer,
Tag rootTag,
@@ -274,14 +286,9 @@ TextMeasurement TextLayoutManager::doMeasure(
LayoutConstraints layoutConstraints) const {
layoutConstraints.maximumSize.height = std::numeric_limits<Float>::infinity();
int attachmentsCount = 0;
for (const auto& fragment : attributedString.getFragments()) {
if (fragment.isAttachment()) {
attachmentsCount++;
}
}
const int attachmentCount = countAttachments(attributedString);
auto env = Environment::current();
auto attachmentPositions = env->NewFloatArray(attachmentsCount * 2);
auto attachmentPositions = env->NewFloatArray(attachmentCount * 2);
auto minimumSize = layoutConstraints.minimumSize;
auto maximumSize = layoutConstraints.maximumSize;
@@ -304,7 +311,7 @@ TextMeasurement TextLayoutManager::doMeasure(
env->GetFloatArrayElements(attachmentPositions, nullptr);
auto attachments = TextMeasurement::Attachments{};
if (attachmentsCount > 0) {
if (attachmentCount > 0) {
const folly::dynamic& fragments = serializedAttributedString["fragments"];
int attachmentIndex = 0;
for (const auto& fragment : fragments) {
@@ -338,14 +345,9 @@ TextMeasurement TextLayoutManager::doMeasureMapBuffer(
LayoutConstraints layoutConstraints) const {
layoutConstraints.maximumSize.height = std::numeric_limits<Float>::infinity();
int attachmentsCount = 0;
for (const auto& fragment : attributedString.getFragments()) {
if (fragment.isAttachment()) {
attachmentsCount++;
}
}
const int attachmentCount = countAttachments(attributedString);
auto env = Environment::current();
auto attachmentPositions = env->NewFloatArray(attachmentsCount * 2);
auto attachmentPositions = env->NewFloatArray(attachmentCount * 2);
auto minimumSize = layoutConstraints.minimumSize;
auto maximumSize = layoutConstraints.maximumSize;
@@ -369,7 +371,7 @@ TextMeasurement TextLayoutManager::doMeasureMapBuffer(
env->GetFloatArrayElements(attachmentPositions, nullptr);
auto attachments = TextMeasurement::Attachments{};
if (attachmentsCount > 0) {
if (attachmentCount > 0) {
int attachmentIndex = 0;
for (const auto& fragment : attributedString.getFragments()) {
if (fragment.isAttachment()) {