Return arrays where appropriate in fxfa.

Also, remove some default method arguments along the way.
Change-Id: Ifbd157499881ed6a3777f3903dd7f0193753cf59
Reviewed-on: https://pdfium-review.googlesource.com/3219
Reviewed-by: dsinclair <dsinclair@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/fpdfsdk/cpdfsdk_widget.cpp b/fpdfsdk/cpdfsdk_widget.cpp
index d3000c6..2ffd170 100644
--- a/fpdfsdk/cpdfsdk_widget.cpp
+++ b/fpdfsdk/cpdfsdk_widget.cpp
@@ -301,7 +301,7 @@
 
       for (int i = 0, sz = pFormField->CountSelectedItems(); i < sz; i++) {
         int nIndex = pFormField->GetSelectedIndex(i);
-        if (nIndex > -1 && nIndex < pWidgetAcc->CountChoiceListItems())
+        if (nIndex > -1 && nIndex < pWidgetAcc->CountChoiceListItems(false))
           pWidgetAcc->SetItemState(nIndex, true, false, false, true);
       }
       break;
@@ -311,7 +311,7 @@
 
       for (int i = 0, sz = pFormField->CountSelectedItems(); i < sz; i++) {
         int nIndex = pFormField->GetSelectedIndex(i);
-        if (nIndex > -1 && nIndex < pWidgetAcc->CountChoiceListItems())
+        if (nIndex > -1 && nIndex < pWidgetAcc->CountChoiceListItems(false))
           pWidgetAcc->SetItemState(nIndex, true, false, false, true);
       }
       pWidgetAcc->SetValue(pFormField->GetValue(), XFA_VALUEPICTURE_Edit);
@@ -430,9 +430,10 @@
       pFormField->ClearOptions(true);
 
       if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
