Simplify FX_UTF8Encode variants.

Doing so reveals places where there are needless copies
and conversions.

Change-Id: I24a868d40aa63836f4167eaf4541964049df7916
Reviewed-on: https://pdfium-review.googlesource.com/2555
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: dsinclair <dsinclair@chromium.org>
diff --git a/core/fxcrt/fx_basic_utf.cpp b/core/fxcrt/fx_basic_utf.cpp
index 8dbbb28..c0f14c8 100644
--- a/core/fxcrt/fx_basic_utf.cpp
+++ b/core/fxcrt/fx_basic_utf.cpp
@@ -73,13 +73,13 @@
     }
   }
 }
-CFX_ByteString FX_UTF8Encode(const FX_WCHAR* pwsStr, FX_STRSIZE len) {
-  if (len < 0)
-    len = FXSYS_wcslen(pwsStr);
 
+CFX_ByteString FX_UTF8Encode(const CFX_WideStringC& wsStr) {
+  FX_STRSIZE len = wsStr.GetLength();
+  const FX_WCHAR* pStr = wsStr.c_str();
   CFX_UTF8Encoder encoder;
   while (len-- > 0)
-    encoder.Input(*pwsStr++);
+    encoder.Input(*pStr++);
 
   return CFX_ByteString(encoder.GetResult());
 }
diff --git a/core/fxcrt/fx_basic_wstring.cpp b/core/fxcrt/fx_basic_wstring.cpp
index e779621..93b9ba7 100644
--- a/core/fxcrt/fx_basic_wstring.cpp
+++ b/core/fxcrt/fx_basic_wstring.cpp
@@ -343,12 +343,10 @@
   m_pData.Swap(pNewData);
 }
 
-// static
 CFX_ByteString CFX_WideString::UTF8Encode() const {
-  return FX_UTF8Encode(*this);
+  return FX_UTF8Encode(AsStringC());
 }
 
-// static
 CFX_ByteString CFX_WideString::UTF16LE_Encode() const {
   if (!m_pData) {
     return CFX_ByteString("\0\0", 2);
diff --git a/core/fxcrt/fx_string.h b/core/fxcrt/fx_string.h
index 750216f..cd93f27 100644
--- a/core/fxcrt/fx_string.h
+++ b/core/fxcrt/fx_string.h
@@ -424,17 +424,10 @@
   return rhs != lhs;
 }
 
-CFX_ByteString FX_UTF8Encode(const FX_WCHAR* pwsStr, FX_STRSIZE len);
-inline CFX_ByteString FX_UTF8Encode(const CFX_WideStringC& wsStr) {
-  return FX_UTF8Encode(wsStr.c_str(), wsStr.GetLength());
-}
-inline CFX_ByteString FX_UTF8Encode(const CFX_WideString& wsStr) {
-  return FX_UTF8Encode(wsStr.c_str(), wsStr.GetLength());
-}
-
+CFX_ByteString FX_UTF8Encode(const CFX_WideStringC& wsStr);
 FX_FLOAT FX_atof(const CFX_ByteStringC& str);
 inline FX_FLOAT FX_atof(const CFX_WideStringC& wsStr) {
-  return FX_atof(FX_UTF8Encode(wsStr.c_str(), wsStr.GetLength()).c_str());
+  return FX_atof(FX_UTF8Encode(wsStr).c_str());
 }
 bool FX_atonum(const CFX_ByteStringC& str, void* pData);
 FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_CHAR* buf);
diff --git a/core/fxcrt/fx_xml_parser.cpp b/core/fxcrt/fx_xml_parser.cpp
index 1b562c2..7658620 100644
--- a/core/fxcrt/fx_xml_parser.cpp
+++ b/core/fxcrt/fx_xml_parser.cpp
@@ -773,7 +773,7 @@
     }
     pElement = pElement->GetParent();
   } while (pElement);
