source: webkit/trunk/JavaScriptCore/kjs/lookup.cpp@ 10563

Last change on this file since 10563 was 9768, checked in by ggaren, 20 years ago

-rolled in patches for http://bugzilla.opendarwin.org/show_bug.cgi?id=3945
[PATCH] Safe merges of comments and other trivialities from KDE's kjs

-patch by Martijn Klingens <[email protected]>

  • kjs/array_instance.h:
  • kjs/array_object.cpp:
  • kjs/array_object.h:
  • kjs/bool_object.cpp:
  • kjs/bool_object.h:
  • kjs/collector.cpp:
  • kjs/collector.h:
  • kjs/completion.h:
  • kjs/context.h:
  • kjs/date_object.cpp:
  • kjs/date_object.h:
  • kjs/debugger.cpp:
  • kjs/debugger.h:
  • kjs/dtoa.h:
  • kjs/error_object.cpp:
  • kjs/error_object.h:
  • kjs/function.cpp:
  • kjs/function.h:
  • kjs/function_object.cpp:
  • kjs/function_object.h:
  • kjs/grammar.y:
  • kjs/identifier.cpp:
  • kjs/identifier.h:
  • kjs/internal.cpp:
  • kjs/internal.h:
  • kjs/interpreter.cpp:
  • kjs/interpreter.h:
  • kjs/interpreter_map.cpp:
  • kjs/interpreter_map.h:
  • kjs/lexer.cpp:
  • kjs/lexer.h:
  • kjs/list.cpp:
  • kjs/list.h:
  • kjs/lookup.cpp:
  • kjs/lookup.h:
  • kjs/math_object.cpp:
  • kjs/math_object.h:
  • kjs/nodes.cpp:
  • kjs/nodes.h:
  • kjs/nodes2string.cpp:
  • kjs/number_object.cpp:
  • kjs/number_object.h:
  • kjs/object.cpp:
  • kjs/object.h:
  • kjs/object_object.cpp:
  • kjs/object_object.h:
  • kjs/operations.cpp:
  • kjs/operations.h:
  • kjs/property_map.cpp:
  • kjs/property_map.h:
  • kjs/reference.cpp:
  • kjs/reference.h:
  • kjs/reference_list.cpp:
  • kjs/reference_list.h:
  • kjs/regexp.cpp:
  • kjs/regexp.h:
  • kjs/regexp_object.cpp:
  • kjs/regexp_object.h:
  • kjs/scope_chain.cpp:
  • kjs/scope_chain.h:
  • kjs/simple_number.h:
  • kjs/string_object.cpp:
  • kjs/string_object.h:
  • kjs/testkjs.cpp:
  • kjs/types.h:
  • kjs/ustring.cpp:
  • kjs/ustring.h:
  • kjs/value.cpp:
  • kjs/value.h:
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.6 KB
Line 
1// -*- c-basic-offset: 2 -*-
2/*
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2000 Harri Porten ([email protected])
5 * Copyright (C) 2003 Apple Computer, Inc.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 */
22
23#include <stdio.h>
24#include <string.h>
25
26#include "lookup.h"
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31
32using namespace KJS;
33
34static inline bool keysMatch(const UChar *c, unsigned len, const char *s)
35{
36 for (unsigned i = 0; i != len; i++, c++, s++)
37 if (c->uc != (unsigned char)*s)
38 return false;
39 return *s == 0;
40}
41
42const HashEntry* Lookup::findEntry( const struct HashTable *table,
43 const UChar *c, unsigned int len )
44{
45#ifndef NDEBUG
46 if (table->type != 2) {
47 fprintf(stderr, "KJS: Unknown hash table version.\n");
48 return 0;
49 }
50#endif
51
52 int h = hash(c, len) % table->hashSize;
53 const HashEntry *e = &table->entries[h];
54
55 // empty bucket ?
56 if (!e->s)
57 return 0;
58
59 do {
60 // compare strings
61 if (keysMatch(c, len, e->s))
62 return e;
63 // try next bucket
64 e = e->next;
65 } while (e);
66
67 return 0;
68}
69
70const HashEntry* Lookup::findEntry( const struct HashTable *table,
71 const Identifier &s )
72{
73 return findEntry( table, s.data(), s.size() );
74}
75
76int Lookup::find(const struct HashTable *table,
77 const UChar *c, unsigned int len)
78{
79 const HashEntry *entry = findEntry( table, c, len );
80 if (entry)
81 return entry->value;
82 return -1;
83}
84
85int Lookup::find(const struct HashTable *table, const Identifier &s)
86{
87 return find(table, s.data(), s.size());
88}
89
90unsigned int Lookup::hash(const UChar *c, unsigned int len)
91{
92 unsigned int val = 0;
93 // ignoring higher byte
94 for (unsigned int i = 0; i < len; i++, c++)
95 val += c->low();
96
97 return val;
98}
99
100unsigned int Lookup::hash(const Identifier &key)
101{
102 return hash(key.data(), key.size());
103}
104
105unsigned int Lookup::hash(const char *s)
106{
107 unsigned int val = 0;
108 while (*s)
109 val += *s++;
110
111 return val;
112}
Note: See TracBrowser for help on using the repository browser.