1 |
4 |
hellwig |
#include "c.h"
|
2 |
|
|
|
3 |
|
|
static char rcsid[] = "$Id: string.c,v 1.1 2002/08/28 23:12:46 drh Exp $";
|
4 |
|
|
|
5 |
|
|
static struct string {
|
6 |
|
|
char *str;
|
7 |
|
|
int len;
|
8 |
|
|
struct string *link;
|
9 |
|
|
} *buckets[1024];
|
10 |
|
|
static int scatter[] = { /* map characters to random values */
|
11 |
|
|
2078917053, 143302914, 1027100827, 1953210302, 755253631,
|
12 |
|
|
2002600785, 1405390230, 45248011, 1099951567, 433832350,
|
13 |
|
|
2018585307, 438263339, 813528929, 1703199216, 618906479,
|
14 |
|
|
573714703, 766270699, 275680090, 1510320440, 1583583926,
|
15 |
|
|
1723401032, 1965443329, 1098183682, 1636505764, 980071615,
|
16 |
|
|
1011597961, 643279273, 1315461275, 157584038, 1069844923,
|
17 |
|
|
471560540, 89017443, 1213147837, 1498661368, 2042227746,
|
18 |
|
|
1968401469, 1353778505, 1300134328, 2013649480, 306246424,
|
19 |
|
|
1733966678, 1884751139, 744509763, 400011959, 1440466707,
|
20 |
|
|
1363416242, 973726663, 59253759, 1639096332, 336563455,
|
21 |
|
|
1642837685, 1215013716, 154523136, 593537720, 704035832,
|
22 |
|
|
1134594751, 1605135681, 1347315106, 302572379, 1762719719,
|
23 |
|
|
269676381, 774132919, 1851737163, 1482824219, 125310639,
|
24 |
|
|
1746481261, 1303742040, 1479089144, 899131941, 1169907872,
|
25 |
|
|
1785335569, 485614972, 907175364, 382361684, 885626931,
|
26 |
|
|
200158423, 1745777927, 1859353594, 259412182, 1237390611,
|
27 |
|
|
48433401, 1902249868, 304920680, 202956538, 348303940,
|
28 |
|
|
1008956512, 1337551289, 1953439621, 208787970, 1640123668,
|
29 |
|
|
1568675693, 478464352, 266772940, 1272929208, 1961288571,
|
30 |
|
|
392083579, 871926821, 1117546963, 1871172724, 1771058762,
|
31 |
|
|
139971187, 1509024645, 109190086, 1047146551, 1891386329,
|
32 |
|
|
994817018, 1247304975, 1489680608, 706686964, 1506717157,
|
33 |
|
|
579587572, 755120366, 1261483377, 884508252, 958076904,
|
34 |
|
|
1609787317, 1893464764, 148144545, 1415743291, 2102252735,
|
35 |
|
|
1788268214, 836935336, 433233439, 2055041154, 2109864544,
|
36 |
|
|
247038362, 299641085, 834307717, 1364585325, 23330161,
|
37 |
|
|
457882831, 1504556512, 1532354806, 567072918, 404219416,
|
38 |
|
|
1276257488, 1561889936, 1651524391, 618454448, 121093252,
|
39 |
|
|
1010757900, 1198042020, 876213618, 124757630, 2082550272,
|
40 |
|
|
1834290522, 1734544947, 1828531389, 1982435068, 1002804590,
|
41 |
|
|
1783300476, 1623219634, 1839739926, 69050267, 1530777140,
|
42 |
|
|
1802120822, 316088629, 1830418225, 488944891, 1680673954,
|
43 |
|
|
1853748387, 946827723, 1037746818, 1238619545, 1513900641,
|
44 |
|
|
1441966234, 367393385, 928306929, 946006977, 985847834,
|
45 |
|
|
1049400181, 1956764878, 36406206, 1925613800, 2081522508,
|
46 |
|
|
2118956479, 1612420674, 1668583807, 1800004220, 1447372094,
|
47 |
|
|
523904750, 1435821048, 923108080, 216161028, 1504871315,
|
48 |
|
|
306401572, 2018281851, 1820959944, 2136819798, 359743094,
|
49 |
|
|
1354150250, 1843084537, 1306570817, 244413420, 934220434,
|
50 |
|
|
672987810, 1686379655, 1301613820, 1601294739, 484902984,
|
51 |
|
|
139978006, 503211273, 294184214, 176384212, 281341425,
|
52 |
|
|
228223074, 147857043, 1893762099, 1896806882, 1947861263,
|
53 |
|
|
1193650546, 273227984, 1236198663, 2116758626, 489389012,
|
54 |
|
|
593586330, 275676551, 360187215, 267062626, 265012701,
|
55 |
|
|
719930310, 1621212876, 2108097238, 2026501127, 1865626297,
|
56 |
|
|
894834024, 552005290, 1404522304, 48964196, 5816381,
|
57 |
|
|
1889425288, 188942202, 509027654, 36125855, 365326415,
|
58 |
|
|
790369079, 264348929, 513183458, 536647531, 13672163,
|
59 |
|
|
313561074, 1730298077, 286900147, 1549759737, 1699573055,
|
60 |
|
|
776289160, 2143346068, 1975249606, 1136476375, 262925046,
|
61 |
|
|
92778659, 1856406685, 1884137923, 53392249, 1735424165,
|
62 |
|
|
1602280572
|
63 |
|
|
};
|
64 |
|
|
char *string(const char *str) {
|
65 |
|
|
const char *s;
|
66 |
|
|
|
67 |
|
|
for (s = str; *s; s++)
|
68 |
|
|
;
|
69 |
|
|
return stringn(str, s - str);
|
70 |
|
|
}
|
71 |
|
|
char *stringd(long n) {
|
72 |
|
|
char str[25], *s = str + sizeof (str);
|
73 |
|
|
unsigned long m;
|
74 |
|
|
|
75 |
|
|
if (n == LONG_MIN)
|
76 |
|
|
m = (unsigned long)LONG_MAX + 1;
|
77 |
|
|
else if (n < 0)
|
78 |
|
|
m = -n;
|
79 |
|
|
else
|
80 |
|
|
m = n;
|
81 |
|
|
do
|
82 |
|
|
*--s = m%10 + '0';
|
83 |
|
|
while ((m /= 10) != 0);
|
84 |
|
|
if (n < 0)
|
85 |
|
|
*--s = '-';
|
86 |
|
|
return stringn(s, str + sizeof (str) - s);
|
87 |
|
|
}
|
88 |
|
|
char *stringn(const char *str, int len) {
|
89 |
|
|
int i;
|
90 |
|
|
unsigned int h;
|
91 |
|
|
const char *end;
|
92 |
|
|
struct string *p;
|
93 |
|
|
|
94 |
|
|
assert(str);
|
95 |
|
|
for (h = 0, i = len, end = str; i > 0; i--)
|
96 |
|
|
h = (h<<1) + scatter[*(unsigned char *)end++];
|
97 |
|
|
h &= NELEMS(buckets)-1;
|
98 |
|
|
for (p = buckets[h]; p; p = p->link)
|
99 |
|
|
if (len == p->len) {
|
100 |
|
|
const char *s1 = str;
|
101 |
|
|
char *s2 = p->str;
|
102 |
|
|
do {
|
103 |
|
|
if (s1 == end)
|
104 |
|
|
return p->str;
|
105 |
|
|
} while (*s1++ == *s2++);
|
106 |
|
|
}
|
107 |
|
|
{
|
108 |
|
|
static char *next, *strlimit;
|
109 |
|
|
if (len + 1 >= strlimit - next) {
|
110 |
|
|
int n = len + 4*1024;
|
111 |
|
|
next = allocate(n, PERM);
|
112 |
|
|
strlimit = next + n;
|
113 |
|
|
}
|
114 |
|
|
NEW(p, PERM);
|
115 |
|
|
p->len = len;
|
116 |
|
|
for (p->str = next; str < end; )
|
117 |
|
|
*next++ = *str++;
|
118 |
|
|
*next++ = 0;
|
119 |
|
|
p->link = buckets[h];
|
120 |
|
|
buckets[h] = p;
|
121 |
|
|
return p->str;
|
122 |
|
|
}
|
123 |
|
|
}
|