-  return pwsSpace ? FX_UTF8Encode(*pwsSpace) : CFX_ByteString();
+  return pwsSpace ? pwsSpace->UTF8Encode() : CFX_ByteString();
 }
 void CXML_Element::GetAttrByIndex(int index,
                                   CFX_ByteString& space,
diff --git a/fxjs/fxjs_v8.cpp b/fxjs/fxjs_v8.cpp
index c96cc1f..667d63b 100644
--- a/fxjs/fxjs_v8.cpp
+++ b/fxjs/fxjs_v8.cpp
@@ -274,7 +274,7 @@
                                    v8::FunctionCallback pMethodCall) {
   v8::Isolate::Scope isolate_scope(m_isolate);
   v8::HandleScope handle_scope(m_isolate);
-  CFX_ByteString bsMethodName = CFX_WideString(sMethodName).UTF8Encode();
+  CFX_ByteString bsMethodName = FX_UTF8Encode(CFX_WideStringC(sMethodName));
   CFXJS_ObjDefinition* pObjDef =
       CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID);
   v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(
@@ -293,7 +293,7 @@
                                      v8::AccessorSetterCallback pPropPut) {
   v8::Isolate::Scope isolate_scope(m_isolate);
   v8::HandleScope handle_scope(m_isolate);
-  CFX_ByteString bsPropertyName = CFX_WideString(sPropName).UTF8Encode();
+  CFX_ByteString bsPropertyName = FX_UTF8Encode(CFX_WideStringC(sPropName));
   CFXJS_ObjDefinition* pObjDef =
       CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID);
   pObjDef->GetInstanceTemplate()->SetAccessor(
@@ -322,7 +322,7 @@
                                   v8::Local<v8::Value> pDefault) {
   v8::Isolate::Scope isolate_scope(m_isolate);
   v8::HandleScope handle_scope(m_isolate);
-  CFX_ByteString bsConstName = CFX_WideString(sConstName).UTF8Encode();
+  CFX_ByteString bsConstName = FX_UTF8Encode(CFX_WideStringC(sConstName));
   CFXJS_ObjDefinition* pObjDef =
       CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID);
   pObjDef->GetInstanceTemplate()->Set(m_isolate, bsConstName.c_str(), pDefault);
@@ -332,7 +332,7 @@
                                       v8::FunctionCallback pMethodCall) {
   v8::Isolate::Scope isolate_scope(m_isolate);
   v8::HandleScope handle_scope(m_isolate);
-  CFX_ByteString bsMethodName = CFX_WideString(sMethodName).UTF8Encode();
+  CFX_ByteString bsMethodName = FX_UTF8Encode(CFX_WideStringC(sMethodName));
   v8::Local<v8::FunctionTemplate> fun =
       v8::FunctionTemplate::New(m_isolate, pMethodCall);
   fun->RemovePrototype();
@@ -347,7 +347,7 @@
                                      v8::FunctionCallback pConstGetter) {
   v8::Isolate::Scope isolate_scope(m_isolate);
   v8::HandleScope handle_scope(m_isolate);
-  CFX_ByteString bsConst = CFX_WideString(sConstName).UTF8Encode();
+  CFX_ByteString bsConst = FX_UTF8Encode(CFX_WideStringC(sConstName));
   v8::Local<v8::FunctionTemplate> fun =
       v8::FunctionTemplate::New(m_isolate, pConstGetter);
   fun->RemovePrototype();
@@ -392,7 +392,7 @@
                                           ->ToObject(v8Context)
                                           .ToLocalChecked());
     } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) {
-      CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode();
+      CFX_ByteString bs = FX_UTF8Encode(CFX_WideStringC(pObjDef->m_ObjName));
       v8::Local<v8::String> m_ObjName =
           v8::String::NewFromUtf8(m_isolate, bs.c_str(),
                                   v8::NewStringType::kNormal, bs.GetLength())
diff --git a/xfa/fgas/font/cfgas_fontmgr.cpp b/xfa/fgas/font/cfgas_fontmgr.cpp
index add6689..bf243a4 100644
--- a/xfa/fgas/font/cfgas_fontmgr.cpp
+++ b/xfa/fgas/font/cfgas_fontmgr.cpp
@@ -629,7 +629,7 @@
     const FX_WCHAR* pszFontFamily) {
   CFX_ByteString bsHash;
   bsHash.Format("%d, %d", wCodePage, dwFontStyles);
-  bsHash += CFX_WideString(pszFontFamily).UTF8Encode();
+  bsHash += FX_UTF8Encode(CFX_WideStringC(pszFontFamily));
   uint32_t dwHash = FX_HashCode_GetA(bsHash.AsStringC(), false);
   std::vector<CFX_RetainPtr<CFGAS_GEFont>>* pFontArray = &m_Hash2Fonts[dwHash];
   if (!pFontArray->empty())
@@ -673,7 +673,7 @@
     bsHash.Format("%d, %d, %d", wCodePage, wBitField, dwFontStyles);
   else
     bsHash.Format("%d, %d", wCodePage, dwFontStyles);
-  bsHash += CFX_WideString(pszFontFamily).UTF8Encode();
+  bsHash += FX_UTF8Encode(CFX_WideStringC(pszFontFamily));
   uint32_t dwHash = FX_HashCode_GetA(bsHash.AsStringC(), false);
   std::vector<CFX_RetainPtr<CFGAS_GEFont>>* pFonts = &m_Hash2Fonts[dwHash];
   for (size_t i = 0; i < pFonts->size(); ++i) {
diff --git a/xfa/fxfa/fm2js/xfa_fm2jscontext.cpp b/xfa/fxfa/fm2js/xfa_fm2jscontext.cpp
index ba65bef..e8cb2d0 100644
--- a/xfa/fxfa/fm2js/xfa_fm2jscontext.cpp
+++ b/xfa/fxfa/fm2js/xfa_fm2jscontext.cpp
@@ -2026,7 +2026,7 @@
   CFX_WideString wsRet;
   widgetValue.FormatPatterns(wsRet, wsFormat, pLocale,
                              XFA_VALUEPICTURE_Display);
-  strLocalDate = FX_UTF8Encode(wsRet.c_str(), wsRet.GetLength());
+  strLocalDate = wsRet.UTF8Encode();
   return true;
 }
 
@@ -2065,7 +2065,7 @@
   CFX_WideString wsRet;
   widgetValue.FormatPatterns(wsRet, wsFormat, pLocale,
                              XFA_VALUEPICTURE_Display);
-  strLocalTime = FX_UTF8Encode(wsRet.c_str(), wsRet.GetLength());
+  strLocalTime = wsRet.UTF8Encode();
   return true;
 }
 
