1 | /*
|
---|
2 | * This file is part of the KDE libraries
|
---|
3 | * Copyright (C) 2003 Apple Computer, Inc
|
---|
4 | *
|
---|
5 | * This library is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU Library General Public
|
---|
7 | * License as published by the Free Software Foundation; either
|
---|
8 | * version 2 of the License, or (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This library is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
13 | * Library General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU Library General Public License
|
---|
16 | * along with this library; see the file COPYING.LIB. If not, write to
|
---|
17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
---|
18 | * Boston, MA 02110-1301, USA.
|
---|
19 | *
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include "config.h"
|
---|
23 |
|
---|
24 | #include "identifier.h"
|
---|
25 |
|
---|
26 | #include "JSLock.h"
|
---|
27 | #include <new> // for placement new
|
---|
28 | #include <string.h> // for strlen
|
---|
29 | #include <wtf/Assertions.h>
|
---|
30 | #include <wtf/FastMalloc.h>
|
---|
31 | #include <wtf/HashSet.h>
|
---|
32 |
|
---|
33 | namespace WTF {
|
---|
34 |
|
---|
35 | template<typename T> struct DefaultHash;
|
---|
36 | template<typename T> struct StrHash;
|
---|
37 |
|
---|
38 | template<> struct StrHash<KJS::UString::Rep *> {
|
---|
39 | static unsigned hash(const KJS::UString::Rep *key) { return key->hash(); }
|
---|
40 | static bool equal(const KJS::UString::Rep *a, const KJS::UString::Rep *b) { return KJS::Identifier::equal(a, b); }
|
---|
41 | };
|
---|
42 |
|
---|
43 | template<> struct DefaultHash<KJS::UString::Rep *> {
|
---|
44 | typedef StrHash<KJS::UString::Rep *> Hash;
|
---|
45 | };
|
---|
46 |
|
---|
47 | }
|
---|
48 |
|
---|
49 | namespace KJS {
|
---|
50 |
|
---|
51 | typedef HashSet<UString::Rep *> IdentifierTable;
|
---|
52 | static IdentifierTable *table;
|
---|
53 |
|
---|
54 | static inline IdentifierTable& identifierTable()
|
---|
55 | {
|
---|
56 | ASSERT(JSLock::lockCount() > 0);
|
---|
57 |
|
---|
58 | if (!table)
|
---|
59 | table = new IdentifierTable;
|
---|
60 | return *table;
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | bool Identifier::equal(const UString::Rep *r, const char *s)
|
---|
65 | {
|
---|
66 | int length = r->len;
|
---|
67 | const UChar *d = r->data();
|
---|
68 | for (int i = 0; i != length; ++i)
|
---|
69 | if (d[i].uc != (unsigned char)s[i])
|
---|
70 | return false;
|
---|
71 | return s[length] == 0;
|
---|
72 | }
|
---|
73 |
|
---|
74 | bool Identifier::equal(const UString::Rep *r, const UChar *s, int length)
|
---|
75 | {
|
---|
76 | if (r->len != length)
|
---|
77 | return false;
|
---|
78 | const UChar *d = r->data();
|
---|
79 | for (int i = 0; i != length; ++i)
|
---|
80 | if (d[i].uc != s[i].uc)
|
---|
81 | return false;
|
---|
82 | return true;
|
---|
83 | }
|
---|
84 |
|
---|
85 | bool Identifier::equal(const UString::Rep *r, const UString::Rep *b)
|
---|
86 | {
|
---|
87 | int length = r->len;
|
---|
88 | if (length != b->len)
|
---|
89 | return false;
|
---|
90 | const UChar *d = r->data();
|
---|
91 | const UChar *s = b->data();
|
---|
92 | for (int i = 0; i != length; ++i)
|
---|
93 | if (d[i].uc != s[i].uc)
|
---|
94 | return false;
|
---|
95 | return true;
|
---|
96 | }
|
---|
97 |
|
---|
98 | struct CStringTranslator
|
---|
99 | {
|
---|
100 | static unsigned hash(const char *c)
|
---|
101 | {
|
---|
102 | return UString::Rep::computeHash(c);
|
---|
103 | }
|
---|
104 |
|
---|
105 | static bool equal(UString::Rep *r, const char *s)
|
---|
106 | {
|
---|
107 | return Identifier::equal(r, s);
|
---|
108 | }
|
---|
109 |
|
---|
110 | static void translate(UString::Rep*& location, const char *c, unsigned hash)
|
---|
111 | {
|
---|
112 | size_t length = strlen(c);
|
---|
113 | UChar *d = static_cast<UChar *>(fastMalloc(sizeof(UChar) * length));
|
---|
114 | for (size_t i = 0; i != length; i++)
|
---|
115 | d[i] = c[i];
|
---|
116 |
|
---|
117 | UString::Rep *r = UString::Rep::create(d, static_cast<int>(length)).releaseRef();
|
---|
118 | r->isIdentifier = 1;
|
---|
119 | r->rc = 0;
|
---|
120 | r->_hash = hash;
|
---|
121 |
|
---|
122 | location = r;
|
---|
123 | }
|
---|
124 | };
|
---|
125 |
|
---|
126 | PassRefPtr<UString::Rep> Identifier::add(const char *c)
|
---|
127 | {
|
---|
128 | if (!c)
|
---|
129 | return &UString::Rep::null;
|
---|
130 | size_t length = strlen(c);
|
---|
131 | if (length == 0)
|
---|
132 | return &UString::Rep::empty;
|
---|
133 |
|
---|
134 | return *identifierTable().add<const char *, CStringTranslator>(c).first;
|
---|
135 | }
|
---|
136 |
|
---|
137 | struct UCharBuffer {
|
---|
138 | const UChar *s;
|
---|
139 | unsigned int length;
|
---|
140 | };
|
---|
141 |
|
---|
142 | struct UCharBufferTranslator
|
---|
143 | {
|
---|
144 | static unsigned hash(const UCharBuffer& buf)
|
---|
145 | {
|
---|
146 | return UString::Rep::computeHash(buf.s, buf.length);
|
---|
147 | }
|
---|
148 |
|
---|
149 | static bool equal(UString::Rep *str, const UCharBuffer& buf)
|
---|
150 | {
|
---|
151 | return Identifier::equal(str, buf.s, buf.length);
|
---|
152 | }
|
---|
153 |
|
---|
154 | static void translate(UString::Rep *& location, const UCharBuffer& buf, unsigned hash)
|
---|
155 | {
|
---|
156 | UChar *d = static_cast<UChar *>(fastMalloc(sizeof(UChar) * buf.length));
|
---|
157 | for (unsigned i = 0; i != buf.length; i++)
|
---|
158 | d[i] = buf.s[i];
|
---|
159 |
|
---|
160 | UString::Rep *r = UString::Rep::create(d, buf.length).releaseRef();
|
---|
161 | r->isIdentifier = 1;
|
---|
162 | r->rc = 0;
|
---|
163 | r->_hash = hash;
|
---|
164 |
|
---|
165 | location = r;
|
---|
166 | }
|
---|
167 | };
|
---|
168 |
|
---|
169 | PassRefPtr<UString::Rep> Identifier::add(const UChar *s, int length)
|
---|
170 | {
|
---|
171 | if (length == 0)
|
---|
172 | return &UString::Rep::empty;
|
---|
173 |
|
---|
174 | UCharBuffer buf = {s, length};
|
---|
175 | return *identifierTable().add<UCharBuffer, UCharBufferTranslator>(buf).first;
|
---|
176 | }
|
---|
177 |
|
---|
178 | PassRefPtr<UString::Rep> Identifier::add(UString::Rep *r)
|
---|
179 | {
|
---|
180 | if (r->isIdentifier)
|
---|
181 | return r;
|
---|
182 |
|
---|
183 | if (r->len == 0)
|
---|
184 | return &UString::Rep::empty;
|
---|
185 |
|
---|
186 | UString::Rep *result = *identifierTable().add(r).first;
|
---|
187 | if (result == r)
|
---|
188 | r->isIdentifier = true;
|
---|
189 | return result;
|
---|
190 | }
|
---|
191 |
|
---|
192 | void Identifier::remove(UString::Rep *r)
|
---|
193 | {
|
---|
194 | identifierTable().remove(r);
|
---|
195 | }
|
---|
196 |
|
---|
197 | } // namespace KJS
|
---|