-        for (int i = 0, sz = pWidgetAcc->CountChoiceListItems(); i < sz; i++) {
+        for (int i = 0, sz = pWidgetAcc->CountChoiceListItems(false); i < sz;
+             i++) {
           CFX_WideString swText;
-          pWidgetAcc->GetChoiceListItem(swText, i);
+          pWidgetAcc->GetChoiceListItem(swText, i, false);
 
           pFormField->InsertOption(swText, i, true);
         }
@@ -444,9 +445,10 @@
       pFormField->ClearOptions(false);
 
       if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
-        for (int i = 0, sz = pWidgetAcc->CountChoiceListItems(); i < sz; i++) {
+        for (int i = 0, sz = pWidgetAcc->CountChoiceListItems(false); i < sz;
+             i++) {
           CFX_WideString swText;
-          pWidgetAcc->GetChoiceListItem(swText, i);
+          pWidgetAcc->GetChoiceListItem(swText, i, false);
 
           pFormField->InsertOption(swText, i, false);
         }
@@ -642,7 +644,7 @@
 #ifdef PDF_ENABLE_XFA
   if (CXFA_FFWidget* hWidget = GetMixXFAWidget()) {
     if (CXFA_WidgetAcc* pWidgetAcc = hWidget->GetDataAcc()) {
-      if (nIndex > -1 && nIndex < pWidgetAcc->CountChoiceListItems())
+      if (nIndex > -1 && nIndex < pWidgetAcc->CountChoiceListItems(false))
         return pWidgetAcc->GetItemState(nIndex);
 
       return false;
diff --git a/xfa/fxfa/app/cxfa_textlayout.cpp b/xfa/fxfa/app/cxfa_textlayout.cpp
index fd913c2..0fab077 100644
--- a/xfa/fxfa/app/cxfa_textlayout.cpp
+++ b/xfa/fxfa/app/cxfa_textlayout.cpp
@@ -224,12 +224,9 @@
     if (!m_pTabstopContext)
       m_pTabstopContext = pdfium::MakeUnique<CXFA_TextTabstopsContext>();
     m_textParser.GetTabstops(pStyle, m_pTabstopContext.get());
-    for (int32_t i = 0; i < m_pTabstopContext->m_iTabCount; i++) {
-      XFA_TABSTOPS* pTab = m_pTabstopContext->m_tabstops.GetDataPtr(i);
-      m_pBreak->AddPositionedTab(pTab->fTabstops);
-    }
+    for (const auto& stop : m_pTabstopContext->m_tabstops)
+      m_pBreak->AddPositionedTab(stop.fTabstops);
   }
-
   float fFontSize = m_textParser.GetFontSize(m_pTextProvider, pStyle);
   m_pBreak->SetFontSize(fFontSize);
   m_pBreak->SetLineBreakTolerance(fFontSize * 0.2f);
@@ -958,11 +955,12 @@
 
 void CXFA_TextLayout::DoTabstops(CFDE_CSSComputedStyle* pStyle,
                                  CXFA_PieceLine* pPieceLine) {
-  if (!m_pTabstopContext || m_pTabstopContext->m_iTabCount == 0)
-    return;
   if (!pStyle || !pPieceLine)
     return;
 
+  if (!m_pTabstopContext || m_pTabstopContext->m_tabstops.empty())
+    return;
+
   int32_t iPieces = pdfium::CollectionSize<int32_t>(pPieceLine->m_textPieces);
   if (iPieces == 0)
     return;
@@ -970,7 +968,7 @@
   XFA_TextPiece* pPiece = pPieceLine->m_textPieces[iPieces - 1].get();
   int32_t& iTabstopsIndex = m_pTabstopContext->m_iTabIndex;
   int32_t iCount = m_textParser.CountTabs(pStyle);
-  if (iTabstopsIndex > m_pTabstopContext->m_iTabCount - 1)
+  if (!pdfium::IndexInBounds(m_pTabstopContext->m_tabstops, iTabstopsIndex))
     return;
 
   if (iCount > 0) {
@@ -986,9 +984,7 @@
   } else if (iTabstopsIndex > -1) {
     float fLeft = 0;
     if (m_pTabstopContext->m_bTabstops) {
-      XFA_TABSTOPS* pTabstops =
-          m_pTabstopContext->m_tabstops.GetDataPtr(iTabstopsIndex);
-      uint32_t dwAlign = pTabstops->dwAlign;
+      uint32_t dwAlign = m_pTabstopContext->m_tabstops[iTabstopsIndex].dwAlign;
       if (dwAlign == FX_HashCode_GetW(L"center", false)) {
         fLeft = pPiece->rtPiece.width / 2.0f;
       } else if (dwAlign == FX_HashCode_GetW(L"right", false) ||
diff --git a/xfa/fxfa/app/cxfa_texttabstopscontext.cpp b/xfa/fxfa/app/cxfa_texttabstopscontext.cpp
index b654e87..58ceb21 100644
--- a/xfa/fxfa/app/cxfa_texttabstopscontext.cpp
+++ b/xfa/fxfa/app/cxfa_texttabstopscontext.cpp
@@ -7,33 +7,21 @@
 #include "xfa/fxfa/app/cxfa_texttabstopscontext.h"
 
 CXFA_TextTabstopsContext::CXFA_TextTabstopsContext()
-    : m_iTabCount(0),
-      m_iTabIndex(-1),
-      m_bTabstops(false),
-      m_fTabWidth(0),
-      m_fLeft(0) {}
+    : m_iTabIndex(-1), m_bTabstops(false), m_fTabWidth(0), m_fLeft(0) {}
 
 CXFA_TextTabstopsContext::~CXFA_TextTabstopsContext() {}
 
 void CXFA_TextTabstopsContext::Append(uint32_t dwAlign, float fTabstops) {
-  int32_t i = 0;
-  for (i = 0; i < m_iTabCount; i++) {
-    XFA_TABSTOPS* pTabstop = m_tabstops.GetDataPtr(i);
-    if (fTabstops < pTabstop->fTabstops) {
-      break;
-    }
-  }
-  m_tabstops.InsertSpaceAt(i, 1);
   XFA_TABSTOPS tabstop;
   tabstop.dwAlign = dwAlign;
   tabstop.fTabstops = fTabstops;
-  m_tabstops.SetAt(i, tabstop);
-  m_iTabCount++;
+
+  auto it = std::lower_bound(m_tabstops.begin(), m_tabstops.end(), tabstop);
+  m_tabstops.insert(it, tabstop);
 }
 
 void CXFA_TextTabstopsContext::RemoveAll() {
-  m_tabstops.RemoveAll();
-  m_iTabCount = 0;
+  m_tabstops.clear();
 }
 
 void CXFA_TextTabstopsContext::Reset() {
diff --git a/xfa/fxfa/app/cxfa_texttabstopscontext.h b/xfa/fxfa/app/cxfa_texttabstopscontext.h
index 64b83a4..9be10dd 100644
--- a/xfa/fxfa/app/cxfa_texttabstopscontext.h
+++ b/xfa/fxfa/app/cxfa_texttabstopscontext.h
@@ -7,11 +7,17 @@
 #ifndef XFA_FXFA_APP_CXFA_TEXTTABSTOPSCONTEXT_H_
 #define XFA_FXFA_APP_CXFA_TEXTTABSTOPSCONTEXT_H_
 
+#include <vector>
+
 #include "core/fxcrt/fx_basic.h"
 
 struct XFA_TABSTOPS {
   uint32_t dwAlign;
   float fTabstops;
+
+  bool operator<(const XFA_TABSTOPS& that) const {
+    return fTabstops < that.fTabstops;
+  }
 };
 
 class CXFA_TextTabstopsContext {
@@ -23,12 +29,11 @@
   void RemoveAll();
   void Reset();
 
-  CFX_ArrayTemplate<XFA_TABSTOPS> m_tabstops;
-  int32_t m_iTabCount;
   int32_t m_iTabIndex;
   bool m_bTabstops;
   float m_fTabWidth;
   float m_fLeft;
+  std::vector<XFA_TABSTOPS> m_tabstops;
 };
 
 #endif  // XFA_FXFA_APP_CXFA_TEXTTABSTOPSCONTEXT_H_
diff --git a/xfa/fxfa/app/xfa_ffchoicelist.cpp b/xfa/fxfa/app/xfa_ffchoicelist.cpp
index 4609645..c628d2e 100644
--- a/xfa/fxfa/app/xfa_ffchoicelist.cpp
+++ b/xfa/fxfa/app/xfa_ffchoicelist.cpp
@@ -6,6 +6,7 @@
 
 #include "xfa/fxfa/app/xfa_ffchoicelist.h"
 
+#include <algorithm>
 #include <vector>
 
 #include "third_party/base/ptr_util.h"
@@ -52,25 +53,18 @@
   m_pNormalWidget->SetDelegate(this);
   m_pNormalWidget->LockUpdate();
 
-  std::vector<CFX_WideString> wsLabelArray;
-  m_pDataAcc->GetChoiceListItems(wsLabelArray, false);
-  int32_t iItems = pdfium::CollectionSize<int32_t>(wsLabelArray);
-  for (int32_t i = 0; i < iItems; i++) {
-    pListBox->AddString(wsLabelArray[i].AsStringC());
-  }
+  for (const auto& label : m_pDataAcc->GetChoiceListItems(false))
+    pListBox->AddString(label.AsStringC());
+
   uint32_t dwExtendedStyle = FWL_STYLEEXT_LTB_ShowScrollBarFocus;
-  if (m_pDataAcc->GetChoiceListOpen() == XFA_ATTRIBUTEENUM_MultiSelect) {
+  if (m_pDataAcc->GetChoiceListOpen() == XFA_ATTRIBUTEENUM_MultiSelect)
     dwExtendedStyle |= FWL_STYLEEXT_LTB_MultiSelection;
-  }
+
   dwExtendedStyle |= GetAlignment();
   m_pNormalWidget->ModifyStylesEx(dwExtendedStyle, 0xFFFFFFFF);
-  CFX_ArrayTemplate<int32_t> iSelArray;
-  m_pDataAcc->GetSelectedItems(iSelArray);
-  int32_t iSelCount = iSelArray.GetSize();
-  for (int32_t j = 0; j < iSelCount; j++) {
-    CFWL_ListItem* item = pListBox->GetItem(nullptr, iSelArray[j]);
-    pListBox->SetSelItem(item, true);
-  }
+  for (int32_t selected : m_pDataAcc->GetSelectedItems())
+    pListBox->SetSelItem(pListBox->GetItem(nullptr, selected), true);
+
   m_pNormalWidget->UnlockUpdate();
   return CXFA_FFField::LoadWidget();
 }
@@ -84,19 +78,18 @@
 
 bool CXFA_FFListBox::CommitData() {
   CFWL_ListBox* pListBox = static_cast<CFWL_ListBox*>(m_pNormalWidget);
+  std::vector<int32_t> iSelArray;
   int32_t iSels = pListBox->CountSelItems();
-  CFX_ArrayTemplate<int32_t> iSelArray;
   for (int32_t i = 0; i < iSels; ++i)
-    iSelArray.Add(pListBox->GetSelIndex(i));
+    iSelArray.push_back(pListBox->GetSelIndex(i));
   m_pDataAcc->SetSelectedItems(iSelArray, true, false, true);
   return true;
 }
 
 bool CXFA_FFListBox::IsDataChanged() {
-  CFX_ArrayTemplate<int32_t> iSelArray;
-  m_pDataAcc->GetSelectedItems(iSelArray);
-  int32_t iOldSels = iSelArray.GetSize();
-  CFWL_ListBox* pListBox = (CFWL_ListBox*)m_pNormalWidget;
+  std::vector<int32_t> iSelArray = m_pDataAcc->GetSelectedItems();
+  int32_t iOldSels = pdfium::CollectionSize<int32_t>(iSelArray);
+  auto* pListBox = static_cast<CFWL_ListBox*>(m_pNormalWidget);
   int32_t iSels = pListBox->CountSelItems();
   if (iOldSels != iSels)
     return true;
@@ -134,28 +127,24 @@
   return dwExtendedStyle;
 }
 bool CXFA_FFListBox::UpdateFWLData() {
-  if (!m_pNormalWidget) {
+  if (!m_pNormalWidget)
     return false;
-  }
-  CFWL_ListBox* pListBox = ((CFWL_ListBox*)m_pNormalWidget);
-  CFX_ArrayTemplate<CFWL_ListItem*> selItemArray;
-  CFX_ArrayTemplate<int32_t> iSelArray;
-  m_pDataAcc->GetSelectedItems(iSelArray);
-  int32_t iSelCount = iSelArray.GetSize();
-  for (int32_t j = 0; j < iSelCount; j++) {
-    CFWL_ListItem* lpItemSel = pListBox->GetSelItem(iSelArray[j]);
-    selItemArray.Add(lpItemSel);
-  }
+
+  auto* pListBox = static_cast<CFWL_ListBox*>(m_pNormalWidget);
+  std::vector<int32_t> iSelArray = m_pDataAcc->GetSelectedItems();
+  std::vector<CFWL_ListItem*> selItemArray(iSelArray.size());
+  std::transform(iSelArray.begin(), iSelArray.end(), selItemArray.begin(),
+                 [pListBox](int32_t val) { return pListBox->GetSelItem(val); });
+
   pListBox->SetSelItem(pListBox->GetSelItem(-1), false);
-  for (int32_t i = 0; i < iSelCount; i++) {
-    ((CFWL_ListBox*)m_pNormalWidget)->SetSelItem(selItemArray[i], true);
-  }
+  for (CFWL_ListItem* pItem : selItemArray)
+    pListBox->SetSelItem(pItem, true);
+
   m_pNormalWidget->Update();
   return true;
 }
-void CXFA_FFListBox::OnSelectChanged(
-    CFWL_Widget* pWidget,
-    const CFX_ArrayTemplate<int32_t>& arrSels) {
+void CXFA_FFListBox::OnSelectChanged(CFWL_Widget* pWidget,
+                                     const std::vector<int32_t>& arrSels) {
   CXFA_EventParam eParam;
   eParam.m_eType = XFA_EVENT_Change;
   eParam.m_pTarget = m_pDataAcc;
@@ -201,7 +190,7 @@
   CXFA_FFField::OnProcessEvent(pEvent);
   switch (pEvent->GetType()) {
     case CFWL_Event::Type::SelectChanged: {
-      CFX_ArrayTemplate<int32_t> arrSels;
+      std::vector<int32_t> arrSels;
       OnSelectChanged(m_pNormalWidget, arrSels);
       break;
     }
@@ -244,17 +233,12 @@
   m_pNormalWidget->SetDelegate(this);
   m_pNormalWidget->LockUpdate();
 
-  std::vector<CFX_WideString> wsLabelArray;
-  m_pDataAcc->GetChoiceListItems(wsLabelArray, false);
-  int32_t iItems = pdfium::CollectionSize<int32_t>(wsLabelArray);
-  for (int32_t i = 0; i < iItems; i++) {
-    pComboBox->AddString(wsLabelArray[i].AsStringC());
-  }
-  CFX_ArrayTemplate<int32_t> iSelArray;
-  m_pDataAcc->GetSelectedItems(iSelArray);
-  int32_t iSelCount = iSelArray.GetSize();
-  if (iSelCount > 0) {
-    pComboBox->SetCurSel(iSelArray[0]);
+  for (const auto& label : m_pDataAcc->GetChoiceListItems(false))
+    pComboBox->AddString(label.AsStringC());
+
+  std::vector<int32_t> iSelArray = m_pDataAcc->GetSelectedItems();
+  if (!iSelArray.empty()) {
+    pComboBox->SetCurSel(iSelArray.front());
   } else {
     CFX_WideString wsText;
     m_pDataAcc->GetValue(wsText, XFA_VALUEPICTURE_Raw);
@@ -375,24 +359,25 @@
   }
   return dwExtendedStyle;
 }
+
 bool CXFA_FFComboBox::UpdateFWLData() {
-  if (!m_pNormalWidget) {
+  auto* pComboBox = static_cast<CFWL_ComboBox*>(m_pNormalWidget);
+  if (!pComboBox)
     return false;
-  }
-  CFX_ArrayTemplate<int32_t> iSelArray;
-  m_pDataAcc->GetSelectedItems(iSelArray);
-  int32_t iSelCount = iSelArray.GetSize();
-  if (iSelCount > 0) {
-    ((CFWL_ComboBox*)m_pNormalWidget)->SetCurSel(iSelArray[0]);
+
+  std::vector<int32_t> iSelArray = m_pDataAcc->GetSelectedItems();
+  if (!iSelArray.empty()) {
+    pComboBox->SetCurSel(iSelArray.front());
   } else {
     CFX_WideString wsText;
-    ((CFWL_ComboBox*)m_pNormalWidget)->SetCurSel(-1);
+    pComboBox->SetCurSel(-1);
     m_pDataAcc->GetValue(wsText, XFA_VALUEPICTURE_Raw);
-    ((CFWL_ComboBox*)m_pNormalWidget)->SetEditText(wsText);
+    pComboBox->SetEditText(wsText);
   }
-  m_pNormalWidget->Update();
+  pComboBox->Update();
   return true;
 }
+
 bool CXFA_FFComboBox::CanUndo() {
   return m_pDataAcc->IsChoiceListAllowTextEntry() &&
          ((CFWL_ComboBox*)m_pNormalWidget)->EditCanUndo();
diff --git a/xfa/fxfa/app/xfa_ffchoicelist.h b/xfa/fxfa/app/xfa_ffchoicelist.h
index b25d186..8875655 100644
--- a/xfa/fxfa/app/xfa_ffchoicelist.h
+++ b/xfa/fxfa/app/xfa_ffchoicelist.h
@@ -7,6 +7,8 @@
 #ifndef XFA_FXFA_APP_XFA_FFCHOICELIST_H_
 #define XFA_FXFA_APP_XFA_FFCHOICELIST_H_
 
+#include <vector>
+
 #include "xfa/fxfa/app/xfa_fffield.h"
 #include "xfa/fxfa/cxfa_ffpageview.h"
 
@@ -24,7 +26,7 @@
                     const CFX_Matrix* pMatrix = nullptr) override;
 
   void OnSelectChanged(CFWL_Widget* pWidget,
-                       const CFX_ArrayTemplate<int32_t>& arrSels);
+                       const std::vector<int32_t>& arrSels);
   void SetItemState(int32_t nIndex, bool bSelected);
   void InsertItem(const CFX_WideStringC& wsLabel, int32_t nIndex = -1);
   void DeleteItem(int32_t nIndex);
diff --git a/xfa/fxfa/parser/cxfa_widgetdata.cpp b/xfa/fxfa/parser/cxfa_widgetdata.cpp
index b267381..a314bec 100644
--- a/xfa/fxfa/parser/cxfa_widgetdata.cpp
+++ b/xfa/fxfa/parser/cxfa_widgetdata.cpp
@@ -795,94 +795,74 @@
   return false;
 }
 
-void CXFA_WidgetData::GetChoiceListItems(
-    std::vector<CFX_WideString>& wsTextArray,
+std::vector<CFX_WideString> CXFA_WidgetData::GetChoiceListItems(
     bool bSaveValue) {
-  std::vector<CXFA_Node*> pItems;
-  int32_t iCount = 0;
-  for (CXFA_Node* pNode = m_pNode->GetNodeItem(XFA_NODEITEM_FirstChild); pNode;
+  std::vector<CXFA_Node*> items;
+  for (CXFA_Node* pNode = m_pNode->GetNodeItem(XFA_NODEITEM_FirstChild);
+       pNode && items.size() < 2;
        pNode = pNode->GetNodeItem(XFA_NODEITEM_NextSibling)) {
-    if (pNode->GetElementType() != XFA_Element::Items)
-      continue;
-    iCount++;
-    pItems.push_back(pNode);
-    if (iCount == 2)
-      break;
+    if (pNode->GetElementType() == XFA_Element::Items)
+      items.push_back(pNode);
   }
-  if (iCount == 0)
-    return;
+  if (items.empty())
+    return std::vector<CFX_WideString>();
 
-  CXFA_Node* pItem = pItems[0];
-  if (iCount > 1) {
-    bool bItemOneHasSave = pItems[0]->GetBoolean(XFA_ATTRIBUTE_Save);
-    bool bItemTwoHasSave = pItems[1]->GetBoolean(XFA_ATTRIBUTE_Save);
+  CXFA_Node* pItem = items.front();
+  if (items.size() > 1) {
+    bool bItemOneHasSave = items[0]->GetBoolean(XFA_ATTRIBUTE_Save);
+    bool bItemTwoHasSave = items[1]->GetBoolean(XFA_ATTRIBUTE_Save);
     if (bItemOneHasSave != bItemTwoHasSave && bSaveValue == bItemTwoHasSave)
-      pItem = pItems[1];
+      pItem = items[1];
   }
+
+  std::vector<CFX_WideString> wsTextArray;
   for (CXFA_Node* pNode = pItem->GetNodeItem(XFA_NODEITEM_FirstChild); pNode;
        pNode = pNode->GetNodeItem(XFA_NODEITEM_NextSibling)) {
     wsTextArray.emplace_back();
     pNode->TryContent(wsTextArray.back());
   }
+  return wsTextArray;
 }
 
 int32_t CXFA_WidgetData::CountSelectedItems() {
-  std::vector<CFX_WideString> wsValueArray;
-  GetSelectedItemsValue(wsValueArray);
+  std::vector<CFX_WideString> wsValueArray = GetSelectedItemsValue();
   if (IsListBox() || !IsChoiceListAllowTextEntry())
     return pdfium::CollectionSize<int32_t>(wsValueArray);
 
   int32_t iSelected = 0;
-  std::vector<CFX_WideString> wsSaveTextArray;
-  GetChoiceListItems(wsSaveTextArray, true);
-  int32_t iValues = pdfium::CollectionSize<int32_t>(wsValueArray);
-  for (int32_t i = 0; i < iValues; i++) {
-    int32_t iSaves = pdfium::CollectionSize<int32_t>(wsSaveTextArray);
-    for (int32_t j = 0; j < iSaves; j++) {
-      if (wsValueArray[i] == wsSaveTextArray[j]) {
-        iSelected++;
-        break;
-      }
-    }
+  std::vector<CFX_WideString> wsSaveTextArray = GetChoiceListItems(true);
+  for (const auto& value : wsValueArray) {
+    if (pdfium::ContainsValue(wsSaveTextArray, value))
+      iSelected++;
   }
   return iSelected;
 }
 
 int32_t CXFA_WidgetData::GetSelectedItem(int32_t nIndex) {
-  std::vector<CFX_WideString> wsValueArray;
-  GetSelectedItemsValue(wsValueArray);
-  std::vector<CFX_WideString> wsSaveTextArray;
-  GetChoiceListItems(wsSaveTextArray, true);
-  int32_t iSaves = pdfium::CollectionSize<int32_t>(wsSaveTextArray);
-  for (int32_t j = 0; j < iSaves; j++) {
-    if (wsValueArray[nIndex] == wsSaveTextArray[j])
-      return j;
-  }
-  return -1;
+  std::vector<CFX_WideString> wsValueArray = GetSelectedItemsValue();
+  if (!pdfium::IndexInBounds(wsValueArray, nIndex))
+    return -1;
+
+  std::vector<CFX_WideString> wsSaveTextArray = GetChoiceListItems(true);
+  auto it = std::find(wsSaveTextArray.begin(), wsSaveTextArray.end(),
+                      wsValueArray[nIndex]);
+  return it != wsSaveTextArray.end() ? it - wsSaveTextArray.begin() : -1;
 }
 
-void CXFA_WidgetData::GetSelectedItems(CFX_ArrayTemplate<int32_t>& iSelArray) {
-  std::vector<CFX_WideString> wsValueArray;
-  GetSelectedItemsValue(wsValueArray);
-  int32_t iValues = pdfium::CollectionSize<int32_t>(wsValueArray);
-  if (iValues < 1)
-    return;
-
-  std::vector<CFX_WideString> wsSaveTextArray;
-  GetChoiceListItems(wsSaveTextArray, true);
-  int32_t iSaves = pdfium::CollectionSize<int32_t>(wsSaveTextArray);
-  for (int32_t i = 0; i < iValues; i++) {
-    for (int32_t j = 0; j < iSaves; j++) {
-      if (wsValueArray[i] == wsSaveTextArray[j]) {
-        iSelArray.Add(j);
-        break;
-      }
-    }
+std::vector<int32_t> CXFA_WidgetData::GetSelectedItems() {
+  std::vector<int32_t> iSelArray;
+  std::vector<CFX_WideString> wsValueArray = GetSelectedItemsValue();
+  std::vector<CFX_WideString> wsSaveTextArray = GetChoiceListItems(true);
+  for (const auto& value : wsValueArray) {
+    auto it = std::find(wsSaveTextArray.begin(), wsSaveTextArray.end(), value);
+    if (it != wsSaveTextArray.end())
+      iSelArray.push_back(it - wsSaveTextArray.begin());
   }
+  return iSelArray;
 }
 
-void CXFA_WidgetData::GetSelectedItemsValue(
-    std::vector<CFX_WideString>& wsSelTextArray) {
+std::vector<CFX_WideString> CXFA_WidgetData::GetSelectedItemsValue() {
+  std::vector<CFX_WideString> wsSelTextArray;
   CFX_WideString wsValue = GetRawValue();
   if (GetChoiceListOpen() == XFA_ATTRIBUTEENUM_MultiSelect) {
     if (!wsValue.IsEmpty()) {
@@ -895,7 +875,6 @@
         iStart = iEnd + 1;
         if (iStart >= iLength)
           break;
-
         iEnd = wsValue.Find(L'\n', iStart);
         if (iEnd < 0)
           wsSelTextArray.push_back(wsValue.Mid(iStart, iLength - iStart));
@@ -904,25 +883,14 @@
   } else {
     wsSelTextArray.push_back(wsValue);
   }
+  return wsSelTextArray;
 }
 
 bool CXFA_WidgetData::GetItemState(int32_t nIndex) {
-  if (nIndex < 0)
-    return false;
-
-  std::vector<CFX_WideString> wsSaveTextArray;
-  GetChoiceListItems(wsSaveTextArray, true);
-  if (pdfium::CollectionSize<int32_t>(wsSaveTextArray) <= nIndex)
-    return false;
-
-  std::vector<CFX_WideString> wsValueArray;
-  GetSelectedItemsValue(wsValueArray);
-  int32_t iValues = pdfium::CollectionSize<int32_t>(wsValueArray);
-  for (int32_t j = 0; j < iValues; j++) {
-    if (wsValueArray[j] == wsSaveTextArray[nIndex])
-      return true;
-  }
-  return false;
+  std::vector<CFX_WideString> wsSaveTextArray = GetChoiceListItems(true);
+  return pdfium::IndexInBounds(wsSaveTextArray, nIndex) &&
+         pdfium::ContainsValue(GetSelectedItemsValue(),
+                               wsSaveTextArray[nIndex]);
 }
 
 void CXFA_WidgetData::SetItemState(int32_t nIndex,
@@ -930,24 +898,17 @@
                                    bool bNotify,
                                    bool bScriptModify,
                                    bool bSyncData) {
-  if (nIndex < 0)
-    return;
-
-  std::vector<CFX_WideString> wsSaveTextArray;
-  GetChoiceListItems(wsSaveTextArray, true);
-  if (pdfium::CollectionSize<int32_t>(wsSaveTextArray) <= nIndex)
+  std::vector<CFX_WideString> wsSaveTextArray = GetChoiceListItems(true);
+  if (!pdfium::IndexInBounds(wsSaveTextArray, nIndex))
     return;
 
   int32_t iSel = -1;
-  std::vector<CFX_WideString> wsValueArray;
-  GetSelectedItemsValue(wsValueArray);
-  int32_t iValues = pdfium::CollectionSize<int32_t>(wsValueArray);
-  for (int32_t j = 0; j < iValues; j++) {
-    if (wsValueArray[j] == wsSaveTextArray[nIndex]) {
-      iSel = j;
-      break;
-    }
-  }
+  std::vector<CFX_WideString> wsValueArray = GetSelectedItemsValue();
+  auto it = std::find(wsValueArray.begin(), wsValueArray.end(),
+                      wsSaveTextArray[nIndex]);
+  if (it != wsValueArray.end())
+    iSel = it - wsValueArray.begin();
+
   if (GetChoiceListOpen() == XFA_ATTRIBUTEENUM_MultiSelect) {
     if (bSelected) {
       if (iSel < 0) {
@@ -960,14 +921,10 @@
                             bSyncData);
       }
     } else if (iSel >= 0) {
-      CFX_ArrayTemplate<int32_t> iSelArray;
-      GetSelectedItems(iSelArray);
-      for (int32_t i = 0; i < iSelArray.GetSize(); i++) {
-        if (iSelArray[i] == nIndex) {
-          iSelArray.RemoveAt(i);
-          break;
-        }
-      }
+      std::vector<int32_t> iSelArray = GetSelectedItems();
+      auto it = std::find(iSelArray.begin(), iSelArray.end(), nIndex);
+      if (it != iSelArray.end())
+        iSelArray.erase(it);
       SetSelectedItems(iSelArray, bNotify, bScriptModify, bSyncData);
     }
   } else {
@@ -986,15 +943,14 @@
   }
 }
 
-void CXFA_WidgetData::SetSelectedItems(CFX_ArrayTemplate<int32_t>& iSelArray,
+void CXFA_WidgetData::SetSelectedItems(const std::vector<int32_t>& iSelArray,
                                        bool bNotify,
                                        bool bScriptModify,
                                        bool bSyncData) {
   CFX_WideString wsValue;
-  int32_t iSize = iSelArray.GetSize();
+  int32_t iSize = pdfium::CollectionSize<int32_t>(iSelArray);
   if (iSize >= 1) {
-    std::vector<CFX_WideString> wsSaveTextArray;
-    GetChoiceListItems(wsSaveTextArray, true);
+    std::vector<CFX_WideString> wsSaveTextArray = GetChoiceListItems(true);
     CFX_WideString wsItemValue;
     for (int32_t i = 0; i < iSize; i++) {
       wsItemValue = (iSize == 1) ? wsSaveTextArray[iSelArray[i]]
@@ -1641,7 +1597,7 @@
       if (eValueType == XFA_VALUEPICTURE_Display) {
         int32_t iSelItemIndex = GetSelectedItem(0);
         if (iSelItemIndex >= 0) {
-          GetChoiceListItem(wsValue, iSelItemIndex);
+          GetChoiceListItem(wsValue, iSelItemIndex, false);
           wsPicture.clear();
         }
       }
diff --git a/xfa/fxfa/parser/cxfa_widgetdata.h b/xfa/fxfa/parser/cxfa_widgetdata.h
index 3e74018..1caecea 100644
--- a/xfa/fxfa/parser/cxfa_widgetdata.h
+++ b/xfa/fxfa/parser/cxfa_widgetdata.h
@@ -93,23 +93,22 @@
   bool IsChoiceListAllowTextEntry();
   int32_t GetChoiceListOpen();
   bool IsListBox();
-  int32_t CountChoiceListItems(bool bSaveValue = false);
+  int32_t CountChoiceListItems(bool bSaveValue);
   bool GetChoiceListItem(CFX_WideString& wsText,
                          int32_t nIndex,
-                         bool bSaveValue = false);
-  void GetChoiceListItems(std::vector<CFX_WideString>& wsTextArray,
-                          bool bSaveValue = false);
+                         bool bSaveValue);
+  std::vector<CFX_WideString> GetChoiceListItems(bool bSaveValue);
   int32_t CountSelectedItems();
   int32_t GetSelectedItem(int32_t nIndex = 0);
-  void GetSelectedItems(CFX_ArrayTemplate<int32_t>& iSelArray);
-  void GetSelectedItemsValue(std::vector<CFX_WideString>& wsSelTextArray);
+  std::vector<int32_t> GetSelectedItems();
+  std::vector<CFX_WideString> GetSelectedItemsValue();
   bool GetItemState(int32_t nIndex);
   void SetItemState(int32_t nIndex,
                     bool bSelected,
                     bool bNotify,
                     bool bScriptModify,
                     bool bSyncData);
-  void SetSelectedItems(CFX_ArrayTemplate<int32_t>& iSelArray,
+  void SetSelectedItems(const std::vector<int32_t>& iSelArray,
                         bool bNotify,
                         bool bScriptModify,
                         bool bSyncData);
diff --git a/xfa/fxfa/parser/xfa_document_datamerger_imp.cpp b/xfa/fxfa/parser/xfa_document_datamerger_imp.cpp
index 227064a..cfc54a1 100644
--- a/xfa/fxfa/parser/xfa_document_datamerger_imp.cpp
+++ b/xfa/fxfa/parser/xfa_document_datamerger_imp.cpp
@@ -153,17 +153,16 @@
       case XFA_Element::ChoiceList:
         defValue.GetChildValueContent(wsValue);
         if (pWidgetData->GetChoiceListOpen() == XFA_ATTRIBUTEENUM_MultiSelect) {
-          std::vector<CFX_WideString> wsSelTextArray;
-          pWidgetData->GetSelectedItemsValue(wsSelTextArray);
-          int32_t iSize = pdfium::CollectionSize<int32_t>(wsSelTextArray);
-          if (iSize >= 1) {
-            CXFA_Node* pValue = nullptr;
-            for (int32_t i = 0; i < iSize; i++) {
-              pValue = pDataNode->CreateSamePacketNode(XFA_Element::DataValue);
+          std::vector<CFX_WideString> wsSelTextArray =
+              pWidgetData->GetSelectedItemsValue();
+          if (!wsSelTextArray.empty()) {
+            for (const auto& text : wsSelTextArray) {
+              CXFA_Node* pValue =
+                  pDataNode->CreateSamePacketNode(XFA_Element::DataValue);
               pValue->SetCData(XFA_ATTRIBUTE_Name, L"value");
               pValue->CreateXMLMappingNode();
               pDataNode->InsertChild(pValue);
-              pValue->SetCData(XFA_ATTRIBUTE_Value, wsSelTextArray[i]);
+              pValue->SetCData(XFA_ATTRIBUTE_Value, text);
             }
           } else {
             CFDE_XMLNode* pXMLNode = pDataNode->GetXMLMappingNode();