1 | // -*- c-basic-offset: 2 -*-
|
---|
2 | /*
|
---|
3 | * This file is part of the KDE libraries
|
---|
4 | * Copyright (C) 1999-2000 Harri Porten ([email protected])
|
---|
5 | * Copyright (C) 2003 Apple Computer, Inc.
|
---|
6 | *
|
---|
7 | * This library is free software; you can redistribute it and/or
|
---|
8 | * modify it under the terms of the GNU Lesser General Public
|
---|
9 | * License as published by the Free Software Foundation; either
|
---|
10 | * version 2 of the License, or (at your option) any later version.
|
---|
11 | *
|
---|
12 | * This library is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
15 | * Lesser General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU Lesser General Public
|
---|
18 | * License along with this library; if not, write to the Free Software
|
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
20 | *
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include "config.h"
|
---|
24 | #include <stdio.h>
|
---|
25 | #include <string.h>
|
---|
26 |
|
---|
27 | #include "lookup.h"
|
---|
28 | #include <wtf/Assertions.h>
|
---|
29 |
|
---|
30 | using namespace KJS;
|
---|
31 |
|
---|
32 | static inline bool keysMatch(const UChar *c, unsigned len, const char *s)
|
---|
33 | {
|
---|
34 | const char* end = s + len;
|
---|
35 | for (; s != end; c++, s++)
|
---|
36 | if (c->uc != (unsigned char)*s)
|
---|
37 | return false;
|
---|
38 | return *s == 0;
|
---|
39 | }
|
---|
40 |
|
---|
41 | static inline const HashEntry* findEntry(const struct HashTable *table, unsigned int hash,
|
---|
42 | const UChar *c, unsigned int len )
|
---|
43 | {
|
---|
44 | #ifndef NDEBUG
|
---|
45 | if (table->type != 2) {
|
---|
46 | fprintf(stderr, "KJS: Unknown hash table version.\n");
|
---|
47 | return 0;
|
---|
48 | }
|
---|
49 | #endif
|
---|
50 | ASSERT(table->hashSize != 0);
|
---|
51 |
|
---|
52 | hash %= table->hashSize;
|
---|
53 |
|
---|
54 | const HashEntry *e = &table->entries[hash];
|
---|
55 |
|
---|
56 | // empty bucket ?
|
---|
57 | if (!e->s)
|
---|
58 | return 0;
|
---|
59 |
|
---|
60 | do {
|
---|
61 | // compare strings
|
---|
62 | if (keysMatch(c, len, e->s))
|
---|
63 | return e;
|
---|
64 |
|
---|
65 | // try next bucket
|
---|
66 | e = e->next;
|
---|
67 | } while (e);
|
---|
68 | return 0;
|
---|
69 | }
|
---|
70 |
|
---|
71 | const HashEntry* Lookup::findEntry(const struct HashTable *table,
|
---|
72 | const Identifier &s )
|
---|
73 | {
|
---|
74 | const HashEntry* entry = ::findEntry(table, s.ustring().rep()->hash(), s.data(), s.size());
|
---|
75 | return entry;
|
---|
76 | }
|
---|
77 |
|
---|
78 | int Lookup::find(const struct HashTable *table,
|
---|
79 | const UChar *c, unsigned int len)
|
---|
80 | {
|
---|
81 | const HashEntry *entry = ::findEntry(table, UString::Rep::computeHash(c, len), c, len);
|
---|
82 | if (entry)
|
---|
83 | return entry->value;
|
---|
84 | return -1;
|
---|
85 | }
|
---|
86 |
|
---|
87 | int Lookup::find(const struct HashTable *table, const Identifier &s)
|
---|
88 | {
|
---|
89 | //printf("looking for:%s\n", s.ascii());
|
---|
90 | const HashEntry *entry = ::findEntry(table, s.ustring().rep()->hash(), s.data(), s.size());
|
---|
91 | if (entry)
|
---|
92 | return entry->value;
|
---|
93 | return -1;
|
---|
94 | }
|
---|