@@ -2104,7 +2104,7 @@
   CFX_WideString wsRet;
   widgetValue.FormatPatterns(wsRet, wsFormat, pLocale,
                              XFA_VALUEPICTURE_Display);
-  strGMTTime = FX_UTF8Encode(wsRet.c_str(), wsRet.GetLength());
+  strGMTTime = wsRet.UTF8Encode();
   return true;
 }
 
@@ -2213,7 +2213,7 @@
     pLocale->GetDateTimeSymbols(wsSymbols);
     AlternateDateTimeSymbols(strRet, wsSymbols, g_sAltTable_Date);
   }
-  strFormat = FX_UTF8Encode(strRet.c_str(), strRet.GetLength());
+  strFormat = strRet.UTF8Encode();
 }
 
 // static
@@ -2264,7 +2264,7 @@
     pLocale->GetDateTimeSymbols(wsSymbols);
     AlternateDateTimeSymbols(strRet, wsSymbols, g_sAltTable_Time);
   }
-  strFormat = FX_UTF8Encode(strRet.c_str(), strRet.GetLength());
+  strFormat = strRet.UTF8Encode();
 }
 
 // static
@@ -2959,10 +2959,8 @@
       CFXJSE_Context::Create(pIsolate, nullptr, nullptr));
 
   auto returnValue = pdfium::MakeUnique<CFXJSE_Value>(pIsolate);
-  CFX_WideString javaScript(wsJavaScriptBuf.AsStringC());
-  pNewContext->ExecuteScript(
-      FX_UTF8Encode(javaScript.c_str(), javaScript.GetLength()).c_str(),
-      returnValue.get());
+  pNewContext->ExecuteScript(FX_UTF8Encode(wsJavaScriptBuf.AsStringC()).c_str(),
+                             returnValue.get());
 
   args.GetReturnValue()->Assign(returnValue.get());
 }
@@ -3428,11 +3426,8 @@
     ++i;
   }
   wsResultBuf.AppendChar(0);
-
   szResultString.Clear();
-  szResultString << FX_UTF8Encode(wsResultBuf.GetBuffer(),
-                                  wsResultBuf.GetLength())
-                        .AsStringC();
+  szResultString << FX_UTF8Encode(wsResultBuf.AsStringC());
 }
 
 // static
@@ -3508,9 +3503,7 @@
   wsResultBuf.AppendChar(0);
 
   szResultString.Clear();
-  szResultString << FX_UTF8Encode(wsResultBuf.GetBuffer(),
-                                  wsResultBuf.GetLength())
-                        .AsStringC();
+  szResultString << FX_UTF8Encode(wsResultBuf.AsStringC());
 }
 
 // static
@@ -3610,10 +3603,8 @@
     iCode = 0;
   }
   wsXMLBuf.AppendChar(0);
-
   szResultString.Clear();
-  szResultString << FX_UTF8Encode(wsXMLBuf.GetBuffer(), wsXMLBuf.GetLength())
-                        .AsStringC();
+  szResultString << FX_UTF8Encode(wsXMLBuf.AsStringC());
 }
 
 // static
@@ -3761,9 +3752,7 @@
   }
   wsResultBuf.AppendChar(0);
   szResultBuf.Clear();
-
-  szResultBuf << FX_UTF8Encode(wsResultBuf.GetBuffer(), wsResultBuf.GetLength())
-                     .AsStringC();
+  szResultBuf << FX_UTF8Encode(wsResultBuf.AsStringC());
 }
 
 // static
@@ -3813,9 +3802,7 @@
   }
   wsResultBuf.AppendChar(0);
   szResultBuf.Clear();
-
-  szResultBuf << FX_UTF8Encode(wsResultBuf.GetBuffer(), wsResultBuf.GetLength())
-                     .AsStringC();
+  szResultBuf << FX_UTF8Encode(wsResultBuf.AsStringC());
 }
 
 // static
@@ -3886,9 +3873,7 @@
   }
   wsResultBuf.AppendChar(0);
   szResultBuf.Clear();
