Move parameters from CPDF_TextPageFind::FindFirst() to ctor.

Then remove redundant members and make more members const.
Also simplify FindFirst() to remove unnecessary work.

Change-Id: I9d4de76c3a05600b007224d8316a1c08999a5b52
Reviewed-on: https://pdfium-review.googlesource.com/c/34855
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdftext/cpdf_textpagefind.cpp b/core/fpdftext/cpdf_textpagefind.cpp
index 248e61a..093d335 100644
--- a/core/fpdftext/cpdf_textpagefind.cpp
+++ b/core/fpdftext/cpdf_textpagefind.cpp
@@ -32,62 +32,51 @@
   return true;
 }
 
-}  // namespace
+WideString GetStringCase(const WideString& wsOriginal, bool bMatchCase) {
+  if (bMatchCase)
+    return wsOriginal;
 
-CPDF_TextPageFind::CPDF_TextPageFind(const CPDF_TextPage* pTextPage)
-    : m_pTextPage(pTextPage) {
-  ASSERT(m_pTextPage);
-
-  m_strText = m_pTextPage->GetAllPageText();
+  WideString wsLower = wsOriginal;
+  wsLower.MakeLower();
+  return wsLower;
 }
 
-CPDF_TextPageFind::~CPDF_TextPageFind() {}
+}  // namespace
+
+CPDF_TextPageFind::CPDF_TextPageFind(const CPDF_TextPage* pTextPage,
+                                     const WideString& findwhat,
+                                     const Options& options,
+                                     Optional<size_t> startPos)
+    : m_pTextPage(pTextPage),
+      m_strText(GetStringCase(pTextPage->GetAllPageText(), options.bMatchCase)),
+      m_findWhat(GetStringCase(findwhat, options.bMatchCase)),
+      m_options(options) {
+  if (!m_strText.IsEmpty()) {
+    m_findNextStart = startPos;
+    m_findPreStart = startPos.value_or(m_strText.GetLength() - 1);
+  }
+}
+
+CPDF_TextPageFind::~CPDF_TextPageFind() = default;
 
 int CPDF_TextPageFind::GetCharIndex(int index) const {
   return m_pTextPage->CharIndexFromTextIndex(index);
 }
 
-bool CPDF_TextPageFind::FindFirst(const WideString& findwhat,
-                                  const Options& options,
-                                  Optional<size_t> startPos) {
-  if (m_strText.IsEmpty() || m_bMatchCase != options.bMatchCase)
-    m_strText = m_pTextPage->GetAllPageText();
-  WideString findwhatStr = findwhat;
-  m_findWhat = findwhatStr;
-  m_options = options;
-  m_bMatchCase = options.bMatchCase;
+bool CPDF_TextPageFind::FindFirst() {
   if (m_strText.IsEmpty())
     return true;
 
-  size_t len = findwhatStr.GetLength();
-  if (!m_bMatchCase) {
-    findwhatStr.MakeLower();
-    m_strText.MakeLower();
-  }
-  m_bMatchWholeWord = options.bMatchWholeWord;
-  m_findNextStart = startPos;
-  if (!startPos.has_value()) {
-    if (!m_strText.IsEmpty())
-      m_findPreStart = m_strText.GetLength() - 1;
-  } else {
-    m_findPreStart = startPos;
-  }
-
-  m_csFindWhatArray.clear();
+  size_t len = m_findWhat.GetLength();
   size_t i = 0;
   for (i = 0; i < len; ++i)
-    if (findwhatStr[i] != ' ')
+    if (m_findWhat[i] != ' ')
       break;
   if (i < len)
-    ExtractFindWhat(findwhatStr);
+    ExtractFindWhat();
   else
-    m_csFindWhatArray.push_back(findwhatStr);
-  if (m_csFindWhatArray.empty())
-    return false;
-
-  m_resStart = 0;
-  m_resEnd = -1;
-  return true;
+    m_csFindWhatArray.push_back(m_findWhat);
+  return !m_csFindWhatArray.empty();
 }
 
 bool CPDF_TextPageFind::FindNext() {
@@ -158,7 +147,7 @@
         }
       }
     }
