From 16479a39d51671b8696b7d98a00ae6f19d60030f Mon Sep 17 00:00:00 2001 From: Jakub Trzebiatowski Date: Mon, 29 Jan 2024 11:39:50 +0100 Subject: [PATCH] De-duplicate the logic for counting attachments --- .../textlayoutmanager/TextLayoutManager.cpp | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.cpp b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.cpp index 1eebf174b2bea5..e2feea5da641bf 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.cpp +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.cpp @@ -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::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::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()) {