Remove ByteString::AllocCopy() and WideString::AllocCopy().

Removing these skirts some unsafe buffer usage issues. Instead, we
can let the StringViews do all the work.

Change-Id: Ib516ebfbf03864679b511c2f137b1ecdf80e2c3d
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/116270
Reviewed-by: Lei Zhang <thestig@chromium.org>
Reviewed-by: Thomas Sepez <tsepez@google.com>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxcrt/bytestring.cpp b/core/fxcrt/bytestring.cpp
index 40b41b0..5155309 100644
--- a/core/fxcrt/bytestring.cpp
+++ b/core/fxcrt/bytestring.cpp
@@ -477,24 +477,13 @@
 }
 
 ByteString ByteString::Substr(size_t first, size_t count) const {
-  if (!m_pData)
+  if (!m_pData) {
     return ByteString();
-
-  if (!IsValidIndex(first))
-    return ByteString();
-
-  if (count == 0 || !IsValidLength(count))
-    return ByteString();
-
-  if (!IsValidIndex(first + count - 1))
-    return ByteString();
-
-  if (first == 0 && count == m_pData->m_nDataLength)
+  }
+  if (first == 0 && count == m_pData->m_nDataLength) {
     return *this;
-
-  ByteString dest;
-  AllocCopy(dest, count, first);
-  return dest;
+  }
+  return ByteString(AsStringView().Substr(first, count));
 }
 
 ByteString ByteString::First(size_t count) const {
@@ -506,17 +495,6 @@
   return Substr(GetLength() - count, count);
 }
 
-void ByteString::AllocCopy(ByteString& dest,
-                           size_t nCopyLen,
-                           size_t nCopyIndex) const {
-  if (nCopyLen == 0)
-    return;
-
-  RetainPtr<StringData> pNewData(
-      StringData::Create(m_pData->m_String + nCopyIndex, nCopyLen));
-  dest.m_pData.Swap(pNewData);
-}
-
 void ByteString::SetAt(size_t index, char c) {
   DCHECK(IsValidIndex(index));
   ReallocBeforeWrite(m_pData->m_nDataLength);
diff --git a/core/fxcrt/bytestring.h b/core/fxcrt/bytestring.h
index 6986674..9d37f4e 100644
--- a/core/fxcrt/bytestring.h
+++ b/core/fxcrt/bytestring.h
@@ -217,7 +217,6 @@
 
   void ReallocBeforeWrite(size_t nNewLen);
   void AllocBeforeWrite(size_t nNewLen);
-  void AllocCopy(ByteString& dest, size_t nCopyLen, size_t nCopyIndex) const;
   void AssignCopy(const char* pSrcData, size_t nSrcLen);
   void Concat(const char* pSrcData, size_t nSrcLen);
   intptr_t ReferenceCountForTesting() const;
diff --git a/core/fxcrt/widestring.cpp b/core/fxcrt/widestring.cpp
index 132393f..2c83fad 100644
--- a/core/fxcrt/widestring.cpp
+++ b/core/fxcrt/widestring.cpp
@@ -796,24 +796,13 @@
 }
 
 WideString WideString::Substr(size_t first, size_t count) const {
-  if (!m_pData)
+  if (!m_pData) {
     return WideString();
-
-  if (!IsValidIndex(first))
-    return WideString();
-
-  if (count == 0 || !IsValidLength(count))
-    return WideString();
-
-  if (!IsValidIndex(first + count - 1))
-    return WideString();
-
-  if (first == 0 && count == GetLength())
+  }
+  if (first == 0 && count == GetLength()) {
     return *this;
-
-  WideString dest;
-  AllocCopy(dest, count, first);
-  return dest;
+  }
+  return WideString(AsStringView().Substr(first, count));
 }
 
 WideString WideString::First(size_t count) const {
@@ -825,17 +814,6 @@
   return Substr(GetLength() - count, count);
 }
 
-void WideString::AllocCopy(WideString& dest,
-                           size_t nCopyLen,
-                           size_t nCopyIndex) const {
-  if (nCopyLen == 0)
-    return;
-
-  RetainPtr<StringData> pNewData(
-      StringData::Create(m_pData->m_String + nCopyIndex, nCopyLen));
-  dest.m_pData.Swap(pNewData);
-}
-
 size_t WideString::Insert(size_t index, wchar_t ch) {
   const size_t cur_length = GetLength();
   if (!IsValidLength(index))
diff --git a/core/fxcrt/widestring.h b/core/fxcrt/widestring.h
index 5ae355c..482eaed 100644
--- a/core/fxcrt/widestring.h
+++ b/core/fxcrt/widestring.h
@@ -238,7 +238,6 @@
 
   void ReallocBeforeWrite(size_t nNewLength);
   void AllocBeforeWrite(size_t nNewLength);
-  void AllocCopy(WideString& dest, size_t nCopyLen, size_t nCopyIndex) const;
   void AssignCopy(const wchar_t* pSrcData, size_t nSrcLen);
   void Concat(const wchar_t* pSrcData, size_t nSrcLen);
   intptr_t ReferenceCountForTesting() const;