-
-  szResultBuf << FX_UTF8Encode(wsResultBuf.GetBuffer(), wsResultBuf.GetLength())
-                     .AsStringC();
+  szResultBuf << FX_UTF8Encode(wsResultBuf.AsStringC());
 }
 
 // static
@@ -4009,8 +3994,7 @@
     return;
   }
 
-  args.GetReturnValue()->SetString(
-      FX_UTF8Encode(wsRet.c_str(), wsRet.GetLength()).AsStringC());
+  args.GetReturnValue()->SetString(wsRet.UTF8Encode().AsStringC());
 }
 
 // static
@@ -4091,8 +4075,7 @@
   lowStringBuf.AppendChar(0);
 
   args.GetReturnValue()->SetString(
-      FX_UTF8Encode(lowStringBuf.GetBuffer(), lowStringBuf.GetLength())
-          .AsStringC());
+      FX_UTF8Encode(lowStringBuf.AsStringC()).AsStringC());
 }
 
 // static
@@ -4156,7 +4139,7 @@
       return;
     }
     args.GetReturnValue()->SetString(
-        FX_UTF8Encode(localeValue.GetValue()).AsStringC());
+        localeValue.GetValue().UTF8Encode().AsStringC());
     return;
   }
 
@@ -4173,7 +4156,7 @@
         return;
       }
       args.GetReturnValue()->SetString(
-          FX_UTF8Encode(localeValue.GetValue()).AsStringC());
+          localeValue.GetValue().UTF8Encode().AsStringC());
       return;
     }
     case XFA_VT_DATE: {
@@ -4185,7 +4168,7 @@
         return;
       }
       args.GetReturnValue()->SetString(
-          FX_UTF8Encode(localeValue.GetValue()).AsStringC());
+          localeValue.GetValue().UTF8Encode().AsStringC());
       return;
     }
     case XFA_VT_TIME: {
@@ -4197,7 +4180,7 @@
         return;
       }
       args.GetReturnValue()->SetString(
-          FX_UTF8Encode(localeValue.GetValue()).AsStringC());
+          localeValue.GetValue().UTF8Encode().AsStringC());
       return;
     }
     case XFA_VT_TEXT: {
@@ -4209,7 +4192,7 @@
         return;
       }
       args.GetReturnValue()->SetString(
-          FX_UTF8Encode(localeValue.GetValue()).AsStringC());
+          localeValue.GetValue().UTF8Encode().AsStringC());
       return;
     }
     case XFA_VT_FLOAT: {
@@ -4241,7 +4224,7 @@
         return;
       }
       args.GetReturnValue()->SetString(
-          FX_UTF8Encode(localeValue2.GetValue()).AsStringC());
+          localeValue2.GetValue().UTF8Encode().AsStringC());
       return;
     }
   }
@@ -4640,8 +4623,7 @@
   upperStringBuf.AppendChar(0);
 
   args.GetReturnValue()->SetString(
-      FX_UTF8Encode(upperStringBuf.GetBuffer(), upperStringBuf.GetLength())
-          .AsStringC());
+      FX_UTF8Encode(upperStringBuf.AsStringC()).AsStringC());
 }
 
 // static
@@ -4931,10 +4913,7 @@
     pContext->ThrowServerDeniedException();
     return;
   }
-
-  args.GetReturnValue()->SetString(
-      FX_UTF8Encode(decodedResponse.c_str(), decodedResponse.GetLength())
-          .AsStringC());
+  args.GetReturnValue()->SetString(decodedResponse.UTF8Encode().AsStringC());
 }
 
 // static
@@ -5680,9 +5659,8 @@
     return;
   }
 
-  CFX_WideString javaScript = wsJavaScriptBuf.MakeString();
   args.GetReturnValue()->SetString(
-      FX_UTF8Encode(javaScript.c_str(), javaScript.GetLength()).AsStringC());
+      FX_UTF8Encode(wsJavaScriptBuf.AsStringC()).AsStringC());
 }
 
 // static
@@ -6439,6 +6417,5 @@
   va_start(arg_ptr, str);
   wsMessage.FormatV(str, arg_ptr);
   va_end(arg_ptr);
-  FXJSE_ThrowMessage(
-      FX_UTF8Encode(wsMessage.c_str(), wsMessage.GetLength()).AsStringC());
+  FXJSE_ThrowMessage(wsMessage.UTF8Encode().AsStringC());
 }
diff --git a/xfa/fxfa/parser/cscript_eventpseudomodel.cpp b/xfa/fxfa/parser/cscript_eventpseudomodel.cpp
index 8a7d80b..8cfedd2 100644
--- a/xfa/fxfa/parser/cscript_eventpseudomodel.cpp
+++ b/xfa/fxfa/parser/cscript_eventpseudomodel.cpp
@@ -25,7 +25,7 @@
     wsValue = pValue->ToWideString();
     return;
   }
