#include "config.h"
#include <stdio.h>
#include <string.h>
#include "lookup.h"
#include <wtf/Assertions.h>
using namespace KJS;
static inline bool keysMatch(const UChar *c, unsigned len, const char *s)
{
const char* end = s + len;
for (; s != end; c++, s++)
if (c->uc != (unsigned char)*s)
return false;
return *s == 0;
}
static inline const HashEntry* findEntry(const struct HashTable *table, unsigned int hash,
const UChar *c, unsigned int len )
{
#ifndef NDEBUG
if (table->type != 2) {
fprintf(stderr, "KJS: Unknown hash table version.\n");
return 0;
}
#endif
ASSERT(table->hashSize != 0);
hash %= table->hashSize;
const HashEntry *e = &table->entries[hash];
if (!e->s)
return 0;
do {
if (keysMatch(c, len, e->s))
return e;
e = e->next;
} while (e);
return 0;
}
const HashEntry* Lookup::findEntry(const struct HashTable *table,
const Identifier &s )
{
const HashEntry* entry = ::findEntry(table, s.ustring().rep()->hash(), s.data(), s.size());
return entry;
}
int Lookup::find(const struct HashTable *table,
const UChar *c, unsigned int len)
{
const HashEntry *entry = ::findEntry(table, UString::Rep::computeHash(c, len), c, len);
if (entry)
return entry->value;
return -1;
}
int Lookup::find(const struct HashTable *table, const Identifier &s)
{
const HashEntry *entry = ::findEntry(table, s.ustring().rep()->hash(), s.data(), s.size());
if (entry)
return entry->value;
return -1;
}