Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

De-duplicate the logic for counting attachments #42596

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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()) {
Expand Down
Loading