-  pValue->SetString(FX_UTF8Encode(wsValue).AsStringC());
+  pValue->SetString(wsValue.UTF8Encode().AsStringC());
 }
 
 void InterProperty(CFXJSE_Value* pValue, int32_t& iValue, bool bSetting) {
diff --git a/xfa/fxfa/parser/cscript_hostpseudomodel.cpp b/xfa/fxfa/parser/cscript_hostpseudomodel.cpp
index eb8177e..a06e02f 100644
--- a/xfa/fxfa/parser/cscript_hostpseudomodel.cpp
+++ b/xfa/fxfa/parser/cscript_hostpseudomodel.cpp
@@ -89,7 +89,7 @@
     return;
   }
   pValue->SetString(
-      FX_UTF8Encode(pNotify->GetAppProvider()->GetLanguage()).AsStringC());
+      pNotify->GetAppProvider()->GetLanguage().UTF8Encode().AsStringC());
 }
 
 void CScript_HostPseudoModel::NumPages(CFXJSE_Value* pValue,
@@ -118,8 +118,9 @@
     return;
   }
   pValue->SetString(
-      FX_UTF8Encode(pNotify->GetAppProvider()->GetPlatform()).AsStringC());
+      pNotify->GetAppProvider()->GetPlatform().UTF8Encode().AsStringC());
 }
+
 void CScript_HostPseudoModel::Title(CFXJSE_Value* pValue,
                                     bool bSetting,
                                     XFA_ATTRIBUTE eAttribute) {
@@ -137,7 +138,7 @@
   }
   CFX_WideString wsTitle;
   pNotify->GetDocEnvironment()->GetTitle(hDoc, wsTitle);
-  pValue->SetString(FX_UTF8Encode(wsTitle).AsStringC());
+  pValue->SetString(wsTitle.UTF8Encode().AsStringC());
 }
 
 void CScript_HostPseudoModel::ValidationsEnabled(CFXJSE_Value* pValue,
@@ -199,7 +200,7 @@
     return;
   }
   pValue->SetString(
-      FX_UTF8Encode(pNotify->GetAppProvider()->GetAppName()).AsStringC());
+      pNotify->GetAppProvider()->GetAppName().UTF8Encode().AsStringC());
 }
 
 void CScript_HostPseudoModel::GotoURL(CFXJSE_Arguments* pArguments) {
@@ -306,7 +307,7 @@
       wsQuestion, wsTitle, wsDefaultAnswer, bMark);
   CFXJSE_Value* pValue = pArguments->GetReturnValue();
   if (pValue)
-    pValue->SetString(FX_UTF8Encode(wsAnswer).AsStringC());
+    pValue->SetString(wsAnswer.UTF8Encode().AsStringC());
 }
 
 void CScript_HostPseudoModel::DocumentInBatch(CFXJSE_Arguments* pArguments) {
@@ -674,7 +675,7 @@
   CFX_WideString wsDataTime = pNotify->GetCurrentDateTime();
   CFXJSE_Value* pValue = pArguments->GetReturnValue();
   if (pValue)
-    pValue->SetString(FX_UTF8Encode(wsDataTime).AsStringC());
+    pValue->SetString(wsDataTime.UTF8Encode().AsStringC());
 }
 
 void CScript_HostPseudoModel::ThrowSetLanguageException() const {
diff --git a/xfa/fxfa/parser/cxfa_node.cpp b/xfa/fxfa/parser/cxfa_node.cpp
index 0fc114b..1e50896 100644
--- a/xfa/fxfa/parser/cxfa_node.cpp
+++ b/xfa/fxfa/parser/cxfa_node.cpp
@@ -1109,8 +1109,7 @@
     return;
   if (bSetting) {
     CFX_WideString wsMessage = L"Unable to set ";
-    FXJSE_ThrowMessage(
-        FX_UTF8Encode(wsMessage.c_str(), wsMessage.GetLength()).AsStringC());
+    FXJSE_ThrowMessage(wsMessage.UTF8Encode().AsStringC());
   } else {
     CXFA_AttachNodeList* pNodeList = new CXFA_AttachNodeList(m_pDocument, this);
     pValue->SetObject(pNodeList, pScriptContext->GetJseNormalClass());
@@ -1124,7 +1123,6 @@
     ThrowInvalidPropertyException();
     return;
   }
-
   uint32_t dwFlag = XFA_RESOLVENODE_Siblings | XFA_RESOLVENODE_ALL;
   CFX_WideString wsExpression = L"#" + GetClassName() + L"[*]";
   Script_Som_ResolveNodeList(pValue, wsExpression, dwFlag);
@@ -1174,7 +1172,7 @@
   }
   CFX_WideString wsSOMExpression;
   GetSOMExpression(wsSOMExpression);