-    if (m_bMatchWholeWord && bMatch) {
+    if (m_options.bMatchWholeWord && bMatch) {
       bMatch = IsMatchWholeWord(m_strText, nResultPos.value(), endIndex);
     }
     nStartPos = endIndex + 1;
@@ -185,8 +174,8 @@
   if (m_strText.IsEmpty() || !m_findPreStart.has_value())
     return false;
 
-  CPDF_TextPageFind find_engine(m_pTextPage.Get());
-  if (!find_engine.FindFirst(m_findWhat, m_options, 0))
+  CPDF_TextPageFind find_engine(m_pTextPage.Get(), m_findWhat, m_options, 0);
+  if (!find_engine.FindFirst())
     return false;
 
   int order = -1;
@@ -216,13 +205,14 @@
   return true;
 }
 
-void CPDF_TextPageFind::ExtractFindWhat(const WideString& findwhat) {
-  if (findwhat.IsEmpty())
+void CPDF_TextPageFind::ExtractFindWhat() {
+  if (m_findWhat.IsEmpty())
     return;
+
   int index = 0;
   while (1) {
     Optional<WideString> word =
-        ExtractSubString(findwhat.c_str(), index, TEXT_SPACE_CHAR);
+        ExtractSubString(m_findWhat.c_str(), index, TEXT_SPACE_CHAR);
     if (!word)
       break;
 
diff --git a/core/fpdftext/cpdf_textpagefind.h b/core/fpdftext/cpdf_textpagefind.h
index 1f73918..9f3ac13 100644
--- a/core/fpdftext/cpdf_textpagefind.h
+++ b/core/fpdftext/cpdf_textpagefind.h
@@ -25,19 +25,22 @@
     bool bConsecutive = false;
   };
 
-  explicit CPDF_TextPageFind(const CPDF_TextPage* pTextPage);
+  CPDF_TextPageFind(const CPDF_TextPage* pTextPage,
+                    const WideString& findwhat,
+                    const Options& options,
+                    Optional<size_t> startPos);
   ~CPDF_TextPageFind();
 
-  bool FindFirst(const WideString& findwhat,
-                 const Options& options,
-                 Optional<size_t> startPos);
+  // Should be called immediately after construction.
+  bool FindFirst();
+
   bool FindNext();
   bool FindPrev();
   int GetCurOrder() const;
   int GetMatchedCount() const;
 
  protected:
-  void ExtractFindWhat(const WideString& findwhat);
+  void ExtractFindWhat();
   bool IsMatchWholeWord(const WideString& csPageText,
                         size_t startPos,
                         size_t endPos);
@@ -48,16 +51,14 @@
 
  private:
   UnownedPtr<const CPDF_TextPage> const m_pTextPage;
-  WideString m_strText;
-  WideString m_findWhat;
+  const WideString m_strText;
+  const WideString m_findWhat;
   std::vector<WideString> m_csFindWhatArray;
   Optional<size_t> m_findNextStart;
   Optional<size_t> m_findPreStart;
   int m_resStart = 0;
   int m_resEnd = -1;
-  Options m_options;
-  bool m_bMatchCase = false;
-  bool m_bMatchWholeWord = false;
+  const Options m_options;
 };
 
 #endif  // CORE_FPDFTEXT_CPDF_TEXTPAGEFIND_H_
diff --git a/fpdfsdk/fpdf_text.cpp b/fpdfsdk/fpdf_text.cpp
index a6e5c5f..9934c0c 100644
--- a/fpdfsdk/fpdf_text.cpp
+++ b/fpdfsdk/fpdf_text.cpp
@@ -276,10 +276,11 @@
   options.bMatchWholeWord = !!(flags & FPDF_MATCHWHOLEWORD);
   options.bConsecutive = !!(flags & FPDF_CONSECUTIVE);
   auto find = pdfium::MakeUnique<CPDF_TextPageFind>(
-      CPDFTextPageFromFPDFTextPage(text_page));
-  find->FindFirst(
+      CPDFTextPageFromFPDFTextPage(text_page),
       WideStringFromFPDFWideString(findwhat), options,
       start_index >= 0 ? Optional<size_t>(start_index) : pdfium::nullopt);
+  find->FindFirst();
+
   // Caller takes ownership.
   return FPDFSchHandleFromCPDFTextPageFind(find.release());
 }