1 |
< |
/* |
1 |
> |
/* Copyright 1988 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
5 |
|
#endif |
6 |
+ |
|
7 |
+ |
/* |
8 |
|
* savestr.c - routines for efficient string storage. |
9 |
|
* |
10 |
|
* Savestr(s) stores a shared read-only string. |
33 |
|
|
34 |
|
static S_HEAD *stab[NHASH]; |
35 |
|
|
36 |
+ |
#define hash(s) (shash(s)%NHASH) |
37 |
+ |
|
38 |
|
extern char *savestr(), *strcpy(), *malloc(); |
39 |
|
|
40 |
|
#define NULL 0 |
55 |
|
|
56 |
|
if (str == NULL) |
57 |
|
return(NULL); |
58 |
< |
hval = shash(str); |
58 |
> |
hval = hash(str); |
59 |
|
for (sp = stab[hval]; sp != NULL; sp = sp->next) |
60 |
|
if (!strcmp(str, string(sp))) { |
61 |
|
sp->nl++; |
81 |
|
|
82 |
|
if (s == NULL) |
83 |
|
return; |
84 |
< |
hval = shash(s); |
84 |
> |
hval = hash(s); |
85 |
|
for (spl = NULL, sp = stab[hval]; sp != NULL; spl = sp, sp = sp->next) |
86 |
|
if (s == string(sp)) { |
87 |
|
if (--sp->nl > 0) |
97 |
|
|
98 |
|
|
99 |
|
int |
100 |
< |
strcmp(s1, s2) /* check for s1==s2 */ |
97 |
< |
register char *s1, *s2; |
98 |
< |
{ |
99 |
< |
if (s1 == s2) |
100 |
< |
return(0); |
101 |
< |
|
102 |
< |
while (*s1 == *s2++) |
103 |
< |
if (!*s1++) |
104 |
< |
return(0); |
105 |
< |
|
106 |
< |
return(*s1 - *--s2); |
107 |
< |
} |
108 |
< |
|
109 |
< |
|
110 |
< |
static int |
111 |
< |
shash(s) /* hash a string */ |
100 |
> |
shash(s) |
101 |
|
register char *s; |
102 |
|
{ |
103 |
|
register int h = 0; |
104 |
|
|
105 |
|
while (*s) |
106 |
< |
h += *s++; |
107 |
< |
|
119 |
< |
return(h % NHASH); |
106 |
> |
h = (h<<1 & 0x7fff) ^ (*s++ & 0xff); |
107 |
> |
return(h); |
108 |
|
} |