-  pValue->SetString(FX_UTF8Encode(wsSOMExpression).AsStringC());
+  pValue->SetString(wsSOMExpression.UTF8Encode().AsStringC());
 }
 
 void CXFA_Node::Script_NodeClass_ApplyXSL(CFXJSE_Arguments* pArguments) {
@@ -1238,7 +1236,7 @@
   GetAttribute(wsExpression.AsStringC(), wsValue);
   CFXJSE_Value* pValue = pArguments->GetReturnValue();
   if (pValue)
-    pValue->SetString(FX_UTF8Encode(wsValue).AsStringC());
+    pValue->SetString(wsValue.UTF8Encode().AsStringC());
 }
 
 void CXFA_Node::Script_NodeClass_GetElement(CFXJSE_Arguments* pArguments) {
@@ -1508,7 +1506,7 @@
 
   CFX_WideString wsNameSpace;
   TryNamespace(wsNameSpace);
-  pValue->SetString(FX_UTF8Encode(wsNameSpace).AsStringC());
+  pValue->SetString(wsNameSpace.UTF8Encode().AsStringC());
 }
 
 void CXFA_Node::Script_NodeClass_Model(CFXJSE_Value* pValue,
@@ -1858,8 +1856,7 @@
   } else {
     CFX_WideString wsValue;
     GetAttribute(eAttribute, wsValue);
-    pValue->SetString(
-        FX_UTF8Encode(wsValue.c_str(), wsValue.GetLength()).AsStringC());
+    pValue->SetString(wsValue.UTF8Encode().AsStringC());
   }
 }
 
@@ -1873,8 +1870,7 @@
 
   CFX_WideString wsValue;
   GetAttribute(eAttribute, wsValue);
-  pValue->SetString(
-      FX_UTF8Encode(wsValue.c_str(), wsValue.GetLength()).AsStringC());
+  pValue->SetString(wsValue.UTF8Encode().AsStringC());
 }
 
 void CXFA_Node::Script_WsdlConnection_Execute(CFXJSE_Arguments* pArguments) {
@@ -1952,7 +1948,7 @@
       default:
         break;
     }
-    pValue->SetString(FX_UTF8Encode(wsMessage).AsStringC());
+    pValue->SetString(wsMessage.UTF8Encode().AsStringC());
   }
 }
 
@@ -2038,8 +2034,7 @@
       CFX_Decimal decimal(content.AsStringC());
       pValue->SetFloat((FX_FLOAT)(double)decimal);
     } else {
-      pValue->SetString(
-          FX_UTF8Encode(content.c_str(), content.GetLength()).AsStringC());
+      pValue->SetString(content.UTF8Encode().AsStringC());
     }
   }
 }
@@ -2057,8 +2052,7 @@
     pValue->SetNull();
     return;
   }
-  pValue->SetString(
-      FX_UTF8Encode(content.c_str(), content.GetLength()).AsStringC());
+  pValue->SetString(content.UTF8Encode().AsStringC());
 }
 
 void CXFA_Node::Script_Boolean_Value(CFXJSE_Value* pValue,
@@ -2109,7 +2103,7 @@
     ArgbDecode(color, a, r, g, b);
     CFX_WideString strColor;
     strColor.Format(L"%d,%d,%d", r, g, b);
-    pValue->SetString(FX_UTF8Encode(strColor).AsStringC());
+    pValue->SetString(strColor.UTF8Encode().AsStringC());
   }
 }
 
@@ -2134,7 +2128,7 @@
     CXFA_Edge edge = border.GetEdge(0);
     CXFA_Measurement thickness = edge.GetMSThickness();
     thickness.ToString(wsThickness);
-    pValue->SetString(FX_UTF8Encode(wsThickness).AsStringC());
+    pValue->SetString(wsThickness.UTF8Encode().AsStringC());
   }
 }
 
@@ -2167,7 +2161,7 @@
     ArgbDecode(color, a, r, g, b);
     CFX_WideString wsColor;
     wsColor.Format(L"%d,%d,%d", r, g, b);
-    pValue->SetString(FX_UTF8Encode(wsColor).AsStringC());
+    pValue->SetString(wsColor.UTF8Encode().AsStringC());
   }
 }
 
@@ -2204,12 +2198,10 @@
     }
   } else {
     CFX_WideString content = GetScriptContent(true);
-    if (content.IsEmpty()) {
+    if (content.IsEmpty())
       pValue->SetNull();
-    } else {
-      pValue->SetString(
-          FX_UTF8Encode(content.c_str(), content.GetLength()).AsStringC());
-    }
+    else
+      pValue->SetString(content.UTF8Encode().AsStringC());
   }
 }
 
