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 "identifier.h" |
23 | #include <config-kjs.h> |
24 | |
25 | #include <wtf/FastMalloc.h> |
26 | #include <wtf/HashSet.h> |
27 | #include <string.h> // for strlen |
28 | #include <new> // for placement new |
29 | |
30 | namespace KJS { |
31 | |
32 | typedef HashSet<UString::Rep *> IdentifierTable; |
33 | static IdentifierTable *table; |
34 | |
35 | static inline IdentifierTable& identifierTable() |
36 | { |
37 | if (!table) |
38 | table = new IdentifierTable; |
39 | return *table; |
40 | } |
41 | |
42 | bool Identifier::equal(const UString::Rep *r, const char *s) |
43 | { |
44 | int length = r->len; |
45 | const UChar *d = r->data(); |
46 | for (int i = 0; i != length; ++i) |
47 | if (d[i].uc != (unsigned char)s[i]) |
48 | return false; |
49 | return s[length] == 0; |
50 | } |
51 | |
52 | bool Identifier::equal(const UString::Rep *r, const UChar *s, int length) |
53 | { |
54 | if (r->len != length) |
55 | return false; |
56 | const UChar *d = r->data(); |
57 | for (int i = 0; i != length; ++i) |
58 | if (d[i].uc != s[i].uc) |
59 | return false; |
60 | return true; |
61 | } |
62 | |
63 | struct CStringTranslator |
64 | { |
65 | static unsigned hash(const char *c) |
66 | { |
67 | return UString::Rep::computeHash(c); |
68 | } |
69 | |
70 | static bool equal(UString::Rep *r, const char *s) |
71 | { |
72 | return Identifier::equal(r, s); |
73 | } |
74 | |
75 | static void translate(UString::Rep*& location, const char *c, unsigned hash) |
76 | { |
77 | size_t length = strlen(c); |
78 | UChar *d = static_cast<UChar *>(fastMalloc(sizeof(UChar) * length)); |
79 | for (size_t i = 0; i != length; i++) |
80 | d[i] = c[i]; |
81 | |
82 | UString::Rep *r = UString::Rep::create(d, static_cast<int>(length)).releaseRef(); |
83 | r->isIdentifier = 1; |
84 | r->rc = 0; |
85 | r->_hash = hash; |
86 | |
87 | location = r; |
88 | } |
89 | }; |
90 | |
91 | PassRefPtr<UString::Rep> Identifier::add(const char *c) |
92 | { |
93 | if (!c) { |
94 | UString::Rep::null.hash(); |
95 | return &UString::Rep::null; |
96 | } |
97 | |
98 | if (!c[0]) { |
99 | UString::Rep::empty.hash(); |
100 | return &UString::Rep::empty; |
101 | } |
102 | |
103 | return *identifierTable().add<const char *, CStringTranslator>(c).first; |
104 | } |
105 | |
106 | struct UCharBuffer { |
107 | const UChar *s; |
108 | unsigned int length; |
109 | }; |
110 | |
111 | struct UCharBufferTranslator |
112 | { |
113 | static unsigned hash(const UCharBuffer& buf) |
114 | { |
115 | return UString::Rep::computeHash(buf.s, buf.length); |
116 | } |
117 | |
118 | static bool equal(UString::Rep *str, const UCharBuffer& buf) |
119 | { |
120 | return Identifier::equal(str, buf.s, buf.length); |
121 | } |
122 | |
123 | static void translate(UString::Rep *& location, const UCharBuffer& buf, unsigned hash) |
124 | { |
125 | UChar *d = static_cast<UChar *>(fastMalloc(sizeof(UChar) * buf.length)); |
126 | for (unsigned i = 0; i != buf.length; i++) |
127 | d[i] = buf.s[i]; |
128 | |
129 | UString::Rep *r = UString::Rep::create(d, buf.length).releaseRef(); |
130 | r->isIdentifier = 1; |
131 | r->rc = 0; |
132 | r->_hash = hash; |
133 | |
134 | location = r; |
135 | } |
136 | }; |
137 | |
138 | PassRefPtr<UString::Rep> Identifier::add(const UChar *s, int length) |
139 | { |
140 | if (!length) { |
141 | UString::Rep::empty.hash(); |
142 | return &UString::Rep::empty; |
143 | } |
144 | |
145 | UCharBuffer buf = {s, static_cast<unsigned int>(length)}; |
146 | return *identifierTable().add<UCharBuffer, UCharBufferTranslator>(buf).first; |
147 | } |
148 | |
149 | PassRefPtr<UString::Rep> Identifier::addSlowCase(UString::Rep *r) |
150 | { |
151 | assert(!r->isIdentifier); |
152 | |
153 | if (r->len == 0) { |
154 | UString::Rep::empty.hash(); |
155 | return &UString::Rep::empty; |
156 | } |
157 | |
158 | UString::Rep *result = *identifierTable().add(r).first; |
159 | if (result == r) |
160 | r->isIdentifier = true; |
161 | return result; |
162 | } |
163 | |
164 | void Identifier::remove(UString::Rep *r) |
165 | { |
166 | identifierTable().remove(r); |
167 | } |
168 | |
169 | } // namespace KJS |
170 | |