Debian Codegraph @

texk/lcdf-typetools/lcdf-typetools-src/liblcdf/permstr.cc

raw
1 // -*- related-file-name: "../include/lcdf/permstr.hh" -*-
2
3 /* permstr.{cc,hh} -- permanent strings
4 *
5 * Copyright (c) 1998-2023 Eddie Kohler
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version. This program is distributed in the hope that it will be
11 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13 * Public License for more details.
14 */
15
16 #ifdef HAVE_CONFIG_H
17 # include <config.h>
18 #endif
19 #include <lcdf/permstr.hh>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdio.h>
23
24 static PermString::Initializer initializer;
25
26 PermString::Doodad PermString::zero_char_doodad = {
27 0, 0, { 0, 0 }
28 };
29 PermString::Doodad PermString::one_char_doodad[256];
30 PermString::Doodad* PermString::buckets[NHASH];
31
32 PermString::Initializer::Initializer()
33 {
34 static int initialized = 0;
35 if (!initialized) {
36 PermString::static_initialize();
37 initialized = 1;
38 }
39 }
40
41 void
42 PermString::static_initialize()
43 {
44 for (int i = 0; i < 256; i++) {
45 one_char_doodad[i].next = 0;
46 one_char_doodad[i].length = 1;
47 one_char_doodad[i].data[0] = i;
48 one_char_doodad[i].data[1] = 0;
49 }
50 }
51
52
53 // This scatter array, and the ideas behind it, are stolen from lcc.
54 static int scatter[] = { /* map characters to random values */
55 2078917053, 143302914, 1027100827, 1953210302, 755253631,
56 2002600785, 1405390230, 45248011, 1099951567, 433832350,
57 2018585307, 438263339, 813528929, 1703199216, 618906479,
58 573714703, 766270699, 275680090, 1510320440, 1583583926,
59 1723401032, 1965443329, 1098183682, 1636505764, 980071615,
60 1011597961, 643279273, 1315461275, 157584038, 1069844923,
61 471560540, 89017443, 1213147837, 1498661368, 2042227746,
62 1968401469, 1353778505, 1300134328, 2013649480, 306246424,
63 1733966678, 1884751139, 744509763, 400011959, 1440466707,
64 1363416242, 973726663, 59253759, 1639096332, 336563455,
65 1642837685, 1215013716, 154523136, 593537720, 704035832,
66 1134594751, 1605135681, 1347315106, 302572379, 1762719719,
67 269676381, 774132919, 1851737163, 1482824219, 125310639,
68 1746481261, 1303742040, 1479089144, 899131941, 1169907872,
69 1785335569, 485614972, 907175364, 382361684, 885626931,
70 200158423, 1745777927, 1859353594, 259412182, 1237390611,
71 48433401, 1902249868, 304920680, 202956538, 348303940,
72 1008956512, 1337551289, 1953439621, 208787970, 1640123668,
73 1568675693, 478464352, 266772940, 1272929208, 1961288571,
74 392083579, 871926821, 1117546963, 1871172724, 1771058762,
75 139971187, 1509024645, 109190086, 1047146551, 1891386329,
76 994817018, 1247304975, 1489680608, 706686964, 1506717157,
77 579587572, 755120366, 1261483377, 884508252, 958076904,
78 1609787317, 1893464764, 148144545, 1415743291, 2102252735,
79 1788268214, 836935336, 433233439, 2055041154, 2109864544,
80 247038362, 299641085, 834307717, 1364585325, 23330161,
81 457882831, 1504556512, 1532354806, 567072918, 404219416,
82 1276257488, 1561889936, 1651524391, 618454448, 121093252,
83 1010757900, 1198042020, 876213618, 124757630, 2082550272,
84 1834290522, 1734544947, 1828531389, 1982435068, 1002804590,
85 1783300476, 1623219634, 1839739926, 69050267, 1530777140,
86 1802120822, 316088629, 1830418225, 488944891, 1680673954,
87 1853748387, 946827723, 1037746818, 1238619545, 1513900641,
88 1441966234, 367393385, 928306929, 946006977, 985847834,
89 1049400181, 1956764878, 36406206, 1925613800, 2081522508,
90 2118956479, 1612420674, 1668583807, 1800004220, 1447372094,
91 523904750, 1435821048, 923108080, 216161028, 1504871315,
92 306401572, 2018281851, 1820959944, 2136819798, 359743094,
93 1354150250, 1843084537, 1306570817, 244413420, 934220434,
94 672987810, 1686379655, 1301613820, 1601294739, 484902984,
95 139978006, 503211273, 294184214, 176384212, 281341425,
96 228223074, 147857043, 1893762099, 1896806882, 1947861263,
97 1193650546, 273227984, 1236198663, 2116758626, 489389012,
98 593586330, 275676551, 360187215, 267062626, 265012701,
99 719930310, 1621212876, 2108097238, 2026501127, 1865626297,
100 894834024, 552005290, 1404522304, 48964196, 5816381,
101 1889425288, 188942202, 509027654, 36125855, 365326415,
102 790369079, 264348929, 513183458, 536647531, 13672163,
103 313561074, 1730298077, 286900147, 1549759737, 1699573055,
104 776289160, 2143346068, 1975249606, 1136476375, 262925046,
105 92778659, 1856406685, 1884137923, 53392249, 1735424165,
106 1602280572
107 };
108
109
110 void
111 PermString::initialize(const char* s, int length)
112 {
113 const unsigned char* m = reinterpret_cast<const unsigned char*>(s);
114 const unsigned char* mm;
115
116 if (length < 0)
117 length = (s ? strlen(s) : 0);
118
119 if (length == 0) {
120 _rep = zero_char_doodad.data;
121 return;
122 } else if (length == 1) {
123 _rep = one_char_doodad[m[0]].data;
124 return;
125 }
126
127 unsigned hash;
128 int l;
129 for (hash = 0, l = length, mm = m; l; mm++, l--)
130 hash = (hash << 1) + scatter[*mm];
131 hash &= (NHASH - 1);
132
133 Doodad *buck;
134 for (buck = buckets[hash]; buck; buck = buck->next)
135 if (length == buck->length && memcmp(s, buck->data, length) == 0) {
136 _rep = buck->data;
137 return;
138 }
139
140 // CANNOT USE new because the structure has variable size.
141 buck = (Doodad *)malloc(sizeof(Doodad) + length - 1);
142 buck->next = buckets[hash];
143 buckets[hash] = buck;
144 buck->length = length;
145 memcpy(buck->data, s, length);
146 buck->data[length] = 0;
147
148 _rep = buck->data;
149 }
150
151 static int pspos;
152 static int pscap = 64;
153 static char *psc = (char *)malloc(pscap);
154
155 static void
156 append(const char *s, int len)
157 {
158 if (pspos + len >= pscap) {
159 pscap *= 2;
160 psc = (char *)realloc(psc, pscap);
161 }
162 memcpy(psc + pspos, s, len);
163 pspos += len;
164 }
165
166 inline static void
167 extend(int len)
168 {
169 while (pspos + len >= pscap) {
170 pscap *= 2;
171 psc = (char *)realloc(psc, pscap);
172 }
173 }
174
175 PermString
176 vpermprintf(const char *s, va_list val)
177 {
178 pspos = 0;
179 while (1) {
180
181 const char *pct = strchr(s, '%');
182 if (!pct) {
183 if (*s)
184 append(s, strlen(s));
185 break;
186 }
187 if (pct != s) {
188 append(s, pct - s);
189 s = pct;
190 }
191
192 int iflag = -1;
193 while (1)
194 switch (*++s) {
195
196 case '0':
197 /* zeroflag = 1; */
198 break;
199
200 case '1': case '2': case '3': case '4': case '5':
201 case '6': case '7': case '8': case '9':
202 assert(iflag == -1 /* Too many decimal flags in permprintf */);
203 iflag = 0;
204 while (*s >= '0' && *s <= '9') {
205 iflag = iflag * 10 + *s - '0';
206 s++;
207 }
208 break;
209
210 case '*':
211 assert(iflag == -1 /* iflag given */);
212 iflag = va_arg(val, int);
213 break;
214
215 case 's': {
216 const char *x = va_arg(val, const char *);
217 if (x) {
218 if (iflag < 0)
219 append(x, strlen(x));
220 else
221 append(x, iflag);
222 }
223 goto pctdone;
224 }
225
226 case 'c': {
227 char c = (char)(va_arg(val, int) & 0xFF);
228 append(&c, 1);
229 goto pctdone;
230 }
231
232 case 'p': {
233 PermString::Capsule x = va_arg(val, PermString::Capsule);
234 PermString px;
235 if (x)
236 px = PermString::decapsule(x);
237 if (iflag < 0 || iflag > px.length())
238 append(px.c_str(), px.length());
239 else
240 append(px.c_str(), iflag);
241 goto pctdone;
242 }
243
244 case 'd': {
245 // FIXME FIXME rewrite for sense
246 int x = va_arg(val, int);
247 if (pspos == pscap)
248 extend(1);
249
250 // FIXME -2^31
251 unsigned int ux = x;
252 if (x < 0) {
253 psc[pspos++] = '-';
254 ux = -x;
255 }
256
257 int numdigits = 0;
258 for (unsigned digcountx = ux; digcountx > 9; digcountx /= 10)
259 numdigits++;
260
261 extend(numdigits + 1);
262 int digit = numdigits;
263 do {
264 psc[pspos + digit] = (ux % 10) + '0';
265 ux /= 10;
266 digit--;
267 } while (ux);
268 pspos += numdigits + 1;
269
270 goto pctdone;
271 }
272
273 case 'g': {
274 // FIXME FIXME rewrite for sense
275 double x = va_arg(val, double);
276 char buffer[1000];
277 int len = snprintf(buffer, sizeof(buffer), "%.10g", x);
278 extend(len);
279 strcpy(psc + pspos, buffer);
280 pspos += len;
281 goto pctdone;
282 }
283
284 default:
285 assert(0 /* Bad % in permprintf */);
286 goto pctdone;
287
288 }
289
290 pctdone:
291 s++;
292 }
293
294 return PermString(psc, pspos);
295 }
296
297 PermString permprintf(const char *s, ...)
298 {
299 va_list val;
300 va_start(val, s);
301 PermString p = vpermprintf(s, val);
302 va_end(val);
303 return p;
304 }
305
306 PermString permcat(PermString a, PermString b)
307 {
308 if (!a || !b)
309 return a ? a : b;
310 unsigned al = a.length();
311 unsigned bl = b.length();
312 char *s = new char[al + bl];
313 memcpy(s, a.c_str(), al);
314 memcpy(s + al, b.c_str(), bl);
315 PermString p(s, al + bl);
316 delete[] s;
317 return p;
318 }