Make wide-form of CPDF_String constructor take a view.
Avoid having callers implictly make temporary wide strings
when inserting literals, thus saving an allocate and copy in
these cases.
-- in turn, make PDF_EncodeText() take a view.
Change-Id: I136726cb1eb96045bf2d0585207d469001e9fa9b
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/91632
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/parser/cpdf_string.cpp b/core/fpdfapi/parser/cpdf_string.cpp
index 2501cd1..fe667d7 100644
--- a/core/fpdfapi/parser/cpdf_string.cpp
+++ b/core/fpdfapi/parser/cpdf_string.cpp
@@ -23,7 +23,7 @@
m_String = pPool->Intern(m_String);
}
-CPDF_String::CPDF_String(WeakPtr<ByteStringPool> pPool, const WideString& str)
+CPDF_String::CPDF_String(WeakPtr<ByteStringPool> pPool, WideStringView str)
: m_String(PDF_EncodeText(str)) {
if (pPool)
m_String = pPool->Intern(m_String);
diff --git a/core/fpdfapi/parser/cpdf_string.h b/core/fpdfapi/parser/cpdf_string.h
index 5f4c0e3..fc9798b 100644
--- a/core/fpdfapi/parser/cpdf_string.h
+++ b/core/fpdfapi/parser/cpdf_string.h
@@ -35,7 +35,7 @@
private:
CPDF_String();
CPDF_String(WeakPtr<ByteStringPool> pPool, const ByteString& str, bool bHex);
- CPDF_String(WeakPtr<ByteStringPool> pPool, const WideString& str);
+ CPDF_String(WeakPtr<ByteStringPool> pPool, WideStringView str);
~CPDF_String() override;
ByteString m_String;
diff --git a/core/fpdfapi/parser/fpdf_parser_decode.cpp b/core/fpdfapi/parser/fpdf_parser_decode.cpp
index f6e3c8c..e40aecc 100644
--- a/core/fpdfapi/parser/fpdf_parser_decode.cpp
+++ b/core/fpdfapi/parser/fpdf_parser_decode.cpp
@@ -515,7 +515,7 @@
return result;
}
-ByteString PDF_EncodeText(const WideString& str) {
+ByteString PDF_EncodeText(WideStringView str) {
size_t i = 0;
size_t len = str.GetLength();
ByteString result;
diff --git a/core/fpdfapi/parser/fpdf_parser_decode.h b/core/fpdfapi/parser/fpdf_parser_decode.h
index 537456b..7ef280e 100644
--- a/core/fpdfapi/parser/fpdf_parser_decode.h
+++ b/core/fpdfapi/parser/fpdf_parser_decode.h
@@ -33,7 +33,7 @@
ByteString PDF_EncodeString(ByteStringView src);
ByteString PDF_HexEncodeString(ByteStringView src);
WideString PDF_DecodeText(pdfium::span<const uint8_t> span);
-ByteString PDF_EncodeText(const WideString& str);
+ByteString PDF_EncodeText(WideStringView str);
std::unique_ptr<fxcodec::ScanlineDecoder> CreateFaxDecoder(
pdfium::span<const uint8_t> src_span,
diff --git a/core/fpdfdoc/cpdf_filespec.cpp b/core/fpdfdoc/cpdf_filespec.cpp
index faa681e..9af7b9c 100644
--- a/core/fpdfdoc/cpdf_filespec.cpp
+++ b/core/fpdfdoc/cpdf_filespec.cpp
@@ -211,6 +211,6 @@
m_pWritableObj->SetString(wsStr.ToDefANSI());
} else if (CPDF_Dictionary* pDict = m_pWritableObj->AsDictionary()) {
pDict->SetNewFor<CPDF_String>(pdfium::stream::kF, wsStr.ToDefANSI(), false);
- pDict->SetNewFor<CPDF_String>("UF", wsStr);
+ pDict->SetNewFor<CPDF_String>("UF", wsStr.AsStringView());
}
}
diff --git a/core/fpdfdoc/cpdf_formfield.cpp b/core/fpdfdoc/cpdf_formfield.cpp
index 37a139e..e882098 100644
--- a/core/fpdfdoc/cpdf_formfield.cpp
+++ b/core/fpdfdoc/cpdf_formfield.cpp
@@ -354,7 +354,7 @@
}
ByteString key(bDefault ? pdfium::form_fields::kDV
: pdfium::form_fields::kV);
- m_pDict->SetNewFor<CPDF_String>(key, csValue);
+ m_pDict->SetNewFor<CPDF_String>(key, csValue.AsStringView());
int iIndex = FindOption(csValue);
if (iIndex < 0) {
if (m_Type == kRichText && !bDefault) {
@@ -514,7 +514,8 @@
void CPDF_FormField::SetItemSelectionSelected(int index,
const WideString& opt_value) {
if (GetType() != kListBox) {
- m_pDict->SetNewFor<CPDF_String>(pdfium::form_fields::kV, opt_value);
+ m_pDict->SetNewFor<CPDF_String>(pdfium::form_fields::kV,
+ opt_value.AsStringView());
CPDF_Array* pI = m_pDict->SetNewFor<CPDF_Array>("I");
pI->AppendNew<CPDF_Number>(index);
return;
@@ -522,14 +523,15 @@
SelectOption(index);
if (!m_bIsMultiSelectListBox) {
- m_pDict->SetNewFor<CPDF_String>(pdfium::form_fields::kV, opt_value);
+ m_pDict->SetNewFor<CPDF_String>(pdfium::form_fields::kV,
+ opt_value.AsStringView());
return;
}
CPDF_Array* pArray = m_pDict->SetNewFor<CPDF_Array>(pdfium::form_fields::kV);
for (int i = 0; i < CountOptions(); i++) {
if (i == index || IsItemSelected(i))
- pArray->AppendNew<CPDF_String>(GetOptionValue(i));
+ pArray->AppendNew<CPDF_String>(GetOptionValue(i).AsStringView());
}
}
@@ -626,7 +628,7 @@
const CPDF_Object* pOpt = GetFieldAttr(m_pDict.Get(), "Opt");
if (!ToArray(pOpt)) {
- ByteString csBExport = PDF_EncodeText(csWExport);
+ ByteString csBExport = PDF_EncodeText(csWExport.AsStringView());
if (bChecked) {
m_pDict->SetNewFor<CPDF_Name>(pdfium::form_fields::kV, csBExport);
} else {
diff --git a/core/fpdfdoc/cpdf_interactiveform.cpp b/core/fpdfdoc/cpdf_interactiveform.cpp
index 56cd622..b6eb591 100644
--- a/core/fpdfdoc/cpdf_interactiveform.cpp
+++ b/core/fpdfdoc/cpdf_interactiveform.cpp
@@ -956,11 +956,12 @@
WideString fullname =
CPDF_FormField::GetFullNameForDict(pField->GetFieldDict());
auto pFieldDict = pDoc->New<CPDF_Dictionary>();
- pFieldDict->SetNewFor<CPDF_String>(pdfium::form_fields::kT, fullname);
+ pFieldDict->SetNewFor<CPDF_String>(pdfium::form_fields::kT,
+ fullname.AsStringView());
if (pField->GetType() == CPDF_FormField::kCheckBox ||
pField->GetType() == CPDF_FormField::kRadioButton) {
WideString csExport = pField->GetCheckValue(false);
- ByteString csBExport = PDF_EncodeText(csExport);
+ ByteString csBExport = PDF_EncodeText(csExport.AsStringView());
CPDF_Object* pOpt =
CPDF_FormField::GetFieldAttr(pField->GetDict(), "Opt");
if (pOpt) {
diff --git a/core/fpdfdoc/cpdf_nametree.cpp b/core/fpdfdoc/cpdf_nametree.cpp
index b44353c..ffec5fe 100644
--- a/core/fpdfdoc/cpdf_nametree.cpp
+++ b/core/fpdfdoc/cpdf_nametree.cpp
@@ -30,8 +30,8 @@
WideString csRight = pLimits->GetUnicodeTextAt(1);
// If the lower limit is greater than the upper limit, swap them.
if (csLeft.Compare(csRight) > 0) {
- pLimits->SetNewAt<CPDF_String>(0, csRight);
- pLimits->SetNewAt<CPDF_String>(1, csLeft);
+ pLimits->SetNewAt<CPDF_String>(0, csRight.AsStringView());
+ pLimits->SetNewAt<CPDF_String>(1, csLeft.AsStringView());
csLeft = pLimits->GetUnicodeTextAt(0);
csRight = pLimits->GetUnicodeTextAt(1);
}
@@ -117,8 +117,8 @@
if (wsName.Compare(csNewRight) > 0)
csNewRight = wsName;
}
- pLimits->SetNewAt<CPDF_String>(0, csNewLeft);
- pLimits->SetNewAt<CPDF_String>(1, csNewRight);
+ pLimits->SetNewAt<CPDF_String>(0, csNewLeft.AsStringView());
+ pLimits->SetNewAt<CPDF_String>(1, csNewRight.AsStringView());
return true;
}
@@ -156,8 +156,8 @@
if (pKidLimits->GetUnicodeTextAt(1).Compare(csNewRight) > 0)
csNewRight = pKidLimits->GetUnicodeTextAt(1);
}
- pLimits->SetNewAt<CPDF_String>(0, csNewLeft);
- pLimits->SetNewAt<CPDF_String>(1, csNewRight);
+ pLimits->SetNewAt<CPDF_String>(0, csNewLeft.AsStringView());
+ pLimits->SetNewAt<CPDF_String>(1, csNewRight.AsStringView());
return true;
}
return false;
@@ -473,7 +473,7 @@
// insertion position is right after the key-value pair returned by |index|.
size_t nNameIndex = (nFindIndex + 1) * 2;
size_t nValueIndex = nNameIndex + 1;
- pFind->InsertNewAt<CPDF_String>(nNameIndex, name);
+ pFind->InsertNewAt<CPDF_String>(nNameIndex, name.AsStringView());
pFind->InsertAt(nValueIndex, std::move(pObj));
// Expand the limits that the newly added name is under, if the name falls
@@ -485,10 +485,10 @@
continue;
if (name.Compare(pLimits->GetUnicodeTextAt(0)) < 0)
- pLimits->SetNewAt<CPDF_String>(0, name);
+ pLimits->SetNewAt<CPDF_String>(0, name.AsStringView());
if (name.Compare(pLimits->GetUnicodeTextAt(1)) > 0)
- pLimits->SetNewAt<CPDF_String>(1, name);
+ pLimits->SetNewAt<CPDF_String>(1, name.AsStringView());
}
return true;
}
diff --git a/fpdfsdk/cpdfsdk_baannot.cpp b/fpdfsdk/cpdfsdk_baannot.cpp
index d9cbe5a..cb00e69 100644
--- a/fpdfsdk/cpdfsdk_baannot.cpp
+++ b/fpdfsdk/cpdfsdk_baannot.cpp
@@ -68,10 +68,11 @@
void CPDFSDK_BAAnnot::SetAnnotName(const WideString& sName) {
CPDF_Dictionary* pDict = GetAnnotDict();
- if (sName.IsEmpty())
+ if (sName.IsEmpty()) {
pDict->RemoveFor(pdfium::annotation::kNM);
- else
- pDict->SetNewFor<CPDF_String>(pdfium::annotation::kNM, sName);
+ return;
+ }
+ pDict->SetNewFor<CPDF_String>(pdfium::annotation::kNM, sName.AsStringView());
}
WideString CPDFSDK_BAAnnot::GetAnnotName() const {
diff --git a/fpdfsdk/fpdf_annot.cpp b/fpdfsdk/fpdf_annot.cpp
index 1889b3b..27edb59 100644
--- a/fpdfsdk/fpdf_annot.cpp
+++ b/fpdfsdk/fpdf_annot.cpp
@@ -983,7 +983,8 @@
if (!pAnnotDict)
return false;
- pAnnotDict->SetNewFor<CPDF_String>(key, WideStringFromFPDFWideString(value));
+ pAnnotDict->SetNewFor<CPDF_String>(
+ key, WideStringFromFPDFWideString(value).AsStringView());
return true;
}
@@ -1059,7 +1060,7 @@
CPDF_Stream* pNewIndirectStream = pDoc->NewIndirect<CPDF_Stream>();
ByteString newAPStream =
- PDF_EncodeText(WideStringFromFPDFWideString(value));
+ PDF_EncodeText(WideStringFromFPDFWideString(value).AsStringView());
pNewIndirectStream->SetData(newAPStream.raw_span());
CPDF_Dictionary* pStreamDict = pNewIndirectStream->GetDict();
diff --git a/fpdfsdk/fpdf_attachment.cpp b/fpdfsdk/fpdf_attachment.cpp
index 7369a10..e93f125 100644
--- a/fpdfsdk/fpdf_attachment.cpp
+++ b/fpdfsdk/fpdf_attachment.cpp
@@ -79,8 +79,8 @@
// Set up the basic entries in the filespec dictionary.
CPDF_Dictionary* pFile = pDoc->NewIndirect<CPDF_Dictionary>();
pFile->SetNewFor<CPDF_Name>("Type", "Filespec");
- pFile->SetNewFor<CPDF_String>("UF", wsName);
- pFile->SetNewFor<CPDF_String>(pdfium::stream::kF, wsName);
+ pFile->SetNewFor<CPDF_String>("UF", wsName.AsStringView());
+ pFile->SetNewFor<CPDF_String>(pdfium::stream::kF, wsName.AsStringView());
// Add the new attachment name and filespec into the document's EmbeddedFiles.
if (!name_tree->AddValueAndName(pFile->MakeReference(pDoc), wsName))
diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp b/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp
index 28e2c84..3160407 100644
--- a/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp
+++ b/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp
@@ -417,7 +417,7 @@
CPDF_Dictionary* pInfoDict = m_pContext->GetPDFDoc()->GetInfo();
if (pInfoDict)
- pInfoDict->SetNewFor<CPDF_String>("Title", wsTitle);
+ pInfoDict->SetNewFor<CPDF_String>("Title", wsTitle.AsStringView());
}
void CPDFXFA_DocEnvironment::ExportData(CXFA_FFDoc* hDoc,
diff --git a/fxjs/cjs_document.cpp b/fxjs/cjs_document.cpp
index 328ff28..7777dda 100644
--- a/fxjs/cjs_document.cpp
+++ b/fxjs/cjs_document.cpp
@@ -748,7 +748,8 @@
if (!m_pFormFillEnv->HasPermissions(kModifyContent))
return CJS_Result::Failure(JSMessage::kPermissionError);
- pDictionary->SetNewFor<CPDF_String>(propName, pRuntime->ToWideString(vp));
+ pDictionary->SetNewFor<CPDF_String>(
+ propName, pRuntime->ToWideString(vp).AsStringView());
m_pFormFillEnv->SetChangeMark();
return CJS_Result::Success();
}