Change CPDF_Dictionary::KeyExist() to take ByteStringView
Make it possible to call CPDF_Dictionary::KeyExist() without creating a
ByteString from a string literal. Change a few callers to avoid
ByteString creation if the key is actually a string literal.
Change-Id: I2071e406d41e4895b32b5d96ad5e325ef0a118fa
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/131870
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
index 27ea644..a8e9e60 100644
--- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
+++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
@@ -534,7 +534,7 @@
while (true) {
name = ByteString::Format("FX%c%d", bsType[0], idnum);
// Avoid name collisions with existing `resource_dict` entries.
- if (resource_dict->KeyExist(name)) {
+ if (resource_dict->KeyExist(name.AsStringView())) {
idnum++;
continue;
}
diff --git a/core/fpdfapi/edit/cpdf_pageorganizer.cpp b/core/fpdfapi/edit/cpdf_pageorganizer.cpp
index 6c1c44f..9fd7aba 100644
--- a/core/fpdfapi/edit/cpdf_pageorganizer.cpp
+++ b/core/fpdfapi/edit/cpdf_pageorganizer.cpp
@@ -166,7 +166,7 @@
RetainPtr<CPDF_Dictionary> dest_page_dict,
RetainPtr<const CPDF_Dictionary> src_page_dict,
const ByteString& key) {
- if (dest_page_dict->KeyExist(key)) {
+ if (dest_page_dict->KeyExist(key.AsStringView())) {
return true;
}
@@ -204,12 +204,12 @@
return nullptr;
}
- if (dict->KeyExist(src_tag)) {
+ if (dict->KeyExist(src_tag.AsStringView())) {
return dict->GetObjectFor(src_tag);
}
while (pp) {
- if (pp->KeyExist(src_tag)) {
+ if (pp->KeyExist(src_tag.AsStringView())) {
return pp->GetObjectFor(src_tag);
}
if (!pp->KeyExist(pdfium::page_object::kParent)) {
diff --git a/core/fpdfapi/page/cpdf_occontext.cpp b/core/fpdfapi/page/cpdf_occontext.cpp
index 7d49a37..0659232 100644
--- a/core/fpdfapi/page/cpdf_occontext.cpp
+++ b/core/fpdfapi/page/cpdf_occontext.cpp
@@ -163,7 +163,7 @@
RetainPtr<const CPDF_Dictionary> pState = pUsage->GetDictFor(csState);
if (pState) {
ByteString csFind = csState + "State";
- if (pState->KeyExist(csFind)) {
+ if (pState->KeyExist(csFind.AsStringView())) {
return pState->GetByteStringFor(csFind) != "OFF";
}
}
diff --git a/core/fpdfapi/parser/cpdf_dictionary.cpp b/core/fpdfapi/parser/cpdf_dictionary.cpp
index 093646e..9d1e008 100644
--- a/core/fpdfapi/parser/cpdf_dictionary.cpp
+++ b/core/fpdfapi/parser/cpdf_dictionary.cpp
@@ -259,7 +259,7 @@
return CFX_Matrix();
}
-bool CPDF_Dictionary::KeyExist(const ByteString& key) const {
+bool CPDF_Dictionary::KeyExist(ByteStringView key) const {
return pdfium::Contains(map_, key);
}
diff --git a/core/fpdfapi/parser/cpdf_dictionary.h b/core/fpdfapi/parser/cpdf_dictionary.h
index 6688f7a..0e47729 100644
--- a/core/fpdfapi/parser/cpdf_dictionary.h
+++ b/core/fpdfapi/parser/cpdf_dictionary.h
@@ -79,7 +79,7 @@
CFX_FloatRect GetRectFor(const ByteString& key) const;
CFX_Matrix GetMatrixFor(const ByteString& key) const;
- bool KeyExist(const ByteString& key) const;
+ bool KeyExist(ByteStringView key) const;
std::vector<ByteString> GetKeys() const;
// Creates a new object owned by the dictionary and returns an unowned
diff --git a/core/fpdfapi/parser/cpdf_linearized_header.cpp b/core/fpdfapi/parser/cpdf_linearized_header.cpp
index 88bab9c..d984e5f 100644
--- a/core/fpdfapi/parser/cpdf_linearized_header.cpp
+++ b/core/fpdfapi/parser/cpdf_linearized_header.cpp
@@ -26,13 +26,14 @@
template <class T>
bool IsValidNumericDictionaryValue(const CPDF_Dictionary* pDict,
- const ByteString& key,
+ ByteStringView key,
T min_value,
bool must_exist = true) {
if (!pDict->KeyExist(key)) {
return !must_exist;
}
- RetainPtr<const CPDF_Number> pNum = pDict->GetNumberFor(key);
+ // TODO(thestig): Avoid ByteString creation.
+ RetainPtr<const CPDF_Number> pNum = pDict->GetNumberFor(ByteString(key));
if (!pNum || !pNum->IsInteger()) {
return false;
}
diff --git a/core/fpdfdoc/cpdf_annot.cpp b/core/fpdfdoc/cpdf_annot.cpp
index 01791c9..73aed4a 100644
--- a/core/fpdfdoc/cpdf_annot.cpp
+++ b/core/fpdfdoc/cpdf_annot.cpp
@@ -117,7 +117,8 @@
pAnnotDict->GetDictFor("Parent");
value = pParentDict ? pParentDict->GetByteStringFor("V") : ByteString();
}
- as = (!value.IsEmpty() && pDict->KeyExist(value)) ? value : "Off";
+ as = (!value.IsEmpty() && pDict->KeyExist(value.AsStringView())) ? value
+ : "Off";
}
return pDict->GetMutableStreamFor(as);
}
diff --git a/core/fpdfdoc/cpdf_apsettings.cpp b/core/fpdfdoc/cpdf_apsettings.cpp
index cf15503..458a31f 100644
--- a/core/fpdfdoc/cpdf_apsettings.cpp
+++ b/core/fpdfdoc/cpdf_apsettings.cpp
@@ -22,7 +22,7 @@
CPDF_ApSettings::~CPDF_ApSettings() = default;
bool CPDF_ApSettings::HasMKEntry(const ByteString& csEntry) const {
- return dict_ && dict_->KeyExist(csEntry);
+ return dict_ && dict_->KeyExist(csEntry.AsStringView());
}
int CPDF_ApSettings::GetRotation() const {
diff --git a/core/fpdfdoc/cpdf_bafontmap.cpp b/core/fpdfdoc/cpdf_bafontmap.cpp
index b50b6b3..be71841 100644
--- a/core/fpdfdoc/cpdf_bafontmap.cpp
+++ b/core/fpdfdoc/cpdf_bafontmap.cpp
@@ -344,7 +344,7 @@
pStreamResList->SetNewFor<CPDF_Reference>("Font", document_,
pStreamResFontList->GetObjNum());
}
- if (!pStreamResFontList->KeyExist(sAlias)) {
+ if (!pStreamResFontList->KeyExist(sAlias.AsStringView())) {
RetainPtr<const CPDF_Dictionary> pFontDict = pFont->GetFontDict();
RetainPtr<CPDF_Object> pObject = pFontDict->IsInline()
? pFontDict->Clone()
diff --git a/core/fpdfdoc/cpdf_generateap.cpp b/core/fpdfdoc/cpdf_generateap.cpp
index 829ebc1..56c1411 100644
--- a/core/fpdfdoc/cpdf_generateap.cpp
+++ b/core/fpdfdoc/cpdf_generateap.cpp
@@ -308,7 +308,7 @@
return false;
}
- if (!font_resource_dict->KeyExist(font_name)) {
+ if (!font_resource_dict->KeyExist(font_name.AsStringView())) {
font_resource_dict->SetNewFor<CPDF_Reference>(font_name, doc,
font_dict->GetObjNum());
}
diff --git a/core/fpdfdoc/cpdf_interactiveform.cpp b/core/fpdfdoc/cpdf_interactiveform.cpp
index be2d6f9..aec0dcc 100644
--- a/core/fpdfdoc/cpdf_interactiveform.cpp
+++ b/core/fpdfdoc/cpdf_interactiveform.cpp
@@ -160,7 +160,7 @@
ByteString key_number;
while (true) {
ByteString key = actual_prefix + key_number;
- if (!pDict->KeyExist(key)) {
+ if (!pDict->KeyExist(key.AsStringView())) {
return key;
}
diff --git a/core/fpdfdoc/cpvt_fontmap.cpp b/core/fpdfdoc/cpvt_fontmap.cpp
index 5b804fe..8e975cb 100644
--- a/core/fpdfdoc/cpvt_fontmap.cpp
+++ b/core/fpdfdoc/cpvt_fontmap.cpp
@@ -43,7 +43,7 @@
RetainPtr<CPDF_Dictionary> pFontList = res_dict_->GetMutableDictFor("Font");
if (ValidateFontResourceDict(pFontList.Get()) &&
- !pFontList->KeyExist(sys_font_alias_)) {
+ !pFontList->KeyExist(sys_font_alias_.AsStringView())) {
pFontList->SetNewFor<CPDF_Reference>(sys_font_alias_, document_,
pPDFFont->GetFontDictObjNum());
}
diff --git a/fpdfsdk/fpdf_edit_embeddertest.cpp b/fpdfsdk/fpdf_edit_embeddertest.cpp
index 639508e..b4adf6c 100644
--- a/fpdfsdk/fpdf_edit_embeddertest.cpp
+++ b/fpdfsdk/fpdf_edit_embeddertest.cpp
@@ -226,16 +226,17 @@
EXPECT_TRUE(font_desc->KeyExist("Descent"));
EXPECT_TRUE(font_desc->KeyExist("CapHeight"));
EXPECT_TRUE(font_desc->KeyExist("StemV"));
- ByteString present("FontFile");
- ByteString absent("FontFile2");
+ ByteStringView present("FontFile");
+ ByteStringView absent("FontFile2");
if (font_type == FPDF_FONT_TRUETYPE) {
std::swap(present, absent);
}
EXPECT_TRUE(font_desc->KeyExist(present));
EXPECT_FALSE(font_desc->KeyExist(absent));
- auto streamAcc =
- pdfium::MakeRetain<CPDF_StreamAcc>(font_desc->GetStreamFor(present));
+ // TODO(thestig): Avoid ByteString creation.
+ auto streamAcc = pdfium::MakeRetain<CPDF_StreamAcc>(
+ font_desc->GetStreamFor(ByteString(present)));
streamAcc->LoadAllDataRaw();
// Check that the font stream is the one that was provided
diff --git a/fpdfsdk/fpdf_flatten.cpp b/fpdfsdk/fpdf_flatten.cpp
index 5a5b970..dea1505 100644
--- a/fpdfsdk/fpdf_flatten.cpp
+++ b/fpdfsdk/fpdf_flatten.cpp
@@ -377,7 +377,7 @@
int i = 0;
while (i < INT_MAX) {
ByteString sKey = ByteString::Format("FFT%d", i);
- if (!pPageXObject->KeyExist(sKey)) {
+ if (!pPageXObject->KeyExist(sKey.AsStringView())) {
key = std::move(sKey);
break;
}