File indexing completed on 2024-05-12 15:43:26
0001 /* 0002 * This file is part of the KDE libraries 0003 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) 0004 * Copyright (C) 2003 Apple Computer, Inc. 0005 * 0006 * This library is free software; you can redistribute it and/or 0007 * modify it under the terms of the GNU Lesser General Public 0008 * License as published by the Free Software Foundation; either 0009 * version 2 of the License, or (at your option) any later version. 0010 * 0011 * This library is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0014 * Lesser General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU Lesser General Public 0017 * License along with this library; if not, write to the Free Software 0018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 0019 * 0020 */ 0021 0022 #include "lookup.h" 0023 0024 #include <string.h> 0025 0026 #include <wtf/Assertions.h> 0027 0028 using namespace KJS; 0029 0030 static inline bool keysMatch(const UChar *c, unsigned len, const char *s) 0031 { 0032 const char *end = s + len; 0033 for (; s != end; c++, s++) 0034 if (c->uc != (unsigned char)*s) { 0035 return false; 0036 } 0037 return *s == 0; 0038 } 0039 0040 static inline const HashEntry *findEntry(const struct HashTable *table, unsigned int hash, 0041 const UChar *c, unsigned int len) 0042 { 0043 #ifndef NDEBUG 0044 if (table->type != 2) { 0045 fprintf(stderr, "KJS: Unknown hash table version.\n"); 0046 return nullptr; 0047 } 0048 #endif 0049 ASSERT(table->hashSize != 0); 0050 0051 hash %= table->hashSize; 0052 0053 const HashEntry *e = &table->entries[hash]; 0054 0055 // empty bucket ? 0056 if (!e->s) { 0057 return nullptr; 0058 } 0059 0060 do { 0061 // compare strings 0062 if (keysMatch(c, len, e->s)) { 0063 return e; 0064 } 0065 0066 // try next bucket 0067 e = e->next; 0068 } while (e); 0069 return nullptr; 0070 } 0071 0072 const HashEntry *Lookup::findEntry(const struct HashTable *table, 0073 const Identifier &s) 0074 { 0075 const HashEntry *entry = ::findEntry(table, s.ustring().rep()->hash(), s.data(), s.size()); 0076 return entry; 0077 } 0078 0079 int Lookup::find(const struct HashTable *table, 0080 const UChar *c, unsigned int len) 0081 { 0082 const HashEntry *entry = ::findEntry(table, UString::Rep::computeHash(c, len), c, len); 0083 if (entry) { 0084 return entry->value; 0085 } 0086 return -1; 0087 } 0088 0089 int Lookup::find(const struct HashTable *table, const Identifier &s) 0090 { 0091 //printf("looking for:%s\n", s.ascii()); 0092 const HashEntry *entry = ::findEntry(table, s.ustring().rep()->hash(), s.data(), s.size()); 0093 if (entry) { 0094 return entry->value; 0095 } 0096 return -1; 0097 }