Use size_t indices in CPDF_TextPage.

Use std::reverse() and/or iterators to avoid reworking some
signed int comparisons.

Change-Id: Ib920974a963c267e8184f162ef3158ae88717176
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/91511
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdftext/cpdf_textpage.cpp b/core/fpdftext/cpdf_textpage.cpp
index 190f488..073f7f3 100644
--- a/core/fpdftext/cpdf_textpage.cpp
+++ b/core/fpdftext/cpdf_textpage.cpp
@@ -923,22 +923,22 @@
     m_pPrevTextObj = pPrevCharInfo->m_pTextObj;
 }
 
-void CPDF_TextPage::SwapTempTextBuf(int iCharListStartAppend,
-                                    int iBufStartAppend) {
+void CPDF_TextPage::SwapTempTextBuf(size_t iCharListStartAppend,
+                                    size_t iBufStartAppend) {
   DCHECK(!m_TempCharList.empty());
-  int i = iCharListStartAppend;
-  int j = fxcrt::CollectionSize<int>(m_TempCharList) - 1;
-  for (; i < j; ++i, --j) {
-    std::swap(m_TempCharList[i], m_TempCharList[j]);
-    std::swap(m_TempCharList[i].m_Index, m_TempCharList[j].m_Index);
+  if (iCharListStartAppend < m_TempCharList.size()) {
+    auto fwd = m_TempCharList.begin() + iCharListStartAppend;
+    auto rev = m_TempCharList.end() - 1;
+    for (; fwd < rev; ++fwd, --rev) {
+      std::swap(*fwd, *rev);
+      std::swap(fwd->m_Index, rev->m_Index);
+    }
   }
-
   pdfium::span<wchar_t> temp_span = m_TempTextBuf.GetWideSpan();
   DCHECK(!temp_span.empty());
-  i = iBufStartAppend;
-  j = fxcrt::CollectionSize<int>(temp_span) - 1;
-  for (; i < j; ++i, --j)
-    std::swap(temp_span[i], temp_span[j]);
+  if (iBufStartAppend < temp_span.size()) {
+    std::reverse(temp_span.begin() + iBufStartAppend, temp_span.end());
+  }
 }
 
 void CPDF_TextPage::ProcessTextObject(const TransformedTextObject& obj) {
@@ -1023,8 +1023,8 @@
   const bool bR2L = IsRightToLeft(*pTextObj, *pFont);
   const bool bIsBidiAndMirrorInverse =
       bR2L && (matrix.a * matrix.d - matrix.b * matrix.c) < 0;
-  int32_t iBufStartAppend = m_TempTextBuf.GetLength();
-  int32_t iCharListStartAppend = fxcrt::CollectionSize<int32_t>(m_TempCharList);
+  const size_t iBufStartAppend = m_TempTextBuf.GetLength();
+  const size_t iCharListStartAppend = m_TempCharList.size();
 
   float spacing = 0;
   const size_t nItems = pTextObj->CountItems();
@@ -1118,7 +1118,7 @@
       m_TempTextBuf.AppendChar(0xfffe);
       continue;
     }
-    int nTotal = wstrItem.GetLength();
+    size_t nTotal = wstrItem.GetLength();
     bool bDel = false;
     const int count = std::min(fxcrt::CollectionSize<int>(m_TempCharList), 7);
     constexpr float kTextCharRatioGapDelta = 0.07f;
@@ -1136,7 +1136,7 @@
       }
     }
     if (!bDel) {
-      for (int nIndex = 0; nIndex < nTotal; ++nIndex) {
+      for (size_t nIndex = 0; nIndex < nTotal; ++nIndex) {
         charinfo.m_Unicode = wstrItem[nIndex];
         if (charinfo.m_Unicode) {
           charinfo.m_Index = m_TextBuf.GetLength();
diff --git a/core/fpdftext/cpdf_textpage.h b/core/fpdftext/cpdf_textpage.h
index 627a297..51f2686 100644
--- a/core/fpdftext/cpdf_textpage.h
+++ b/core/fpdftext/cpdf_textpage.h
@@ -130,7 +130,7 @@
       const CPDF_TextObject* pTextObj) const;
   TextOrientation FindTextlineFlowOrientation() const;
   void AppendGeneratedCharacter(wchar_t unicode, const CFX_Matrix& formMatrix);
-  void SwapTempTextBuf(int iCharListStartAppend, int iBufStartAppend);
+  void SwapTempTextBuf(size_t iCharListStartAppend, size_t iBufStartAppend);
   WideString GetTextByPredicate(
       const std::function<bool(const CharInfo&)>& predicate) const;