summaryrefslogtreecommitdiff
path: root/core/fpdfdoc/csection.cpp
diff options
context:
space:
mode:
authorTom Sepez <tsepez@chromium.org>2017-03-20 11:51:32 -0700
committerChromium commit bot <commit-bot@chromium.org>2017-03-20 21:41:00 +0000
commita105e003fd8ac96029f015f20ac8ad1dee8e6391 (patch)
treee10bed113802113f3b34ba5363f84e7ee198a143 /core/fpdfdoc/csection.cpp
parent25a9caff72ca0267e9ede38f780127e32c51cb9d (diff)
downloadpdfium-a105e003fd8ac96029f015f20ac8ad1dee8e6391.tar.xz
Use std::vector<std::unique_ptr<CPVT_WordInfo>> as word array.
We never push nullptrs into this array, so remove some checks for nullness, but be really careful about bounds checking. Change-Id: I79960a4cc9a729b3d5985f297aea8c4b03ceb601 Reviewed-on: https://pdfium-review.googlesource.com/3103 Reviewed-by: Lei Zhang <thestig@chromium.org> Commit-Queue: Tom Sepez <tsepez@chromium.org>
Diffstat (limited to 'core/fpdfdoc/csection.cpp')
-rw-r--r--core/fpdfdoc/csection.cpp77
1 files changed, 29 insertions, 48 deletions
diff --git a/core/fpdfdoc/csection.cpp b/core/fpdfdoc/csection.cpp
index 0c7825585c..6198d780fd 100644
--- a/core/fpdfdoc/csection.cpp
+++ b/core/fpdfdoc/csection.cpp
@@ -15,25 +15,13 @@
CSection::CSection(CPDF_VariableText* pVT) : m_pVT(pVT) {}
-CSection::~CSection() {
- ResetAll();
-}
+CSection::~CSection() {}
void CSection::ResetAll() {
- ResetWordArray();
- ResetLineArray();
-}
-
-void CSection::ResetLineArray() {
+ m_WordArray.clear();
m_LineArray.clear();
}
-void CSection::ResetWordArray() {
- for (int32_t i = 0, sz = m_WordArray.GetSize(); i < sz; i++)
- delete m_WordArray.GetAt(i);
- m_WordArray.RemoveAll();
-}
-
void CSection::ResetLinePlace() {
int32_t i = 0;
for (auto& pLine : m_LineArray) {
@@ -44,13 +32,10 @@ void CSection::ResetLinePlace() {
CPVT_WordPlace CSection::AddWord(const CPVT_WordPlace& place,
const CPVT_WordInfo& wordinfo) {
- CPVT_WordInfo* pWord = new CPVT_WordInfo(wordinfo);
- int32_t nWordIndex =
- pdfium::clamp(place.nWordIndex, 0, m_WordArray.GetSize());
- if (nWordIndex == m_WordArray.GetSize())
- m_WordArray.Add(pWord);
- else
- m_WordArray.InsertAt(nWordIndex, pWord);
+ int32_t nWordIndex = pdfium::clamp(
+ place.nWordIndex, 0, pdfium::CollectionSize<int32_t>(m_WordArray));
+ m_WordArray.insert(m_WordArray.begin() + nWordIndex,
+ pdfium::MakeUnique<CPVT_WordInfo>(wordinfo));
return place;
}
@@ -202,54 +187,50 @@ CPVT_WordPlace CSection::SearchWordPlace(float fx,
int32_t nRight = range.EndPos.nWordIndex + 1;
int32_t nMid = (nLeft + nRight) / 2;
while (nLeft < nRight) {
- if (nMid == nLeft) {
+ if (nMid == nLeft)
break;
- }
if (nMid == nRight) {
nMid--;
break;
}
- if (CPVT_WordInfo* pWord = m_WordArray.GetAt(nMid)) {
- if (fx >
- pWord->fWordX + m_pVT->GetWordWidth(*pWord) * VARIABLETEXT_HALF) {
- nLeft = nMid;
- nMid = (nLeft + nRight) / 2;
- continue;
- } else {
- nRight = nMid;
- nMid = (nLeft + nRight) / 2;
- continue;
- }
- } else {
+ if (!pdfium::IndexInBounds(m_WordArray, nMid))
break;
+ CPVT_WordInfo* pWord = m_WordArray[nMid].get();
+ if (fx > pWord->fWordX + m_pVT->GetWordWidth(*pWord) * VARIABLETEXT_HALF) {
+ nLeft = nMid;
+ nMid = (nLeft + nRight) / 2;
+ continue;
}
+ nRight = nMid;
+ nMid = (nLeft + nRight) / 2;
}
- if (CPVT_WordInfo* pWord = m_WordArray.GetAt(nMid)) {
- if (fx > pWord->fWordX + m_pVT->GetWordWidth(*pWord) * VARIABLETEXT_HALF) {
+ if (pdfium::IndexInBounds(m_WordArray, nMid)) {
+ CPVT_WordInfo* pWord = m_WordArray[nMid].get();
+ if (fx > pWord->fWordX + m_pVT->GetWordWidth(*pWord) * VARIABLETEXT_HALF)
wordplace.nWordIndex = nMid;
- }
}
return wordplace;
}
void CSection::ClearLeftWords(int32_t nWordIndex) {
for (int32_t i = nWordIndex; i >= 0; i--) {
- delete m_WordArray.GetAt(i);
- m_WordArray.RemoveAt(i);
+ if (pdfium::IndexInBounds(m_WordArray, i))
+ m_WordArray.erase(m_WordArray.begin() + i);
}
}
void CSection::ClearRightWords(int32_t nWordIndex) {
- for (int32_t i = m_WordArray.GetSize() - 1; i > nWordIndex; i--) {
- delete m_WordArray.GetAt(i);
- m_WordArray.RemoveAt(i);
+ int32_t sz = pdfium::CollectionSize<int32_t>(m_WordArray);
+ for (int32_t i = sz - 1; i > nWordIndex; i--) {
+ if (pdfium::IndexInBounds(m_WordArray, i))
+ m_WordArray.erase(m_WordArray.begin() + i);
}
}
void CSection::ClearMidWords(int32_t nBeginIndex, int32_t nEndIndex) {
for (int32_t i = nEndIndex; i > nBeginIndex; i--) {
- delete m_WordArray.GetAt(i);
- m_WordArray.RemoveAt(i);
+ if (pdfium::IndexInBounds(m_WordArray, i))
+ m_WordArray.erase(m_WordArray.begin() + i);
}
}
@@ -266,11 +247,11 @@ void CSection::ClearWords(const CPVT_WordRange& PlaceRange) {
} else if (PlaceRange.EndPos.WordCmp(SecEndPos) <= 0) {
ClearLeftWords(PlaceRange.EndPos.nWordIndex);
} else {
- ResetWordArray();
+ m_WordArray.clear();
}
}
void CSection::ClearWord(const CPVT_WordPlace& place) {
- delete m_WordArray.GetAt(place.nWordIndex);
- m_WordArray.RemoveAt(place.nWordIndex);
+ if (pdfium::IndexInBounds(m_WordArray, place.nWordIndex))
+ m_WordArray.erase(m_WordArray.begin() + place.nWordIndex);
}