7#include "core/fpdftext/cpdf_textpagefind.h"
13#include "core/fpdftext/cpdf_textpage.h"
14#include "core/fxcrt/check.h"
15#include "core/fxcrt/compiler_specific.h"
16#include "core/fxcrt/fx_extension.h"
17#include "core/fxcrt/fx_string.h"
18#include "core/fxcrt/fx_system.h"
19#include "core/fxcrt/fx_unicode.h"
20#include "core/fxcrt/ptr_util.h"
21#include "core/fxcrt/stl_util.h"
25constexpr wchar_t kNonBreakingSpace = 160;
27bool IsIgnoreSpaceCharacter(
wchar_t curChar) {
28 if (curChar < 255 || (curChar >= 0x0600 && curChar <= 0x06FF) ||
29 (curChar >= 0xFE70 && curChar <= 0xFEFF) ||
30 (curChar >= 0xFB50 && curChar <= 0xFDFF) ||
31 (curChar >= 0x0400 && curChar <= 0x04FF) ||
32 (curChar >= 0x0500 && curChar <= 0x052F) ||
33 (curChar >= 0xA640 && curChar <= 0xA69F) ||
34 (curChar >= 0x2DE0 && curChar <= 0x2DFF) || curChar == 8467 ||
35 (curChar >= 0x2000 && curChar <= 0x206F)) {
41bool IsMatchWholeWord(
const WideString& csPageText,
44 if (startPos > endPos)
46 wchar_t char_left = 0;
47 wchar_t char_right = 0;
48 size_t char_count = endPos - startPos + 1;
51 if (char_count == 1 && csPageText[startPos] > 255)
54 char_left = csPageText[startPos - 1];
55 if (startPos + char_count < csPageText.GetLength())
56 char_right = csPageText[startPos + char_count];
57 if ((char_left >
'A' && char_left <
'a') ||
58 (char_left >
'a' && char_left <
'z') ||
59 (char_left > 0xfb00 && char_left < 0xfb06) ||
61 (char_right >
'A' && char_right <
'a') ||
62 (char_right >
'a' && char_right <
'z') ||
63 (char_right > 0xfb00 && char_right < 0xfb06) ||
67 if (!((
'A' > char_left || char_left >
'Z') &&
68 (
'a' > char_left || char_left >
'z') &&
69 (
'A' > char_right || char_right >
'Z') &&
70 (
'a' > char_right || char_right >
'z'))) {
75 FXSYS_IsDecimalDigit(csPageText[startPos])) {
79 FXSYS_IsDecimalDigit(csPageText[endPos])) {
95std::optional<
WideString> ExtractSubString(
const wchar_t* lpszFullString,
99 while (iSubString--) {
100 lpszFullString = wcschr(lpszFullString, L' ');
101 if (!lpszFullString) {
106 while (*lpszFullString == L' ') {
111 const wchar_t* lpchEnd = wcschr(lpszFullString, L' ');
112 int nLen = lpchEnd ?
static_cast<
int>(lpchEnd - lpszFullString)
113 :
static_cast<
int>(wcslen(lpszFullString));
118 return WideString(lpszFullString,
static_cast<size_t>(nLen));
122std::vector<WideString> ExtractFindWhat(
const WideString& findwhat) {
123 std::vector<WideString> findwhat_array;
125 size_t len = findwhat.GetLength();
127 for (i = 0; i < len; ++i)
128 if (findwhat[i] !=
' ')
131 findwhat_array.push_back(findwhat);
132 return findwhat_array;
137 std::optional<
WideString> word = ExtractSubString(findwhat.c_str(), index);
138 if (!word.has_value())
141 if (word->IsEmpty()) {
142 findwhat_array.push_back(L"");
148 while (pos < word->GetLength()) {
150 wchar_t curChar = word.value()[pos];
151 if (IsIgnoreSpaceCharacter(curChar)) {
157 findwhat_array.push_back(word->First(pos));
158 findwhat_array.push_back(curStr);
159 if (pos == word->GetLength() - 1) {
163 word.emplace(word->Last(word->GetLength() - pos - 1));
170 if (!word->IsEmpty())
171 findwhat_array.push_back(word.value());
174 return findwhat_array;
184 std::optional<size_t> startPos) {
185 std::vector<WideString> findwhat_array =
186 ExtractFindWhat(GetStringCase(findwhat, options.bMatchCase));
187 auto find =
pdfium::WrapUnique(
195 const std::vector<WideString>& findwhat_array,
197 std::optional<size_t> startPos)
198 : m_pTextPage(pTextPage),
199 m_strText(GetStringCase(pTextPage->GetAllPageText(), options.bMatchCase)),
200 m_csFindWhatArray(findwhat_array),
202 if (!m_strText.IsEmpty()) {
203 m_findNextStart = startPos;
204 m_findPreStart = startPos.value_or(m_strText.GetLength() - 1);
211 return m_pTextPage->CharIndexFromTextIndex(index);
215 return m_strText.IsEmpty() || !m_csFindWhatArray.empty();
219 if (m_strText.IsEmpty() || !m_findNextStart.has_value())
222 const size_t strLen = m_strText.GetLength();
223 size_t nStartPos = m_findNextStart.value();
224 if (nStartPos >= strLen) {
228 int nCount = fxcrt::CollectionSize<
int>(m_csFindWhatArray);
229 std::optional<size_t> nResultPos = 0;
230 bool bSpaceStart =
false;
231 for (
int iWord = 0; iWord < nCount; iWord++) {
233 if (csWord.IsEmpty()) {
234 if (iWord == nCount - 1) {
235 if (nStartPos >= strLen) {
238 wchar_t strInsert = m_strText[nStartPos];
239 if (strInsert == L'\n' || strInsert == L' ' || strInsert == L'\r' ||
240 strInsert == kNonBreakingSpace) {
241 nResultPos = nStartPos + 1;
245 }
else if (iWord == 0) {
250 nResultPos = m_strText.Find(csWord.AsStringView(), nStartPos);
251 if (!nResultPos.has_value())
254 size_t endIndex = nResultPos.value() + csWord.GetLength() - 1;
256 m_resStart = nResultPos.value();
258 if (iWord != 0 && !bSpaceStart) {
259 size_t PreResEndPos = nStartPos;
260 int curChar = csWord[0];
261 WideString lastWord = m_csFindWhatArray[iWord - 1];
262 int lastChar = lastWord.Back();
263 if (nStartPos == nResultPos.value() &&
264 !(IsIgnoreSpaceCharacter(lastChar) ||
265 IsIgnoreSpaceCharacter(curChar))) {
268 for (size_t d = PreResEndPos; d < nResultPos.value(); d++) {
269 wchar_t strInsert = m_strText[d];
270 if (strInsert != L'\n' && strInsert != L' ' && strInsert != L'\r' &&
271 strInsert != kNonBreakingSpace) {
276 }
else if (bSpaceStart) {
277 if (nResultPos.value() > 0) {
278 wchar_t strInsert = m_strText[nResultPos.value() - 1];
279 if (strInsert != L'\n' && strInsert != L' ' && strInsert != L'\r' &&
280 strInsert != kNonBreakingSpace) {
282 m_resStart = nResultPos.value();
284 m_resStart = nResultPos.value() - 1;
288 if (m_options.bMatchWholeWord && bMatch)
289 bMatch = IsMatchWholeWord(m_strText, nResultPos.value(), endIndex);
292 nStartPos = endIndex + 1;
295 size_t index = bSpaceStart ? 1 : 0;
296 nStartPos = m_resStart + m_csFindWhatArray[index].GetLength();
299 m_resEnd = nResultPos.value() + m_csFindWhatArray.back().GetLength() - 1;
301 m_findNextStart = m_resStart + 1;
302 m_findPreStart = m_resEnd - 1;
304 m_findNextStart = m_resEnd + 1;
305 m_findPreStart = m_resStart - 1;
311 if (m_strText.IsEmpty() || !m_findPreStart.has_value())
315 if (!find_engine.FindFirst())
323 int temp = cur_order + cur_match;
324 if (temp < 0 ||
static_cast<size_t>(temp) > m_findPreStart.value() + 1)
333 m_resStart = m_pTextPage->TextIndexFromCharIndex(order);
334 m_resEnd = m_pTextPage->TextIndexFromCharIndex(order + matches - 1);
336 m_findNextStart = m_resStart + 1;
337 m_findPreStart = m_resEnd - 1;
339 m_findNextStart = m_resEnd + 1;
340 m_findPreStart = m_resStart - 1;
346 return GetCharIndex(m_resStart);
350 int resStart = GetCharIndex(m_resStart);
351 int resEnd = GetCharIndex(m_resEnd);
352 return resEnd - resStart + 1;
int GetMatchedCount() const
bool FXSYS_IsDecimalDigit(wchar_t c)
constexpr wchar_t kRightSingleQuotationMark
fxcrt::WideString WideString