Remove std::wstring usage in CJS_Field.

Use PDFium's WideString class consistently, instead of converting from
WideString to std::wstring and back.

Bug: pdfium:488
Change-Id: I17915d9f275bf620648cdec88d76c4beb31671f5
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/61550
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
diff --git a/fxjs/cjs_field.cpp b/fxjs/cjs_field.cpp
index 1d49924..37aa797 100644
--- a/fxjs/cjs_field.cpp
+++ b/fxjs/cjs_field.cpp
@@ -577,30 +577,33 @@
 CJS_Field::~CJS_Field() = default;
 
 // note: iControlNo = -1, means not a widget.
-void CJS_Field::ParseFieldName(const std::wstring& strFieldNameParsed,
-                               std::wstring& strFieldName,
+void CJS_Field::ParseFieldName(const WideString& strFieldNameParsed,
+                               WideString& strFieldName,
                                int& iControlNo) {
-  int iStart = strFieldNameParsed.find_last_of(L'.');
-  if (iStart == -1) {
+  auto reverse_it = strFieldNameParsed.rbegin();
+  while (reverse_it != strFieldNameParsed.rend()) {
+    if (*reverse_it == L'.')
+      break;
+    ++reverse_it;
+  }
+  if (reverse_it == strFieldNameParsed.rend()) {
     strFieldName = strFieldNameParsed;
     iControlNo = -1;
     return;
   }
-  std::wstring suffixal = strFieldNameParsed.substr(iStart + 1);
+  WideString suffixal =
+      strFieldNameParsed.Right(reverse_it - strFieldNameParsed.rbegin());
   iControlNo = FXSYS_wtoi(suffixal.c_str());
   if (iControlNo == 0) {
-    int iSpaceStart;
-    while ((iSpaceStart = suffixal.find_last_of(L" ")) != -1) {
-      suffixal.erase(iSpaceStart, 1);
-    }
-
-    if (suffixal.compare(L"0") != 0) {
+    suffixal.TrimRight(L' ');
+    if (suffixal != L"0") {
       strFieldName = strFieldNameParsed;
       iControlNo = -1;
       return;
     }
   }
-  strFieldName = strFieldNameParsed.substr(0, iStart);
+  strFieldName =
+      strFieldNameParsed.Left(strFieldNameParsed.rend() - reverse_it - 1);
 }
 
 bool CJS_Field::AttachField(CJS_Document* pDocument,
@@ -617,13 +620,13 @@
   swFieldNameTemp.Replace(L"..", L".");
 
   if (pForm->CountFields(swFieldNameTemp) <= 0) {
-    std::wstring strFieldName;
+    WideString strFieldName;
     int iControlNo = -1;
-    ParseFieldName(swFieldNameTemp.c_str(), strFieldName, iControlNo);
+    ParseFieldName(swFieldNameTemp, strFieldName, iControlNo);
     if (iControlNo == -1)
       return false;
 
-    m_FieldName = strFieldName.c_str();
+    m_FieldName = strFieldName;
     m_nFormControlIndex = iControlNo;
     return true;
   }
diff --git a/fxjs/cjs_field.h b/fxjs/cjs_field.h
index 823a6a0..fd40b1f 100644
--- a/fxjs/cjs_field.h
+++ b/fxjs/cjs_field.h
@@ -7,7 +7,6 @@
 #ifndef FXJS_CJS_FIELD_H_
 #define FXJS_CJS_FIELD_H_
 
-#include <string>
 #include <vector>
 
 #include "fxjs/cjs_document.h"
@@ -351,8 +350,8 @@
                                const std::vector<v8::Local<v8::Value>>& params);
 
   void SetDelay(bool bDelay);
-  void ParseFieldName(const std::wstring& strFieldNameParsed,
-                      std::wstring& strFieldName,
+  void ParseFieldName(const WideString& strFieldNameParsed,
+                      WideString& strFieldName,
                       int& iControlNo);
   std::vector<CPDF_FormField*> GetFormFields() const;
   CPDF_FormField* GetFirstFormField() const;