fix logic
[personal-kdelibs.git] / kjs / lookup.cpp
blob6ed9a291f3d3910e646e048e5b192316073cbb59
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.
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.
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.
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
23 #include "lookup.h"
25 #include <config.h>
26 #include <stdio.h>
27 #include <string.h>
29 #include <wtf/Assertions.h>
31 using namespace KJS;
33 static inline bool keysMatch(const UChar *c, unsigned len, const char *s)
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;
42 static inline const HashEntry* findEntry(const struct HashTable *table, unsigned int hash,
43 const UChar *c, unsigned int len )
45 #ifndef NDEBUG
46 if (table->type != 2) {
47 fprintf(stderr, "KJS: Unknown hash table version.\n");
48 return 0;
50 #endif
51 ASSERT(table->hashSize != 0);
53 hash %= table->hashSize;
55 const HashEntry *e = &table->entries[hash];
57 // empty bucket ?
58 if (!e->s)
59 return 0;
61 do {
62 // compare strings
63 if (keysMatch(c, len, e->s))
64 return e;
66 // try next bucket
67 e = e->next;
68 } while (e);
69 return 0;
72 const HashEntry* Lookup::findEntry(const struct HashTable *table,
73 const Identifier &s )
75 const HashEntry* entry = ::findEntry(table, s.ustring().rep()->hash(), s.data(), s.size());
76 return entry;
79 int Lookup::find(const struct HashTable *table,
80 const UChar *c, unsigned int len)
82 const HashEntry *entry = ::findEntry(table, UString::Rep::computeHash(c, len), c, len);
83 if (entry)
84 return entry->value;
85 return -1;
88 int Lookup::find(const struct HashTable *table, const Identifier &s)
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;