lookup.cpp
// -*- c-basic-offset: 2 -*-
/*
* This file is part of the KDE libraries
* Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
* Copyright (C) 2003 Apple Computer, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <stdio.h>
#include <string.h>
#include "lookup.h"
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
using namespace KJS;
static bool keysMatch(const UChar *c, unsigned len, const char *s)
{
for (unsigned i = 0; i != len; i++, c++, s++)
if (c->uc != (unsigned char)*s)
return false;
return *s == 0;
}
const HashEntry* Lookup::findEntry( const struct HashTable *table,
const UChar *c, unsigned int len )
{
#ifndef NDEBUG
if (table->type != 2) {
fprintf(stderr, "KJS: Unknown hash table version.\n");
return 0;
}
#endif
int h = hash(c, len) % table->hashSize;
const HashEntry *e = &table->entries[h];
// empty bucket ?
if (!e->soffset)
return 0;
while(1) {
// compare strings
if (keysMatch(c, len, &table->sbase[e->soffset]))
return e;
// try next bucket
if(e->next < 0) break;
e = &table->entries[e->next];
}
return 0;
}
const HashEntry* Lookup::findEntry( const struct HashTable *table,
const Identifier &s )
{
return findEntry( table, s.data(), s.size() );
}
int Lookup::find(const struct HashTable *table,
const UChar *c, unsigned int len)
{
const HashEntry *entry = findEntry( table, c, len );
if (entry)
return entry->value;
return -1;
}
int Lookup::find(const struct HashTable *table, const Identifier &s)
{
return find(table, s.data(), s.size());
}
unsigned int Lookup::hash(const UChar *c, unsigned int len)
{
unsigned int val = 0;
// ignoring higher byte
for (unsigned int i = 0; i < len; i++, c++)
val += c->low();
return val;
}
unsigned int Lookup::hash(const Identifier &key)
{
return hash(key.data(), key.size());
}
unsigned int Lookup::hash(const char *s)
{
unsigned int val = 0;
while (*s)
val += *s++;
return val;
}
KJS::HashTable::sbase
const char *const sbase
pointer to the string table.
Definition: lookup.h:96
KJS::HashEntry::value
short int value
value is the result value (usually an enum value)
Definition: lookup.h:45
KJS::HashTable::type
int type
type is a version number.
Definition: lookup.h:76
KJS::Identifier::data
const UChar * data() const
returns a UChar pointer to the string of the identifier with a size defined by size().
Definition: identifier.h:62
KJS::Lookup::hash
static unsigned int hash(const Identifier &key)
Calculate the hash value for a given key.
Definition: lookup.cpp:102
KJS::HashEntry
An entry in a hash table.
Definition: lookup.h:37
KJS::HashTable::hashSize
int hashSize
the maximum value for the hash.
Definition: lookup.h:91
KJS::HashTable
A hash table Usually the hashtable is generated by the create_hash_table script, from a ...
Definition: lookup.h:72
KJS::HashEntry::soffset
unsigned short soffset
s is the offset to the string key (e.g.
Definition: lookup.h:41
KJS::HashEntry::next
short next
next is the index to the next entry for the same hash value
Definition: lookup.h:58
KJS::UChar::low
unsigned char low() const
Definition: ustring.h:77
KJS::UChar
Unicode character.
Definition: ustring.h:51
KJS::Lookup::findEntry
static const HashEntry * findEntry(const struct HashTable *table, const Identifier &s)
Find an entry in the table, and return the entry This variant gives access to the other attributes of...
Definition: lookup.cpp:72
KJS::Identifier::size
int size() const
The size of the UChar string returned.
Definition: identifier.h:66
KJS::Lookup::find
static int find(const struct HashTable *table, const Identifier &s)
Find an entry in the table, and return its value (i.e.
Definition: lookup.cpp:87
KJS::HashTable::entries
const HashEntry *const entries
pointer to the array of entries Mind that some entries in the array are null (0,0,0,0).
Definition: lookup.h:87
KJS::Identifier
Represents an Identifier for a Javascript object.
Definition: identifier.h:32

KDE Home | KDE Accessibility Home | Description of Access Keys