Tizen 2.1 base
[external/enchant.git] / src / ispell / hash.cpp
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * hash.c - a simple hash function for ispell
4  *
5  * Pace Willisson, 1983
6  *
7  * Copyright 1992, 1993, Geoff Kuenning, Granada Hills, CA
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All modifications to the source code must be clearly marked as
20  *    such.  Binary redistributions based on modified source code
21  *    must be clearly marked as modified versions in the documentation
22  *    and/or other materials provided with the distribution.
23  * 4. All advertising materials mentioning features or use of this software
24  *    must display the following acknowledgment:
25  *      This product includes software developed by Geoff Kuenning and
26  *      other unpaid contributors.
27  * 5. The name of Geoff Kuenning may not be used to endorse or promote
28  *    products derived from this software without specific prior
29  *    written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY GEOFF KUENNING AND CONTRIBUTORS ``AS IS'' AND
32  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34  * ARE DISCLAIMED.  IN NO EVENT SHALL GEOFF KUENNING OR CONTRIBUTORS BE LIABLE
35  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41  * SUCH DAMAGE.
42  */
43
44 /*
45  * $Log$
46  * Revision 1.4  2003/08/14 17:51:27  dom
47  * update license - exception clause should be Lesser GPL
48  *
49  * Revision 1.3  2003/07/28 20:40:26  dom
50  * fix up the license clause, further win32-registry proof some directory getting functions
51  *
52  * Revision 1.2  2003/07/16 22:52:39  dom
53  * LGPL + exception license
54  *
55  * Revision 1.1  2003/07/15 01:15:05  dom
56  * ispell enchant backend
57  *
58  * Revision 1.2  2003/01/29 05:50:11  hippietrail
59  *
60  * Fixed my mess in EncodingManager.
61  * Changed many C casts to C++ casts.
62  *
63  * Revision 1.1  2003/01/24 05:52:33  hippietrail
64  *
65  * Refactored ispell code. Old ispell global variables had been put into
66  * an allocated structure, a pointer to which was passed to many functions.
67  * I have now made all such functions and variables private members of the
68  * ISpellChecker class. It was C OO, now it's C++ OO.
69  *
70  * I've fixed the makefiles and tested compilation but am unable to test
71  * operation. Please back out my changes if they cause problems which
72  * are not obvious or easy to fix.
73  *
74  * Revision 1.5  2002/09/19 05:31:15  hippietrail
75  *
76  * More Ispell cleanup.  Conditional globals and DEREF macros are removed.
77  * K&R function declarations removed, converted to Doxygen style comments
78  * where possible.  No code has been changed (I hope).  Compiles for me but
79  * unable to test.
80  *
81  * Revision 1.4  2002/09/17 03:03:29  hippietrail
82  *
83  * After seeking permission on the developer list I've reformatted all the
84  * spelling source which seemed to have parts which used 2, 3, 4, and 8
85  * spaces for tabs.  It should all look good with our standard 4-space
86  * tabs now.
87  * I've concentrated just on indentation in the actual code.  More prettying
88  * could be done.
89  * * NO code changes were made *
90  *
91  * Revision 1.3  2002/09/13 17:20:13  mpritchett
92  * Fix more warnings for Linux build
93  *
94  * Revision 1.2  2001/05/12 16:05:42  thomasf
95  * Big pseudo changes to ispell to make it pass around a structure rather
96  * than rely on all sorts of gloabals willy nilly here and there.  Also
97  * fixed our spelling class to work with accepting suggestions once more.
98  * This code is dirty, gross and ugly (not to mention still not supporting
99  * multiple hash sized just yet) but it works on my machine and will no
100  * doubt break other machines.
101  *
102  * Revision 1.1  2001/04/15 16:01:24  tomas_f
103  * moving to spell/xp
104  *
105  * Revision 1.3  1998/12/29 14:55:33  eric
106  *
107  * I've doctored the ispell code pretty extensively here.  It is now
108  * warning-free on Win32.  It also *works* on Win32 now, since I
109  * replaced all the I/O calls with ANSI standard ones.
110  *
111  * Revision 1.2  1998/12/28 23:11:30  eric
112  *
113  * modified spell code and integration to build on Windows.
114  * This is still a hack.
115  *
116  * Actually, it doesn't yet WORK on Windows.  It just builds.
117  * SpellCheckInit is failing for some reason.
118  *
119  * Revision 1.1  1998/12/28 18:04:43  davet
120  * Spell checker code stripped from ispell.  At this point, there are
121  * two external routines...  the Init routine, and a check-a-word routine
122  * which returns a boolean value, and takes a 16 bit char string.
123  * The code resembles the ispell code as much as possible still.
124  *
125  * Revision 1.20  1994/01/25  07:11:34  geoff
126  * Get rid of all old RCS log lines in preparation for the 3.1 release.
127  *
128  */
129
130 #include "ispell_checker.h"
131
132 /*
133  * The following hash algorithm is due to Ian Dall, with slight modifications
134  * by Geoff Kuenning to reflect the results of testing with the English
135  * dictionaries actually distributed with ispell.
136  */
137 #define HASHSHIFT   5
138
139 #ifdef NO_CAPITALIZATION_SUPPORT
140 #define HASHUPPER(c)    c
141 #else /* NO_CAPITALIZATION_SUPPORT */
142 #define HASHUPPER(c)    mytoupper(c)
143 #endif /* NO_CAPITALIZATION_SUPPORT */
144
145 /*
146  * \param s
147  * \param hashtblsize
148  */
149 int ISpellChecker::hash (ichar_t *s, int hashtblsize)
150 {
151     register long       h = 0;
152     register int        i;
153
154 #ifdef ICHAR_IS_CHAR
155     for (i = 4;  i--  &&  *s != 0;  )
156                 h = (h << 8) | HASHUPPER (*s++);
157 #else /* ICHAR_IS_CHAR */
158     for (i = 2;  i--  &&  *s != 0;  )
159                 h = (h << 16) | HASHUPPER (*s++);
160 #endif /* ICHAR_IS_CHAR */
161     while (*s != 0)
162         {
163                 /*
164                  * We have to do circular shifts the hard way, since C doesn't
165                  * have them even though the hardware probably does.  Oh, well.
166                  */
167                 h = (h << HASHSHIFT)
168                   | ((h >> (32 - HASHSHIFT)) & ((1 << HASHSHIFT) - 1));
169                 h ^= HASHUPPER (*s++);
170         }
171     return static_cast<unsigned long>(h) % hashtblsize;
172 }