@@ -2258,8 +2250,7 @@
       if (pNode && pNode->GetElementType() == XFA_Element::Decimal) {
         if (pUIChild->GetElementType() == XFA_Element::NumericEdit &&
             (pNode->GetInteger(XFA_ATTRIBUTE_FracDigits) == -1)) {
-          pValue->SetString(
-              FX_UTF8Encode(content.c_str(), content.GetLength()).AsStringC());
+          pValue->SetString(content.UTF8Encode().AsStringC());
         } else {
           CFX_Decimal decimal(content.AsStringC());
           pValue->SetFloat((FX_FLOAT)(double)decimal);
@@ -2272,8 +2263,7 @@
         CFX_Decimal decimal(content.AsStringC());
         pValue->SetFloat((FX_FLOAT)(double)decimal);
       } else {
-        pValue->SetString(
-            FX_UTF8Encode(content.c_str(), content.GetLength()).AsStringC());
+        pValue->SetString(content.UTF8Encode().AsStringC());
       }
     }
   }
@@ -2291,7 +2281,7 @@
   } else {
     CFX_WideString wsValue;
     pWidgetData->GetValue(wsValue, XFA_VALUEPICTURE_Edit);
-    pValue->SetString(FX_UTF8Encode(wsValue).AsStringC());
+    pValue->SetString(wsValue.UTF8Encode().AsStringC());
   }
 }
 
@@ -2323,7 +2313,7 @@
     ArgbDecode(color, a, r, g, b);
     CFX_WideString wsColor;
     wsColor.Format(L"%d,%d,%d", r, g, b);
-    pValue->SetString(FX_UTF8Encode(wsColor).AsStringC());
+    pValue->SetString(wsColor.UTF8Encode().AsStringC());
   }
 }
 
@@ -2345,7 +2335,7 @@
   } else {
     CFX_WideString wsValue;
     pWidgetData->GetValue(wsValue, XFA_VALUEPICTURE_Display);
-    pValue->SetString(FX_UTF8Encode(wsValue).AsStringC());
+    pValue->SetString(wsValue.UTF8Encode().AsStringC());
   }
 }
 
@@ -2366,7 +2356,7 @@
     CFX_WideString wsValue;
     if (pInfo)
       wsValue = pInfo->pName;
-    pValue->SetString(FX_UTF8Encode(wsValue).AsStringC());
+    pValue->SetString(wsValue.UTF8Encode().AsStringC());
   }
 }
 
@@ -2477,13 +2467,11 @@
     return;
   }
   CFX_WideString wsValue;
-  bool bHasItem = pWidgetData->GetChoiceListItem(wsValue, iIndex, true);
-  if (bHasItem) {
-    pArguments->GetReturnValue()->SetString(
-        FX_UTF8Encode(wsValue.c_str(), wsValue.GetLength()).AsStringC());
-  } else {
+  if (!pWidgetData->GetChoiceListItem(wsValue, iIndex, true)) {
     pArguments->GetReturnValue()->SetNull();
+    return;
   }
+  pArguments->GetReturnValue()->SetString(wsValue.UTF8Encode().AsStringC());
 }
 
 void CXFA_Node::Script_Field_BoundItem(CFXJSE_Arguments* pArguments) {
@@ -2502,7 +2490,7 @@
   pWidgetData->GetItemValue(wsValue.AsStringC(), wsBoundValue);
   CFXJSE_Value* pValue = pArguments->GetReturnValue();
   if (pValue)
-    pValue->SetString(FX_UTF8Encode(wsBoundValue).AsStringC());
+    pValue->SetString(wsBoundValue.UTF8Encode().AsStringC());
 }
 
 void CXFA_Node::Script_Field_GetItemState(CFXJSE_Arguments* pArguments) {
@@ -2554,13 +2542,11 @@
     return;
   }
   CFX_WideString wsValue;
-  bool bHasItem = pWidgetData->GetChoiceListItem(wsValue, iIndex, false);
-  if (bHasItem) {
-    pArguments->GetReturnValue()->SetString(
-        FX_UTF8Encode(wsValue.c_str(), wsValue.GetLength()).AsStringC());
-  } else {
+  if (!pWidgetData->GetChoiceListItem(wsValue, iIndex, false)) {
     pArguments->GetReturnValue()->SetNull();
+    return;
   }
+  pArguments->GetReturnValue()->SetString(wsValue.UTF8Encode().AsStringC());
 }
 
 void CXFA_Node::Script_Field_SetItemState(CFXJSE_Arguments* pArguments) {
@@ -2646,7 +2632,7 @@
     if (wsValue.IsEmpty() && curVersion >= XFA_VERSION_300) {
       pValue->SetNull();
     } else {
-      pValue->SetString(FX_UTF8Encode(wsValue).AsStringC());
+      pValue->SetString(wsValue.UTF8Encode().AsStringC());
     }
   }
 }
@@ -2814,9 +2800,7 @@
   } else {
     CFX_WideString wsLocaleName;
     GetLocaleName(wsLocaleName);
-    pValue->SetString(
-        FX_UTF8Encode(wsLocaleName.c_str(), wsLocaleName.GetLength())
-            .AsStringC());
+    pValue->SetString(wsLocaleName.UTF8Encode().AsStringC());
   }
 }
 
