diff options
author | Oliver Chang <ochang@chromium.org> | 2016-01-11 08:45:31 -0800 |
---|---|---|
committer | Oliver Chang <ochang@chromium.org> | 2016-01-11 08:45:31 -0800 |
commit | 3f1c71f5a6ea058e3eec611c9dcc759b374dcb80 (patch) | |
tree | b4ef8de26360979381c441cbdfaba94e493b09f6 /core/src/fpdfapi/fpdf_parser | |
parent | c909ce872d999a17ffd44afdc88caf2de43e6cba (diff) | |
download | pdfium-3f1c71f5a6ea058e3eec611c9dcc759b374dcb80.tar.xz |
Merge to XFA: Use std::map as CPDF_Dictionary's underlying store.
Replaces CFX_CMapByteStringToPtr. XFA still uses CFX_CMapByteStringToPtr
so it's not completely removed just yet.
Adds begin()/end() to CPDF_Dictionary and removes the
GetStartPos()/GetNextElement() functions to traverse the dictionary.
Callers are changed accordingly. AddValue() is also removed.
TBR=tsepez@chromium.org
Review URL: https://codereview.chromium.org/1541703003 .
(cherry picked from commit 14f39950451bb9c2a11fbc7173fd47367410f80f)
Review URL: https://codereview.chromium.org/1576033002 .
Diffstat (limited to 'core/src/fpdfapi/fpdf_parser')
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp | 138 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp | 28 | ||||
-rw-r--r-- | core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp | 7 |
3 files changed, 68 insertions, 105 deletions
diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp index 685becaf69..2cb81002fb 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_objects.cpp @@ -267,12 +267,9 @@ CPDF_Object* CPDF_Object::CloneInternal(FX_BOOL bDirect, case PDFOBJ_DICTIONARY: { CPDF_Dictionary* pCopy = new CPDF_Dictionary(); const CPDF_Dictionary* pThis = AsDictionary(); - FX_POSITION pos = pThis->m_Map.GetStartPosition(); - while (pos) { - CFX_ByteString key; - CPDF_Object* value; - pThis->m_Map.GetNextAssoc(pos, key, (void*&)value); - pCopy->m_Map.SetAt(key, value->CloneInternal(bDirect, visited)); + for (const auto& it : *pThis) { + pCopy->m_Map.insert(std::make_pair( + it.first, it.second->CloneInternal(bDirect, visited))); } return pCopy; } @@ -578,38 +575,23 @@ FX_BOOL CPDF_Array::Identical(CPDF_Array* pOther) const { return TRUE; } CPDF_Dictionary::~CPDF_Dictionary() { - FX_POSITION pos = m_Map.GetStartPosition(); - while (pos) { - if (CPDF_Object* value = static_cast<CPDF_Object*>(m_Map.GetNextValue(pos))) - value->Release(); - } -} -FX_POSITION CPDF_Dictionary::GetStartPos() const { - return m_Map.GetStartPosition(); -} -CPDF_Object* CPDF_Dictionary::GetNextElement(FX_POSITION& pos, - CFX_ByteString& key) const { - if (!pos) { - return NULL; + for (const auto& it : m_Map) { + it.second->Release(); } - CPDF_Object* p; - m_Map.GetNextAssoc(pos, key, (void*&)p); - return p; } CPDF_Object* CPDF_Dictionary::GetElement(const CFX_ByteStringC& key) const { - CPDF_Object* p = NULL; - m_Map.Lookup(key, (void*&)p); - return p; + auto it = m_Map.find(key); + if (it == m_Map.end()) + return nullptr; + return it->second; } CPDF_Object* CPDF_Dictionary::GetElementValue( const CFX_ByteStringC& key) const { - CPDF_Object* p = NULL; - m_Map.Lookup(key, (void*&)p); - return p ? p->GetDirect() : NULL; + CPDF_Object* p = GetElement(key); + return p ? p->GetDirect() : nullptr; } CFX_ByteString CPDF_Dictionary::GetString(const CFX_ByteStringC& key) const { - CPDF_Object* p = NULL; - m_Map.Lookup(key, (void*&)p); + CPDF_Object* p = GetElement(key); if (p) { return p->GetString(); } @@ -617,8 +599,7 @@ CFX_ByteString CPDF_Dictionary::GetString(const CFX_ByteStringC& key) const { } CFX_ByteStringC CPDF_Dictionary::GetConstString( const CFX_ByteStringC& key) const { - CPDF_Object* p = NULL; - m_Map.Lookup(key, (void*&)p); + CPDF_Object* p = GetElement(key); if (p) { return p->GetConstString(); } @@ -626,16 +607,14 @@ CFX_ByteStringC CPDF_Dictionary::GetConstString( } CFX_WideString CPDF_Dictionary::GetUnicodeText(const CFX_ByteStringC& key, CFX_CharMap* pCharMap) const { - CPDF_Object* p = NULL; - m_Map.Lookup(key, (void*&)p); + CPDF_Object* p = GetElement(key); if (CPDF_Reference* pRef = ToReference(p)) p = pRef->GetDirect(); return p ? p->GetUnicodeText(pCharMap) : CFX_WideString(); } CFX_ByteString CPDF_Dictionary::GetString(const CFX_ByteStringC& key, const CFX_ByteStringC& def) const { - CPDF_Object* p = NULL; - m_Map.Lookup(key, (void*&)p); + CPDF_Object* p = GetElement(key); if (p) { return p->GetString(); } @@ -644,32 +623,28 @@ CFX_ByteString CPDF_Dictionary::GetString(const CFX_ByteStringC& key, CFX_ByteStringC CPDF_Dictionary::GetConstString( const CFX_ByteStringC& key, const CFX_ByteStringC& def) const { - CPDF_Object* p = NULL; - m_Map.Lookup(key, (void*&)p); + CPDF_Object* p = GetElement(key); if (p) { return p->GetConstString(); } return CFX_ByteStringC(def); } int CPDF_Dictionary::GetInteger(const CFX_ByteStringC& key) const { - CPDF_Object* p = NULL; - m_Map.Lookup(key, (void*&)p); + CPDF_Object* p = GetElement(key); if (p) { return p->GetInteger(); } return 0; } int CPDF_Dictionary::GetInteger(const CFX_ByteStringC& key, int def) const { - CPDF_Object* p = NULL; - m_Map.Lookup(key, (void*&)p); + CPDF_Object* p = GetElement(key); if (p) { return p->GetInteger(); } return def; } FX_FLOAT CPDF_Dictionary::GetNumber(const CFX_ByteStringC& key) const { - CPDF_Object* p = NULL; - m_Map.Lookup(key, (void*&)p); + CPDF_Object* p = GetElement(key); if (p) { return p->GetNumber(); } @@ -677,8 +652,7 @@ FX_FLOAT CPDF_Dictionary::GetNumber(const CFX_ByteStringC& key) const { } FX_BOOL CPDF_Dictionary::GetBoolean(const CFX_ByteStringC& key, FX_BOOL bDefault) const { - CPDF_Object* p = NULL; - m_Map.Lookup(key, (void*&)p); + CPDF_Object* p = GetElement(key); if (ToBoolean(p)) return p->GetInteger(); return bDefault; @@ -714,67 +688,67 @@ CFX_Matrix CPDF_Dictionary::GetMatrix(const CFX_ByteStringC& key) const { return matrix; } FX_BOOL CPDF_Dictionary::KeyExist(const CFX_ByteStringC& key) const { - void* value; - return m_Map.Lookup(key, value); + return pdfium::ContainsKey(m_Map, key); } void CPDF_Dictionary::SetAt(const CFX_ByteStringC& key, CPDF_Object* pObj) { ASSERT(IsDictionary()); - void* pValue = nullptr; - m_Map.Lookup(key, pValue); - CPDF_Object* pExisting = static_cast<CPDF_Object*>(pValue); - if (pExisting == pObj) + // Avoid 2 constructions of CFX_ByteString. + CFX_ByteString key_bytestring = key; + auto it = m_Map.find(key_bytestring); + if (it == m_Map.end()) { + if (pObj) { + m_Map.insert(std::make_pair(key_bytestring, pObj)); + } return; + } - if (pExisting) - pExisting->Release(); + if (it->second == pObj) + return; + it->second->Release(); if (pObj) - m_Map.SetAt(key, pObj); + it->second = pObj; else - m_Map.RemoveKey(key); -} - -void CPDF_Dictionary::AddValue(const CFX_ByteStringC& key, CPDF_Object* pObj) { - ASSERT(m_Type == PDFOBJ_DICTIONARY); - m_Map.AddValue(key, pObj); + m_Map.erase(it); } void CPDF_Dictionary::RemoveAt(const CFX_ByteStringC& key) { ASSERT(m_Type == PDFOBJ_DICTIONARY); - CPDF_Object* p = NULL; - m_Map.Lookup(key, (void*&)p); - if (!p) { + auto it = m_Map.find(key); + if (it == m_Map.end()) return; - } - p->Release(); - m_Map.RemoveKey(key); + + it->second->Release(); + m_Map.erase(it); } void CPDF_Dictionary::ReplaceKey(const CFX_ByteStringC& oldkey, const CFX_ByteStringC& newkey) { ASSERT(m_Type == PDFOBJ_DICTIONARY); - CPDF_Object* p = NULL; - m_Map.Lookup(oldkey, (void*&)p); - if (!p) { + auto old_it = m_Map.find(oldkey); + if (old_it == m_Map.end()) return; + + // Avoid 2 constructions of CFX_ByteString. + CFX_ByteString newkey_bytestring = newkey; + auto new_it = m_Map.find(newkey_bytestring); + if (new_it != m_Map.end()) { + new_it->second->Release(); + new_it->second = old_it->second; + } else { + m_Map.insert(std::make_pair(newkey_bytestring, old_it->second)); } - m_Map.RemoveKey(oldkey); - m_Map.SetAt(newkey, p); + m_Map.erase(old_it); } FX_BOOL CPDF_Dictionary::Identical(CPDF_Dictionary* pOther) const { if (!pOther) { return FALSE; } - if (m_Map.GetCount() != pOther->m_Map.GetCount()) { + if (m_Map.size() != pOther->m_Map.size()) { return FALSE; } - FX_POSITION pos = m_Map.GetStartPosition(); - while (pos) { - CFX_ByteString key; - void* value; - m_Map.GetNextAssoc(pos, key, value); - if (!value) - return FALSE; - if (!static_cast<CPDF_Object*>(value)->IsIdentical(pOther->GetElement(key))) + for (const auto& it : m_Map) { + const CFX_ByteString& key = it.first; + if (!it.second->IsIdentical(pOther->GetElement(key))) return FALSE; } return TRUE; @@ -798,7 +772,7 @@ void CPDF_Dictionary::SetAtReference(const CFX_ByteStringC& key, void CPDF_Dictionary::AddReference(const CFX_ByteStringC& key, CPDF_IndirectObjects* pDoc, FX_DWORD objnum) { - AddValue(key, new CPDF_Reference(pDoc, objnum)); + SetAt(key, new CPDF_Reference(pDoc, objnum)); } void CPDF_Dictionary::SetAtNumber(const CFX_ByteStringC& key, FX_FLOAT f) { CPDF_Number* pNumber = new CPDF_Number; diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp index 236ecaa837..3ab4423172 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_parser.cpp @@ -869,11 +869,9 @@ FX_BOOL CPDF_Parser::RebuildCrossRef() { if (!pRoot || (pRef && IsValidObjectNumber(pRef->GetRefObjNum()) && m_ObjectInfo[pRef->GetRefObjNum()].pos != 0)) { - FX_POSITION trailer_pos = pTrailer->GetStartPos(); - while (trailer_pos) { - CFX_ByteString key; - CPDF_Object* pElement = - pTrailer->GetNextElement(trailer_pos, key); + for (const auto& it : *pTrailer) { + const CFX_ByteString& key = it.first; + CPDF_Object* pElement = it.second; FX_DWORD dwObjNum = pElement ? pElement->GetObjNum() : 0; if (dwObjNum) { @@ -2162,13 +2160,7 @@ CPDF_Object* CPDF_SyntaxParser::GetObject(CPDF_IndirectObjects* pObjList, continue; CFX_ByteStringC keyNoSlash(key.c_str() + 1, key.GetLength() - 1); - // TODO(thestig): Remove this conditional once CPDF_Dictionary has a - // better underlying map implementation. - if (nKeys < 32) { - pDict->SetAt(keyNoSlash, pObj); - } else { - pDict->AddValue(keyNoSlash, pObj); - } + pDict->SetAt(keyNoSlash, pObj); } if (IsSignatureDict(pDict.get())) { @@ -2315,8 +2307,8 @@ CPDF_Object* CPDF_SyntaxParser::GetObjectByStrict( return nullptr; } if (key.GetLength() > 1) { - pDict->AddValue(CFX_ByteStringC(key.c_str() + 1, key.GetLength() - 1), - obj.release()); + pDict->SetAt(CFX_ByteStringC(key.c_str() + 1, key.GetLength() - 1), + obj.release()); } } if (pContext) { @@ -3058,11 +3050,9 @@ FX_BOOL CPDF_DataAvail::IsObjectsAvail( if (pDict && pDict->GetString("Type") == "Page" && !bParsePage) { continue; } - FX_POSITION pos = pDict->GetStartPos(); - while (pos) { - CPDF_Object* value; - CFX_ByteString key; - value = pDict->GetNextElement(pos, key); + for (const auto& it : *pDict) { + const CFX_ByteString& key = it.first; + CPDF_Object* value = it.second; if (key != "Parent") { new_obj_array.Add(value); } diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp index ec155a8d02..1abf665d65 100644 --- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp +++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp @@ -383,10 +383,9 @@ CFX_ByteTextBuf& operator<<(CFX_ByteTextBuf& buf, const CPDF_Object* pObj) { case PDFOBJ_DICTIONARY: { const CPDF_Dictionary* p = pObj->AsDictionary(); buf << "<<"; - FX_POSITION pos = p->GetStartPos(); - while (pos) { - CFX_ByteString key; - CPDF_Object* pValue = p->GetNextElement(pos, key); + for (const auto& it : *p) { + const CFX_ByteString& key = it.first; + CPDF_Object* pValue = it.second; buf << "/" << PDF_NameEncode(key); if (pValue && pValue->GetObjNum()) { buf << " " << pValue->GetObjNum() << " 0 R "; |