From 8b36e5cc3d5f5f579c6b060e2c40b896a4b02bc0 Mon Sep 17 00:00:00 2001 From: tsepez Date: Fri, 8 Apr 2016 09:00:35 -0700 Subject: Make CFX_WideString::FromLocal() take a CFX_ByteStringC arg It doesn't persist the string beyond the duration of the call, hence it should take the *StringC variant. Doing so avoids some allocs by changing to the *StringC ctor in a few places, at the cost of some explicit .ToByteStringC() calls (which are cheap). Review URL: https://codereview.chromium.org/1862953004 --- core/fpdfdoc/doc_action.cpp | 3 ++- core/fpdfdoc/doc_basic.cpp | 14 +++++++++----- core/fpdfdoc/doc_form.cpp | 5 +++-- core/fxcrt/fx_basic_bstring.cpp | 2 +- core/fxcrt/fx_basic_wstring.cpp | 13 +++++++------ core/fxcrt/fx_extension.cpp | 2 +- core/fxcrt/include/fx_basic.h | 4 ++-- core/fxcrt/include/fx_string.h | 4 ++-- fpdfsdk/fsdk_mgr.cpp | 6 +++--- fpdfsdk/javascript/PublicMethods.cpp | 4 ++-- xfa/fgas/font/fgas_gefont.cpp | 6 ++++-- xfa/fxbarcode/BC_UtilCodingConvert.cpp | 4 ++-- 12 files changed, 38 insertions(+), 29 deletions(-) diff --git a/core/fpdfdoc/doc_action.cpp b/core/fpdfdoc/doc_action.cpp index c3ab552606..075849482c 100644 --- a/core/fpdfdoc/doc_action.cpp +++ b/core/fpdfdoc/doc_action.cpp @@ -73,7 +73,8 @@ CFX_WideString CPDF_Action::GetFilePath() const { if (type == "Launch") { CPDF_Dictionary* pWinDict = m_pDict->GetDictBy("Win"); if (pWinDict) { - return CFX_WideString::FromLocal(pWinDict->GetStringBy("F")); + return CFX_WideString::FromLocal( + pWinDict->GetStringBy("F").AsByteStringC()); } } return path; diff --git a/core/fpdfdoc/doc_basic.cpp b/core/fpdfdoc/doc_basic.cpp index 9318387f7f..308bf04287 100644 --- a/core/fpdfdoc/doc_basic.cpp +++ b/core/fpdfdoc/doc_basic.cpp @@ -315,23 +315,27 @@ bool CPDF_FileSpec::GetFileName(CFX_WideString* csFileName) const { if (CPDF_Dictionary* pDict = m_pObj->AsDictionary()) { *csFileName = pDict->GetUnicodeTextBy("UF"); if (csFileName->IsEmpty()) { - *csFileName = CFX_WideString::FromLocal(pDict->GetStringBy("F")); + *csFileName = CFX_WideString::FromLocal(pDict->GetConstStringBy("F")); } if (pDict->GetStringBy("FS") == "URL") return true; if (csFileName->IsEmpty()) { if (pDict->KeyExist("DOS")) { - *csFileName = CFX_WideString::FromLocal(pDict->GetStringBy("DOS")); + *csFileName = CFX_WideString::FromLocal( + pDict->GetStringBy("DOS").AsByteStringC()); } else if (pDict->KeyExist("Mac")) { - *csFileName = CFX_WideString::FromLocal(pDict->GetStringBy("Mac")); + *csFileName = CFX_WideString::FromLocal( + pDict->GetStringBy("Mac").AsByteStringC()); } else if (pDict->KeyExist("Unix")) { - *csFileName = CFX_WideString::FromLocal(pDict->GetStringBy("Unix")); + *csFileName = CFX_WideString::FromLocal( + pDict->GetStringBy("Unix").AsByteStringC()); } else { return false; } } } else if (m_pObj->IsString()) { - *csFileName = CFX_WideString::FromLocal(m_pObj->GetString()); + *csFileName = + CFX_WideString::FromLocal(m_pObj->GetString().AsByteStringC()); } else { return false; } diff --git a/core/fpdfdoc/doc_form.cpp b/core/fpdfdoc/doc_form.cpp index 80c09714a2..384bcaa435 100644 --- a/core/fpdfdoc/doc_form.cpp +++ b/core/fpdfdoc/doc_form.cpp @@ -35,12 +35,13 @@ CFX_WideString FPDFDOC_FDF_GetFieldValue(const CPDF_Dictionary& pFieldDict, const CFX_ByteString csBValue = pFieldDict.GetStringBy("V"); for (const auto& encoding : g_fieldEncoding) { if (bsEncoding == encoding.m_name) - return CFX_WideString::FromCodePage(csBValue, encoding.m_codePage); + return CFX_WideString::FromCodePage(csBValue.AsByteStringC(), + encoding.m_codePage); } CFX_ByteString csTemp = csBValue.Left(2); if (csTemp == "\xFF\xFE" || csTemp == "\xFE\xFF") return PDF_DecodeText(csBValue); - return CFX_WideString::FromLocal(csBValue); + return CFX_WideString::FromLocal(csBValue.AsByteStringC()); } } // namespace diff --git a/core/fxcrt/fx_basic_bstring.cpp b/core/fxcrt/fx_basic_bstring.cpp index 2c2264a419..cc0dfe2341 100644 --- a/core/fxcrt/fx_basic_bstring.cpp +++ b/core/fxcrt/fx_basic_bstring.cpp @@ -824,7 +824,7 @@ CFX_ByteString CFX_ByteString::FromUnicode(const FX_WCHAR* str, // static CFX_ByteString CFX_ByteString::FromUnicode(const CFX_WideString& str) { - return CFX_CharMap::GetByteString(0, str); + return CFX_CharMap::GetByteString(0, str.AsWideStringC()); } int CFX_ByteString::Compare(const CFX_ByteStringC& str) const { diff --git a/core/fxcrt/fx_basic_wstring.cpp b/core/fxcrt/fx_basic_wstring.cpp index e2e122be9a..b5ccaf6da5 100644 --- a/core/fxcrt/fx_basic_wstring.cpp +++ b/core/fxcrt/fx_basic_wstring.cpp @@ -747,12 +747,12 @@ void CFX_WideString::SetAt(FX_STRSIZE nIndex, FX_WCHAR ch) { } // static -CFX_WideString CFX_WideString::FromLocal(const CFX_ByteString& str) { +CFX_WideString CFX_WideString::FromLocal(const CFX_ByteStringC& str) { return FromCodePage(str, 0); } // static -CFX_WideString CFX_WideString::FromCodePage(const CFX_ByteString& str, +CFX_WideString CFX_WideString::FromCodePage(const CFX_ByteStringC& str, uint16_t codepage) { return CFX_CharMap::GetWideString(codepage, str); } @@ -943,7 +943,7 @@ FX_FLOAT CFX_WideString::GetFloat() const { // static CFX_ByteString CFX_CharMap::GetByteString(uint16_t codepage, - const CFX_WideString& wstr) { + const CFX_WideStringC& wstr) { FXSYS_assert(IsValidCodePage(codepage)); int src_len = wstr.GetLength(); int dest_len = FXSYS_WideCharToMultiByte(codepage, 0, wstr.c_str(), src_len, @@ -960,15 +960,16 @@ CFX_ByteString CFX_CharMap::GetByteString(uint16_t codepage, // static CFX_WideString CFX_CharMap::GetWideString(uint16_t codepage, - const CFX_ByteString& bstr) { + const CFX_ByteStringC& bstr) { FXSYS_assert(IsValidCodePage(codepage)); int src_len = bstr.GetLength(); int dest_len = - FXSYS_MultiByteToWideChar(codepage, 0, bstr, src_len, nullptr, 0); + FXSYS_MultiByteToWideChar(codepage, 0, bstr.c_str(), src_len, nullptr, 0); CFX_WideString wstr; if (dest_len) { FX_WCHAR* dest_buf = wstr.GetBuffer(dest_len); - FXSYS_MultiByteToWideChar(codepage, 0, bstr, src_len, dest_buf, dest_len); + FXSYS_MultiByteToWideChar(codepage, 0, bstr.c_str(), src_len, dest_buf, + dest_len); wstr.ReleaseBuffer(dest_len); } return wstr; diff --git a/core/fxcrt/fx_extension.cpp b/core/fxcrt/fx_extension.cpp index 7e8029baba..f1fd980120 100644 --- a/core/fxcrt/fx_extension.cpp +++ b/core/fxcrt/fx_extension.cpp @@ -134,7 +134,7 @@ FX_FLOAT FXSYS_strtof(const FX_CHAR* pcsStr, iLength = (int32_t)FXSYS_strlen(pcsStr); } CFX_WideString ws = - CFX_WideString::FromLocal(CFX_ByteString(pcsStr, iLength)); + CFX_WideString::FromLocal(CFX_ByteStringC(pcsStr, iLength)); return FXSYS_wcstof(ws.c_str(), iLength, pUsedLen); } FX_FLOAT FXSYS_wcstof(const FX_WCHAR* pwsStr, diff --git a/core/fxcrt/include/fx_basic.h b/core/fxcrt/include/fx_basic.h index 744b2dd10f..1cb5174351 100644 --- a/core/fxcrt/include/fx_basic.h +++ b/core/fxcrt/include/fx_basic.h @@ -186,10 +186,10 @@ class CFX_FileBufferArchive { class CFX_CharMap { public: static CFX_ByteString GetByteString(uint16_t codepage, - const CFX_WideString& wstr); + const CFX_WideStringC& wstr); static CFX_WideString GetWideString(uint16_t codepage, - const CFX_ByteString& bstr); + const CFX_ByteStringC& bstr); CFX_CharMap() = delete; }; diff --git a/core/fxcrt/include/fx_string.h b/core/fxcrt/include/fx_string.h index 382e141dc2..8a0b3ff3db 100644 --- a/core/fxcrt/include/fx_string.h +++ b/core/fxcrt/include/fx_string.h @@ -497,8 +497,8 @@ class CFX_WideString { ~CFX_WideString(); - static CFX_WideString FromLocal(const CFX_ByteString& str); - static CFX_WideString FromCodePage(const CFX_ByteString& str, + static CFX_WideString FromLocal(const CFX_ByteStringC& str); + static CFX_WideString FromCodePage(const CFX_ByteStringC& str, uint16_t codepage); static CFX_WideString FromUTF8(const CFX_ByteStringC& str); diff --git a/fpdfsdk/fsdk_mgr.cpp b/fpdfsdk/fsdk_mgr.cpp index 6c7a8bb4b2..e6ad18d232 100644 --- a/fpdfsdk/fsdk_mgr.cpp +++ b/fpdfsdk/fsdk_mgr.cpp @@ -296,7 +296,7 @@ CFX_WideString CPDFDoc_Environment::JS_fieldBrowse() { if (nActualLen <= 0 || nActualLen > nRequiredLen) return CFX_WideString(); - return CFX_WideString::FromLocal(CFX_ByteString(pBuff.get(), nActualLen)); + return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff.get(), nActualLen)); } CFX_WideString CPDFDoc_Environment::JS_docGetFilePath() { @@ -316,7 +316,7 @@ CFX_WideString CPDFDoc_Environment::JS_docGetFilePath() { if (nActualLen <= 0 || nActualLen > nRequiredLen) return CFX_WideString(); - return CFX_WideString::FromLocal(CFX_ByteString(pBuff.get(), nActualLen)); + return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff.get(), nActualLen)); } void CPDFDoc_Environment::JS_docSubmitForm(void* formData, @@ -476,7 +476,7 @@ void CPDFSDK_Document::ProcJavascriptFun() { CPDF_Action jsAction = docJS.GetJSAction(i, csJSName); if (m_pEnv->GetActionHander()) m_pEnv->GetActionHander()->DoAction_JavaScript( - jsAction, CFX_WideString::FromLocal(csJSName), this); + jsAction, CFX_WideString::FromLocal(csJSName.AsByteStringC()), this); } } diff --git a/fpdfsdk/javascript/PublicMethods.cpp b/fpdfsdk/javascript/PublicMethods.cpp index 308163d454..d5b961d61d 100644 --- a/fpdfsdk/javascript/PublicMethods.cpp +++ b/fpdfsdk/javascript/PublicMethods.cpp @@ -837,7 +837,7 @@ FX_BOOL CJS_PublicMethods::AFNumber_Format(IJS_Context* cc, } // for processing currency string - Value = CFX_WideString::FromLocal(strValue); + Value = CFX_WideString::FromLocal(strValue.AsByteStringC()); std::wstring strValue2 = Value.c_str(); if (bCurrencyPrepend) @@ -1120,7 +1120,7 @@ FX_BOOL CJS_PublicMethods::AFPercent_Format( if (iNegative) strValue = "-" + strValue; strValue += "%"; - Value = CFX_WideString::FromLocal(strValue); + Value = CFX_WideString::FromLocal(strValue.AsByteStringC()); #endif return TRUE; } diff --git a/xfa/fgas/font/fgas_gefont.cpp b/xfa/fgas/font/fgas_gefont.cpp index 3d740ba43a..8652c742fc 100644 --- a/xfa/fgas/font/fgas_gefont.cpp +++ b/xfa/fgas/font/fgas_gefont.cpp @@ -347,9 +347,11 @@ uint8_t CFX_GEFont::GetCharSet() const { void CFX_GEFont::GetFamilyName(CFX_WideString& wsFamily) const { if (!m_pFont->GetSubstFont() || m_pFont->GetSubstFont()->m_Family.GetLength() == 0) { - wsFamily = CFX_WideString::FromLocal(m_pFont->GetFamilyName()); + wsFamily = + CFX_WideString::FromLocal(m_pFont->GetFamilyName().AsByteStringC()); } else { - wsFamily = CFX_WideString::FromLocal(m_pFont->GetSubstFont()->m_Family); + wsFamily = CFX_WideString::FromLocal( + m_pFont->GetSubstFont()->m_Family.AsByteStringC()); } } void CFX_GEFont::GetPsName(CFX_WideString& wsName) const { diff --git a/xfa/fxbarcode/BC_UtilCodingConvert.cpp b/xfa/fxbarcode/BC_UtilCodingConvert.cpp index 79311691e2..118e45e95d 100644 --- a/xfa/fxbarcode/BC_UtilCodingConvert.cpp +++ b/xfa/fxbarcode/BC_UtilCodingConvert.cpp @@ -17,13 +17,13 @@ void CBC_UtilCodingConvert::UnicodeToLocale(const CFX_WideString& src, void CBC_UtilCodingConvert::LocaleToUtf8(const CFX_ByteString& src, CFX_ByteString& dst) { - CFX_WideString unicode = CFX_WideString::FromLocal(src); + CFX_WideString unicode = CFX_WideString::FromLocal(src.AsByteStringC()); dst = unicode.UTF8Encode(); } void CBC_UtilCodingConvert::LocaleToUtf8(const CFX_ByteString& src, CFX_ByteArray& dst) { - CFX_WideString unicode = CFX_WideString::FromLocal(src); + CFX_WideString unicode = CFX_WideString::FromLocal(src.AsByteStringC()); CFX_ByteString utf8 = unicode.UTF8Encode(); for (int32_t i = 0; i < utf8.GetLength(); i++) { dst.Add(utf8[i]); -- cgit v1.2.3