Avoid an unnecessary loop in CPDF_FormField::IsItemSelected().
There is no need to go through all the elements in an array when the
caller knows it only wants the one at a particular position.
Change-Id: I64dc1b8de9fbbdb9b8fb79a3c4c019a9a180dec6
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/52550
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfdoc/cpdf_formfield.cpp b/core/fpdfdoc/cpdf_formfield.cpp
index d5ac71b..283c9b3 100644
--- a/core/fpdfdoc/cpdf_formfield.cpp
+++ b/core/fpdfdoc/cpdf_formfield.cpp
@@ -557,18 +557,12 @@
if (!pArray)
return false;
- int iPos = -1;
- for (int j = 0; j < CountSelectedOptions(); j++) {
- if (GetSelectedOptionIndex(j) == index) {
- iPos = j;
- break;
+ for (int i = 0; i < CountSelectedOptions(); ++i) {
+ if (GetSelectedOptionIndex(i) == index) {
+ const CPDF_Object* pDirectObj = pArray->GetDirectObjectAt(i);
+ return pDirectObj && pDirectObj->GetUnicodeText() == opt_value;
}
}
- for (int i = 0; i < static_cast<int>(pArray->size()); i++) {
- const CPDF_Object* pDirectObj = pArray->GetDirectObjectAt(i);
- if (pDirectObj && pDirectObj->GetUnicodeText() == opt_value && i == iPos)
- return true;
- }
return false;
}