lookup.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stdio.h>
00024 #include <string.h>
00025
00026 #include "lookup.h"
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032 using namespace KJS;
00033
00034 const HashEntry* Lookup::findEntry( const struct HashTable *table,
00035 const UChar *c, unsigned int len )
00036 {
00037 if (table->type != 2) {
00038 fprintf(stderr, "KJS: Unknown hash table version.\n");
00039 return 0;
00040 }
00041 char *ascii = new char[len+1];
00042 unsigned int i;
00043 for(i = 0; i < len; i++, c++) {
00044 if (!c->high())
00045 ascii[i] = c->low();
00046 else
00047 break;
00048 }
00049 ascii[i] = '\0';
00050
00051 int h = hash(ascii) % table->hashSize;
00052 const HashEntry *e = &table->entries[h];
00053
00054
00055 if (!e->s) {
00056 delete [] ascii;
00057 return 0;
00058 }
00059
00060 do {
00061
00062 if (strcmp(ascii, e->s) == 0) {
00063 delete [] ascii;
00064 return e;
00065 }
00066
00067 e = e->next;
00068 } while (e);
00069
00070 delete [] ascii;
00071 return 0;
00072 }
00073
00074 const HashEntry* Lookup::findEntry( const struct HashTable *table,
00075 const UString &s )
00076 {
00077 return findEntry( table, s.data(), s.size() );
00078 }
00079
00080 int Lookup::find(const struct HashTable *table,
00081 const UChar *c, unsigned int len)
00082 {
00083 const HashEntry *entry = findEntry( table, c, len );
00084 if (entry)
00085 return entry->value;
00086 return -1;
00087 }
00088
00089 int Lookup::find(const struct HashTable *table, const UString &s)
00090 {
00091 return find(table, s.data(), s.size());
00092 }
00093
00094 unsigned int Lookup::hash(const UChar *c, unsigned int len)
00095 {
00096 unsigned int val = 0;
00097
00098 for (unsigned int i = 0; i < len; i++, c++)
00099 val += c->low();
00100
00101 return val;
00102 }
00103
00104 unsigned int Lookup::hash(const UString &key)
00105 {
00106 return hash(key.data(), key.size());
00107 }
00108
00109 unsigned int Lookup::hash(const char *s)
00110 {
00111 unsigned int val = 0;
00112 while (*s)
00113 val += *s++;
00114
00115 return val;
00116 }
This file is part of the documentation for kdelibs Version 3.1.4.