Modernize parts of CPDFSDK_AnnotIterator
- Change some method parameters to use spans and pass by reference.
- Use range-based for-loops.
- Change enum TabOrder to an enum class.
Change-Id: I6c227449314918462d90886fa036b08d7f975231
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/111951
Reviewed-by: Nigi <nigi@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/fpdfsdk/cpdfsdk_annotiterator.cpp b/fpdfsdk/cpdfsdk_annotiterator.cpp
index 58ec083..326624b 100644
--- a/fpdfsdk/cpdfsdk_annotiterator.cpp
+++ b/fpdfsdk/cpdfsdk_annotiterator.cpp
@@ -14,7 +14,9 @@
#include "fpdfsdk/cpdfsdk_annot.h"
#include "fpdfsdk/cpdfsdk_pageview.h"
#include "fpdfsdk/cpdfsdk_widget.h"
+#include "third_party/base/containers/adapters.h"
#include "third_party/base/containers/contains.h"
+#include "third_party/base/containers/span.h"
namespace {
@@ -80,23 +82,25 @@
}
CFX_FloatRect CPDFSDK_AnnotIterator::AddToAnnotsList(
- std::vector<UnownedPtr<CPDFSDK_Annot>>* sa,
+ std::vector<UnownedPtr<CPDFSDK_Annot>>& sa,
size_t idx) {
- CPDFSDK_Annot* pLeftTopAnnot = sa->at(idx);
+ CPDFSDK_Annot* pLeftTopAnnot = sa[idx];
CFX_FloatRect rcLeftTop = GetAnnotRect(pLeftTopAnnot);
m_Annots.emplace_back(pLeftTopAnnot);
- sa->erase(sa->begin() + idx);
+ sa.erase(sa.begin() + idx);
return rcLeftTop;
}
void CPDFSDK_AnnotIterator::AddSelectedToAnnots(
- std::vector<UnownedPtr<CPDFSDK_Annot>>* sa,
- std::vector<size_t>* aSelect) {
- for (size_t i = 0; i < aSelect->size(); ++i)
- m_Annots.emplace_back(sa->at(aSelect->at(i)));
+ std::vector<UnownedPtr<CPDFSDK_Annot>>& sa,
+ pdfium::span<const size_t> aSelect) {
+ for (size_t select_idx : aSelect) {
+ m_Annots.emplace_back(sa[select_idx]);
+ }
- for (size_t i = aSelect->size(); i > 0; --i)
- sa->erase(sa->begin() + aSelect->at(i - 1));
+ for (size_t select_idx : pdfium::base::Reversed(aSelect)) {
+ sa.erase(sa.begin() + select_idx);
+ }
}
// static
@@ -105,19 +109,19 @@
CPDF_Page* pPDFPage = pPageView->GetPDFPage();
ByteString sTabs = pPDFPage->GetDict()->GetByteStringFor("Tabs");
if (sTabs == "R")
- return kRow;
+ return TabOrder::kRow;
if (sTabs == "C")
- return kColumn;
- return kStructure;
+ return TabOrder::kColumn;
+ return TabOrder::kStructure;
}
void CPDFSDK_AnnotIterator::GenerateResults() {
switch (m_eTabOrder) {
- case kStructure:
+ case TabOrder::kStructure:
CollectAnnots(&m_Annots);
break;
- case kRow: {
+ case TabOrder::kRow: {
std::vector<UnownedPtr<CPDFSDK_Annot>> sa;
CollectAnnots(&sa);
std::sort(sa.begin(), sa.end(), CompareByLeftAscending);
@@ -135,7 +139,7 @@
if (nLeftTopIndex < 0)
continue;
- CFX_FloatRect rcLeftTop = AddToAnnotsList(&sa, nLeftTopIndex);
+ CFX_FloatRect rcLeftTop = AddToAnnotsList(sa, nLeftTopIndex);
std::vector<size_t> aSelect;
for (size_t i = 0; i < sa.size(); ++i) {
@@ -144,12 +148,12 @@
if (fCenterY > rcLeftTop.bottom && fCenterY < rcLeftTop.top)
aSelect.push_back(i);
}
- AddSelectedToAnnots(&sa, &aSelect);
+ AddSelectedToAnnots(sa, aSelect);
}
break;
}
- case kColumn: {
+ case TabOrder::kColumn: {
std::vector<UnownedPtr<CPDFSDK_Annot>> sa;
CollectAnnots(&sa);
std::sort(sa.begin(), sa.end(), CompareByTopDescending);
@@ -170,7 +174,7 @@
if (nLeftTopIndex < 0)
continue;
- CFX_FloatRect rcLeftTop = AddToAnnotsList(&sa, nLeftTopIndex);
+ CFX_FloatRect rcLeftTop = AddToAnnotsList(sa, nLeftTopIndex);
std::vector<size_t> aSelect;
for (size_t i = 0; i < sa.size(); ++i) {
@@ -179,7 +183,7 @@
if (fCenterX > rcLeftTop.left && fCenterX < rcLeftTop.right)
aSelect.push_back(i);
}
- AddSelectedToAnnots(&sa, &aSelect);
+ AddSelectedToAnnots(sa, aSelect);
}
break;
}
diff --git a/fpdfsdk/cpdfsdk_annotiterator.h b/fpdfsdk/cpdfsdk_annotiterator.h
index 74f93a4..e1b2e13 100644
--- a/fpdfsdk/cpdfsdk_annotiterator.h
+++ b/fpdfsdk/cpdfsdk_annotiterator.h
@@ -12,6 +12,7 @@
#include "core/fpdfdoc/cpdf_annot.h"
#include "core/fxcrt/fx_coordinates.h"
#include "core/fxcrt/unowned_ptr.h"
+#include "third_party/base/containers/span.h"
class CPDFSDK_Annot;
class CPDFSDK_PageView;
@@ -29,16 +30,16 @@
CPDFSDK_Annot* GetPrevAnnot(CPDFSDK_Annot* pAnnot);
private:
- enum TabOrder : uint8_t { kStructure = 0, kRow, kColumn };
+ enum class TabOrder : uint8_t { kStructure = 0, kRow, kColumn };
static TabOrder GetTabOrder(CPDFSDK_PageView* pPageView);
void GenerateResults();
void CollectAnnots(std::vector<UnownedPtr<CPDFSDK_Annot>>* pArray);
- CFX_FloatRect AddToAnnotsList(std::vector<UnownedPtr<CPDFSDK_Annot>>* sa,
+ CFX_FloatRect AddToAnnotsList(std::vector<UnownedPtr<CPDFSDK_Annot>>& sa,
size_t idx);
- void AddSelectedToAnnots(std::vector<UnownedPtr<CPDFSDK_Annot>>* sa,
- std::vector<size_t>* aSelect);
+ void AddSelectedToAnnots(std::vector<UnownedPtr<CPDFSDK_Annot>>& sa,
+ pdfium::span<const size_t> aSelect);
UnownedPtr<CPDFSDK_PageView> const m_pPageView;
const std::vector<CPDF_Annot::Subtype> m_subtypes;