Move {Byte,Wide}String::SetAt() into StringTemplate<>

Same with Reserve().

Change-Id: Iae05a175cdafa5fbe9bdd93586b35d0e5983ab34
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/116675
Reviewed-by: Thomas Sepez <tsepez@google.com>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxcrt/bytestring.cpp b/core/fxcrt/bytestring.cpp
index 9b8f539..bec3d05 100644
--- a/core/fxcrt/bytestring.cpp
+++ b/core/fxcrt/bytestring.cpp
@@ -294,10 +294,6 @@
   return true;
 }
 
-void ByteString::Reserve(size_t len) {
-  GetBuffer(len);
-}
-
 intptr_t ByteString::ReferenceCountForTesting() const {
   return m_pData ? m_pData->m_nRefs : 0;
 }
@@ -326,12 +322,6 @@
   return Substr(GetLength() - count, count);
 }
 
-void ByteString::SetAt(size_t index, char c) {
-  DCHECK(IsValidIndex(index));
-  ReallocBeforeWrite(m_pData->m_nDataLength);
-  m_pData->m_String[index] = c;
-}
-
 void ByteString::MakeLower() {
   if (IsEmpty())
     return;
diff --git a/core/fxcrt/bytestring.h b/core/fxcrt/bytestring.h
index 98d1019..62794c8 100644
--- a/core/fxcrt/bytestring.h
+++ b/core/fxcrt/bytestring.h
@@ -100,10 +100,6 @@
   ByteString& operator+=(const ByteString& str);
   ByteString& operator+=(ByteStringView str);
 
-  void SetAt(size_t index, char c);
-
-  void Reserve(size_t len);
-
   ByteString Substr(size_t offset) const;
   ByteString Substr(size_t first, size_t count) const;
   ByteString First(size_t count) const;
diff --git a/core/fxcrt/string_template.cpp b/core/fxcrt/string_template.cpp
index 9ec367b..cc3e1f9 100644
--- a/core/fxcrt/string_template.cpp
+++ b/core/fxcrt/string_template.cpp
@@ -139,6 +139,13 @@
 }
 
 template <typename T>
+void StringTemplate<T>::SetAt(size_t index, T ch) {
+  DCHECK(IsValidIndex(index));
+  ReallocBeforeWrite(m_pData->m_nDataLength);
+  m_pData->span()[index] = ch;
+}
+
+template <typename T>
 std::optional<size_t> StringTemplate<T>::Find(T ch, size_t start) const {
   return Find(StringView(ch), start);
 }
diff --git a/core/fxcrt/string_template.h b/core/fxcrt/string_template.h
index fd850ad..4bf0b80 100644
--- a/core/fxcrt/string_template.h
+++ b/core/fxcrt/string_template.h
@@ -126,6 +126,11 @@
     return Find(ch, start).has_value();
   }
 
+  // Overwrite character at `index`.
+  void SetAt(size_t index, T ch);
+
+  void Reserve(size_t len) { GetBuffer(len); }
+
  protected:
   using StringData = StringDataTemplate<T>;
 
diff --git a/core/fxcrt/widestring.cpp b/core/fxcrt/widestring.cpp
index 6f14a26..bf73577 100644
--- a/core/fxcrt/widestring.cpp
+++ b/core/fxcrt/widestring.cpp
@@ -549,10 +549,6 @@
   return Compare(other) < 0;
 }
 
-void WideString::Reserve(size_t len) {
-  GetBuffer(len);
-}
-
 intptr_t WideString::ReferenceCountForTesting() const {
   return m_pData ? m_pData->m_nRefs : 0;
 }
@@ -793,12 +789,6 @@
   return result;
 }
 
-void WideString::SetAt(size_t index, wchar_t c) {
-  DCHECK(IsValidIndex(index));
-  ReallocBeforeWrite(m_pData->m_nDataLength);
-  m_pData->m_String[index] = c;
-}
-
 int WideString::Compare(const wchar_t* str) const {
   if (m_pData)
     return str ? wcscmp(m_pData->m_String, str) : 1;
diff --git a/core/fxcrt/widestring.h b/core/fxcrt/widestring.h
index 479e1be..a52c301 100644
--- a/core/fxcrt/widestring.h
+++ b/core/fxcrt/widestring.h
@@ -105,8 +105,6 @@
   bool operator<(WideStringView str) const;
   bool operator<(const WideString& other) const;
 
-  void SetAt(size_t index, wchar_t c);
-
   int Compare(const wchar_t* str) const;
   int Compare(const WideString& str) const;
   int CompareNoCase(const wchar_t* str) const;
@@ -131,8 +129,6 @@
   void TrimRight(wchar_t target);
   void TrimRight(WideStringView targets);
 
-  void Reserve(size_t len);
-
   int GetInteger() const;
 
   size_t Replace(WideStringView pOld, WideStringView pNew);