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