KHtml

idstring.cpp
1 /*
2  * This file is part of the DOM implementation for KDE.
3  *
4  * Copyright (C) 2008, 2009 Maksim Orlovich ([email protected])
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "idstring.h"
24 #include <assert.h>
25 
26 namespace khtml
27 {
28 
29 CaseNormalizeMode IDTableBase::MappingKey::caseNormalizationMode;
30 
31 bool IDTableBase::MappingKey::operator==(const MappingKey &other) const
32 {
33  if (IDTableBase::MappingKey::caseNormalizationMode == IDS_CaseSensitive) {
34  return !strcmp(str, other.str);
35  } else {
36  return !strcasecmp(str, other.str);
37  }
38 }
39 
40 static inline unsigned int qHash(const IDTableBase::MappingKey &key)
41 {
42  if (!key.str) {
43  return 82610334; //same as empty
44  } else if (key.caseNormalizationMode == IDS_CaseSensitive) {
45  return key.str->hash();
46  } else if (key.caseNormalizationMode == IDS_NormalizeLower) {
47  return key.str->lowerHash();
48  } else { // caseNormalizationMode == IDS_NormalizeUpper
49  return key.str->upperHash();
50  }
51 }
52 
53 void IDTableBase::releaseId(unsigned id)
54 {
55  IDTableBase::MappingKey::caseNormalizationMode = IDS_CaseSensitive;
56 
57  m_mappingLookup.remove(m_mappings[id].name);
58  m_mappings[id].name->deref();
59  m_idFreeList.append(id);
60 }
61 
62 unsigned short IDTableBase::grabId(DOMStringImpl *origName, CaseNormalizeMode cnm)
63 {
64  unsigned short newId;
65 
66  // Check for existing one, ignoring case if needed
67  IDTableBase::MappingKey::caseNormalizationMode = cnm;
69  if (i != m_mappingLookup.constEnd()) {
70  newId = *i;
71  refId(newId);
72  return newId;
73  }
74 
75  // Nope. Allocate new ID. If there is normalization going on, we may now have to
76  // update our case so the canonical mapping is of the expected case. We
77  // may also have to deep-copy
78  DOMStringImpl *name = nullptr;
79  switch (cnm) {
80  case IDS_CaseSensitive:
81  if (origName->m_shallowCopy) {
82  // Create a new copy of the data since we may be extending its
83  // lifetime indefinitely
84  name = new DOMStringImpl(origName->s, origName->l);
85  name->m_hash = origName->m_hash;
86  } else {
87  name = origName;
88  }
89  break;
90  case IDS_NormalizeUpper:
91  name = origName->upper();
92  break;
93  case IDS_NormalizeLower:
94  name = origName->lower();
95  break;
96  }
97 
98  Q_ASSERT(name);
99  name->ref();
100 
101  if (!m_idFreeList.isEmpty()) {
102  // Grab from freelist..
103  newId = m_idFreeList.last();
104  m_idFreeList.removeLast();
105  m_mappings[newId].name = name;
106  } else {
107  // Make a new one --- if we can (we keep one spot for "last resort" mapping)
108  if (m_mappings.size() < 0xFFFE) {
109  m_mappings.append(Mapping(name));
110  newId = m_mappings.size() - 1;
111  } else {
112  // We ran out of resources. Did we add a fallback mapping yet?
113  // We use the same one for everything; and don't even keep track
114  // of what it may go to, as we will have no way of freeing
115  // the aliases. In particular, this means we no longer need the name..
116  name->deref();
117 
118  if (m_mappings.size() == 0xFFFE) {
119  // Need a new mapping..
120  name = new DOMStringImpl("_khtml_fallback");
121  m_mappings.append(Mapping(name));
122  m_mappings[0xFFFF].refCount = 1; // pin it.
123  name->ref();
124  } else {
125  name = m_mappings[0xFFFF].name; // No need to ref the name
126  // here as the entry is eternal anyway
127  }
128  newId = 0xFFFF;
129  }
130  }
131 
132  m_mappingLookup[name] = newId;
133 
134  refId(newId);
135  return newId;
136 }
137 
138 void IDTableBase::addStaticMapping(unsigned id, const DOMString &name)
139 {
140  addHiddenMapping(id, name);
141  IDTableBase::MappingKey::caseNormalizationMode = IDS_CaseSensitive;
142  m_mappingLookup[name.implementation()] = id;
143 }
144 
145 void IDTableBase::addHiddenMapping(unsigned id, const DOMString &name)
146 {
147  DOMStringImpl *nameImpl = name.implementation();
148  if (nameImpl) {
149  nameImpl->ref();
150  }
151 
152  if (id >= m_mappings.size()) {
153  m_mappings.resize(id + 1);
154  }
155  m_mappings[id] = Mapping(nameImpl);
156  m_mappings[id].refCount = 1; // Pin it.
157 }
158 
159 }
160 
This file is part of the HTML rendering engine for KDE.
QHash::const_iterator constFind(const Key &key) const const
QHash::const_iterator constEnd() const const
This class implements the basic string we use in the DOM.
Definition: dom_string.h:44
KCALENDARCORE_EXPORT uint qHash(const KCalendarCore::Period &key)
DOMStringImpl * implementation() const
Definition: dom_string.h:145
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Oct 26 2021 22:48:03 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.