Change `CPDF_TextPage::m_CharIndices` to be a struct.
Use a well-defined struct to store the data, instead of using a
convention where even-indexed array values are indices and odd-indexed
array values are counts. Also change the stored values to ints, as there
may be more that UINT16_MAX characters.
Change-Id: I11f3b1ebaec71520f1b80c5d181b023585abf948
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/98291
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdftext/cpdf_textpage.cpp b/core/fpdftext/cpdf_textpage.cpp
index 8a20482..b6ed0df 100644
--- a/core/fpdftext/cpdf_textpage.cpp
+++ b/core/fpdftext/cpdf_textpage.cpp
@@ -294,28 +294,25 @@
const int nCount = CountChars();
if (nCount)
- m_CharIndices.push_back(0);
+ m_CharIndices.push_back({0, 0});
+ bool skipped = false;
for (int i = 0; i < nCount; ++i) {
const CharInfo& charinfo = m_CharList[i];
if (charinfo.m_CharType == CPDF_TextPage::CharType::kGenerated ||
(charinfo.m_Unicode != 0 && !IsControlChar(charinfo)) ||
(charinfo.m_Unicode == 0 && charinfo.m_CharCode != 0)) {
- if (m_CharIndices.size() % 2) {
- m_CharIndices.push_back(1);
- } else {
- m_CharIndices.back() += 1;
- }
+ m_CharIndices.back().count++;
+ skipped = true;
} else {
- if (m_CharIndices.size() % 2) {
- m_CharIndices.back() = i + 1;
+ if (skipped) {
+ m_CharIndices.push_back({i + 1, 0});
+ skipped = false;
} else {
- m_CharIndices.push_back(i + 1);
+ m_CharIndices.back().index = i + 1;
}
}
}
- if (m_CharIndices.size() % 2)
- m_CharIndices.pop_back();
}
int CPDF_TextPage::CountChars() const {
@@ -324,22 +321,22 @@
int CPDF_TextPage::CharIndexFromTextIndex(int text_index) const {
int count = 0;
- for (size_t i = 0; i < m_CharIndices.size(); i += 2) {
- count += m_CharIndices[i + 1];
+ for (const auto& info : m_CharIndices) {
+ count += info.count;
if (count > text_index)
- return text_index - count + m_CharIndices[i + 1] + m_CharIndices[i];
+ return text_index - count + info.count + info.index;
}
return -1;
}
int CPDF_TextPage::TextIndexFromCharIndex(int char_index) const {
int count = 0;
- for (size_t i = 0; i < m_CharIndices.size(); i += 2) {
- int text_index = char_index - m_CharIndices[i];
- if (text_index < m_CharIndices[i + 1])
+ for (const auto& info : m_CharIndices) {
+ int text_index = char_index - info.index;
+ if (text_index < info.count)
return text_index >= 0 ? text_index + count : -1;
- count += m_CharIndices[i + 1];
+ count += info.count;
}
return -1;
}
diff --git a/core/fpdftext/cpdf_textpage.h b/core/fpdftext/cpdf_textpage.h
index 8c881bf..17954ca 100644
--- a/core/fpdftext/cpdf_textpage.h
+++ b/core/fpdftext/cpdf_textpage.h
@@ -14,7 +14,6 @@
#include <vector>
#include "core/fpdfapi/page/cpdf_pageobjectholder.h"
-#include "core/fxcrt/data_vector.h"
#include "core/fxcrt/fx_coordinates.h"
#include "core/fxcrt/unowned_ptr.h"
#include "core/fxcrt/widestring.h"
@@ -103,6 +102,11 @@
CFX_Matrix m_formMatrix;
};
+ struct CharSegment {
+ int index;
+ int count;
+ };
+
void Init();
bool IsHyphen(wchar_t curChar) const;
void ProcessObject();
@@ -137,7 +141,7 @@
const std::function<bool(const CharInfo&)>& predicate) const;
UnownedPtr<const CPDF_Page> const m_pPage;
- DataVector<uint16_t> m_CharIndices;
+ std::vector<CharSegment> m_CharIndices;
std::deque<CharInfo> m_CharList;
std::deque<CharInfo> m_TempCharList;
WideTextBuffer m_TextBuf;