]> git.saurik.com Git - apple/javascriptcore.git/blob - kjs/lookup.cpp
JavaScriptCore-466.1.tar.gz
[apple/javascriptcore.git] / kjs / lookup.cpp
1 // -*- c-basic-offset: 2 -*-
2 /*
3 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
4 * Copyright (C) 2003, 2007 Apple Inc. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser 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 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22 #include "config.h"
23 #include "lookup.h"
24
25 #include <wtf/Assertions.h>
26
27 namespace KJS {
28
29 static inline bool keysMatch(const UChar* c, unsigned len, const char* s)
30 {
31 // FIXME: This can run off the end of |s| if |c| has a U+0000 character in it.
32 const char* end = s + len;
33 for (; s != end; c++, s++)
34 if (c->uc != *s)
35 return false;
36 return *s == 0;
37 }
38
39 static inline const HashEntry* findEntry(const struct HashTable* table, unsigned int hash,
40 const UChar* c, unsigned int len)
41 {
42 ASSERT(table->type == 3);
43
44 const HashEntry* e = &table->entries[hash & table->hashSizeMask];
45
46 if (!e->s)
47 return 0;
48
49 do {
50 // compare strings
51 if (keysMatch(c, len, e->s))
52 return e;
53
54 // try next bucket
55 e = e->next;
56 } while (e);
57 return 0;
58 }
59
60 const HashEntry* Lookup::findEntry(const struct HashTable* table, const Identifier& s)
61 {
62 return KJS::findEntry(table, s.ustring().rep()->computedHash(), s.data(), s.size());
63 }
64
65 int Lookup::find(const struct HashTable *table, const UChar *c, unsigned int len)
66 {
67 const HashEntry *entry = KJS::findEntry(table, UString::Rep::computeHash(c, len), c, len);
68 if (entry)
69 return entry->value.intValue;
70 return -1;
71 }
72
73 int Lookup::find(const struct HashTable* table, const Identifier& s)
74 {
75 const HashEntry* entry = KJS::findEntry(table, s.ustring().rep()->computedHash(), s.data(), s.size());
76 if (entry)
77 return entry->value.intValue;
78 return -1;
79 }
80
81 }