@@ -3415,11 +3399,9 @@
     SetAttribute(XFA_ATTRIBUTE_Checksum, pValue->ToWideString().AsStringC());
     return;
   }
-
   CFX_WideString wsChecksum;
   GetAttribute(XFA_ATTRIBUTE_Checksum, wsChecksum, false);
-  pValue->SetString(
-      FX_UTF8Encode(wsChecksum.c_str(), wsChecksum.GetLength()).AsStringC());
+  pValue->SetString(wsChecksum.UTF8Encode().AsStringC());
 }
 
 void CXFA_Node::Script_Packet_GetAttribute(CFXJSE_Arguments* pArguments) {
@@ -3427,7 +3409,6 @@
     ThrowParamCountMismatchException(L"getAttribute");
     return;
   }
-
   CFX_ByteString bsAttributeName = pArguments->GetUTF8String(0);
   CFX_WideString wsAttributeValue;
   CFDE_XMLNode* pXMLNode = GetXMLMappingNode();
@@ -3437,8 +3418,7 @@
         wsAttributeValue);
   }
   pArguments->GetReturnValue()->SetString(
-      FX_UTF8Encode(wsAttributeValue.c_str(), wsAttributeValue.GetLength())
-          .AsStringC());
+      wsAttributeValue.UTF8Encode().AsStringC());
 }
 
 void CXFA_Node::Script_Packet_SetAttribute(CFXJSE_Arguments* pArguments) {
@@ -3446,7 +3426,6 @@
     ThrowParamCountMismatchException(L"setAttribute");
     return;
   }
-
   CFX_ByteString bsValue = pArguments->GetUTF8String(0);
   CFX_ByteString bsName = pArguments->GetUTF8String(1);
   CFDE_XMLNode* pXMLNode = GetXMLMappingNode();
@@ -3492,8 +3471,7 @@
       CFDE_XMLElement* pXMLElement = static_cast<CFDE_XMLElement*>(pXMLNode);
       pXMLElement->GetTextData(wsTextData);
     }
-    pValue->SetString(
-        FX_UTF8Encode(wsTextData.c_str(), wsTextData.GetLength()).AsStringC());
+    pValue->SetString(wsTextData.UTF8Encode().AsStringC());
   }
 }
 
diff --git a/xfa/fxfa/parser/cxfa_object.cpp b/xfa/fxfa/parser/cxfa_object.cpp
index 45e3442..c8cd354 100644
--- a/xfa/fxfa/parser/cxfa_object.cpp
+++ b/xfa/fxfa/parser/cxfa_object.cpp
@@ -42,9 +42,7 @@
     ThrowInvalidPropertyException();
     return;
   }
-  CFX_WideStringC className = GetClassName();
-  pValue->SetString(
-      FX_UTF8Encode(className.c_str(), className.GetLength()).AsStringC());
+  pValue->SetString(FX_UTF8Encode(GetClassName()).AsStringC());
 }
 
 void CXFA_Object::ThrowInvalidPropertyException() const {
@@ -71,6 +69,5 @@
   va_start(arg_ptr, str);
   wsMessage.FormatV(str, arg_ptr);
   va_end(arg_ptr);
-  FXJSE_ThrowMessage(
-      FX_UTF8Encode(wsMessage.c_str(), wsMessage.GetLength()).AsStringC());
+  FXJSE_ThrowMessage(wsMessage.UTF8Encode().AsStringC());
 }
diff --git a/xfa/fxfa/parser/cxfa_scriptcontext.cpp b/xfa/fxfa/parser/cxfa_scriptcontext.cpp
index a54ef99..584cd76 100644
--- a/xfa/fxfa/parser/cxfa_scriptcontext.cpp
+++ b/xfa/fxfa/parser/cxfa_scriptcontext.cpp
@@ -161,10 +161,9 @@
       hRetValue->SetUndefined();
       return false;
     }
-    btScript =
-        FX_UTF8Encode(wsJavaScript.GetBuffer(), wsJavaScript.GetLength());
+    btScript = FX_UTF8Encode(wsJavaScript.AsStringC());
   } else {
-    btScript = FX_UTF8Encode(wsScript.c_str(), wsScript.GetLength());
+    btScript = FX_UTF8Encode(wsScript);
   }
   CXFA_Object* pOriginalObject = m_pThisObject;
   m_pThisObject = pThisObject;
@@ -491,8 +490,7 @@
   if (!pTextNode->TryCData(XFA_ATTRIBUTE_Value, wsScript))
     return false;
 
-  CFX_ByteString btScript =
-      FX_UTF8Encode(wsScript.c_str(), wsScript.GetLength());
+  CFX_ByteString btScript = FX_UTF8Encode(wsScript);
   std::unique_ptr<CFXJSE_Value> hRetValue(new CFXJSE_Value(m_pIsolate));
   CXFA_Node* pThisObject = pParent->GetNodeItem(XFA_NODEITEM_Parent);
   CFXJSE_Context* pVariablesContext =