Use spans in CFX_WideTextBuf.

Change-Id: I0bf90c61513c5ba42e62d0fe0f569c89ca264fc5
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/67830
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 489e7d4..cc901fa 100644
--- a/core/fpdftext/cpdf_textpage.cpp
+++ b/core/fpdftext/cpdf_textpage.cpp
@@ -892,19 +892,22 @@
     m_pPrevTextObj = pPrevCharInfo->m_pTextObj;
 }
 
-void CPDF_TextPage::SwapTempTextBuf(int32_t iCharListStartAppend,
-                                    int32_t iBufStartAppend) {
-  int32_t i = iCharListStartAppend;
-  int32_t j = pdfium::CollectionSize<int32_t>(m_TempCharList) - 1;
+void CPDF_TextPage::SwapTempTextBuf(int iCharListStartAppend,
+                                    int iBufStartAppend) {
+  ASSERT(!m_TempCharList.empty());
+  int i = iCharListStartAppend;
+  int j = pdfium::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);
   }
-  wchar_t* pTempBuffer = m_TempTextBuf.GetBuffer();
+
+  pdfium::span<wchar_t> temp_span = m_TempTextBuf.GetWideSpan();
+  ASSERT(!temp_span.empty());
   i = iBufStartAppend;
-  j = m_TempTextBuf.GetLength() - 1;
+  j = pdfium::CollectionSize<int>(temp_span) - 1;
   for (; i < j; ++i, --j)
-    std::swap(pTempBuffer[i], pTempBuffer[j]);
+    std::swap(temp_span[i], temp_span[j]);
 }
 
 void CPDF_TextPage::ProcessTextObject(const TransformedTextObject& obj) {
diff --git a/core/fpdftext/cpdf_textpage.h b/core/fpdftext/cpdf_textpage.h
index 37cb095..20da8f5 100644
--- a/core/fpdftext/cpdf_textpage.h
+++ b/core/fpdftext/cpdf_textpage.h
@@ -129,7 +129,7 @@
       const CPDF_TextObject* pTextObj) const;
   TextOrientation FindTextlineFlowOrientation() const;
   void AppendGeneratedCharacter(wchar_t unicode, const CFX_Matrix& formMatrix);
-  void SwapTempTextBuf(int32_t iCharListStartAppend, int32_t iBufStartAppend);
+  void SwapTempTextBuf(int iCharListStartAppend, int iBufStartAppend);
   WideString GetTextByPredicate(
       const std::function<bool(const CharInfo&)>& predicate) const;
 
diff --git a/core/fxcrt/cfx_widetextbuf.cpp b/core/fxcrt/cfx_widetextbuf.cpp
index 8e91d68..44b649d 100644
--- a/core/fxcrt/cfx_widetextbuf.cpp
+++ b/core/fxcrt/cfx_widetextbuf.cpp
@@ -10,22 +10,22 @@
   return m_DataSize / sizeof(wchar_t);
 }
 
-wchar_t* CFX_WideTextBuf::GetBuffer() const {
-  return reinterpret_cast<wchar_t*>(m_pBuffer.get());
+pdfium::span<wchar_t> CFX_WideTextBuf::GetWideSpan() {
+  return pdfium::make_span(reinterpret_cast<wchar_t*>(m_pBuffer.get()),
+                           GetLength());
+}
+
+pdfium::span<const wchar_t> CFX_WideTextBuf::GetWideSpan() const {
+  return pdfium::make_span(reinterpret_cast<wchar_t*>(m_pBuffer.get()),
+                           GetLength());
 }
 
 WideStringView CFX_WideTextBuf::AsStringView() const {
-  return WideStringView(reinterpret_cast<const wchar_t*>(m_pBuffer.get()),
-                        m_DataSize / sizeof(wchar_t));
+  return WideStringView(GetWideSpan());
 }
 
 WideString CFX_WideTextBuf::MakeString() const {
-  return WideString(reinterpret_cast<const wchar_t*>(m_pBuffer.get()),
-                    m_DataSize / sizeof(wchar_t));
-}
-
-void CFX_WideTextBuf::Delete(int start_index, int count) {
-  CFX_BinaryBuf::Delete(start_index * sizeof(wchar_t), count * sizeof(wchar_t));
+  return WideString(AsStringView());
 }
 
 void CFX_WideTextBuf::AppendChar(wchar_t ch) {
@@ -34,6 +34,10 @@
   m_DataSize += sizeof(wchar_t);
 }
 
+void CFX_WideTextBuf::Delete(int start_index, int count) {
+  CFX_BinaryBuf::Delete(start_index * sizeof(wchar_t), count * sizeof(wchar_t));
+}
+
 CFX_WideTextBuf& CFX_WideTextBuf::operator<<(ByteStringView ascii) {
   ExpandBuf(ascii.GetLength() * sizeof(wchar_t));
   for (uint8_t ch : ascii) {
diff --git a/core/fxcrt/cfx_widetextbuf.h b/core/fxcrt/cfx_widetextbuf.h
index 6739d73..2232799 100644
--- a/core/fxcrt/cfx_widetextbuf.h
+++ b/core/fxcrt/cfx_widetextbuf.h
@@ -10,13 +10,15 @@
 #include "core/fxcrt/cfx_binarybuf.h"
 #include "core/fxcrt/fx_string.h"
 #include "core/fxcrt/fx_system.h"
+#include "third_party/base/span.h"
 
 class CFX_WideTextBuf final : public CFX_BinaryBuf {
  public:
   // CFX_BinaryBuf:
   size_t GetLength() const override;
 
-  wchar_t* GetBuffer() const;
+  pdfium::span<wchar_t> GetWideSpan();
+  pdfium::span<const wchar_t> GetWideSpan() const;
   WideStringView AsStringView() const;
   WideString MakeString() const;
 
diff --git a/core/fxcrt/cfx_widetextbuf_unittest.cpp b/core/fxcrt/cfx_widetextbuf_unittest.cpp
index ddca23f..c8160ee 100644
--- a/core/fxcrt/cfx_widetextbuf_unittest.cpp
+++ b/core/fxcrt/cfx_widetextbuf_unittest.cpp
@@ -10,7 +10,7 @@
 
 TEST(WideTextBuf, EmptyBuf) {
   CFX_WideTextBuf wtb;
-  EXPECT_EQ(nullptr, wtb.GetBuffer());
+  EXPECT_TRUE(wtb.GetWideSpan().empty());
   EXPECT_TRUE(wtb.AsStringView().IsEmpty());
   EXPECT_TRUE(wtb.MakeString().IsEmpty());
 }