1// -*- c-basic-offset: 2 -*-
2/*
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
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 "lookup.h"
24
25#include <config-kjs.h>
26#include <stdio.h>
27#include <string.h>
28
29#include <wtf/Assertions.h>
30
31using namespace KJS;
32
33static inline bool keysMatch(const UChar *c, unsigned len, const char *s)
34{
35 const char* end = s + len;
36 for (; s != end; c++, s++)
37 if (c->uc != (unsigned char)*s)
38 return false;
39 return *s == 0;
40}
41
42static inline const HashEntry* findEntry(const struct HashTable *table, unsigned int hash,
43 const UChar *c, unsigned int len )
44{
45#ifndef NDEBUG
46 if (table->type != 2) {
47 fprintf(stderr, "KJS: Unknown hash table version.\n");
48 return 0;
49 }
50#endif
51 ASSERT(table->hashSize != 0);
52
53 hash %= table->hashSize;
54
55 const HashEntry *e = &table->entries[hash];
56
57 // empty bucket ?
58 if (!e->s)
59 return 0;
60
61 do {
62 // compare strings
63 if (keysMatch(c, len, e->s))
64 return e;
65
66 // try next bucket
67 e = e->next;
68 } while (e);
69 return 0;
70}
71
72const HashEntry* Lookup::findEntry(const struct HashTable *table,
73 const Identifier &s )
74{
75 const HashEntry* entry = ::findEntry(table, s.ustring().rep()->hash(), s.data(), s.size());
76 return entry;
77}
78
79int Lookup::find(const struct HashTable *table,
80 const UChar *c, unsigned int len)
81{
82 const HashEntry *entry = ::findEntry(table, UString::Rep::computeHash(c, len), c, len);
83 if (entry)
84 return entry->value;
85 return -1;
86}
87
88int Lookup::find(const struct HashTable *table, const Identifier &s)
89{
90 //printf("looking for:%s\n", s.ascii());
91 const HashEntry *entry = ::findEntry(table, s.ustring().rep()->hash(), s.data(), s.size());
92 if (entry)
93 return entry->value;
94 return -1;
95}
96