Debian Codegraph @

texk/web2c/luatexdir/tex/stringpool.c

raw
1 /*
2
3 stringpool.w
4
5 Copyright 2009-2010 Taco Hoekwater <taco@@luatex.org>
6
7 This file is part of LuaTeX.
8
9 LuaTeX is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2 of the License, or (at your
12 option) any later version.
13
14 LuaTeX is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with LuaTeX; if not, see <http://www.gnu.org/licenses/>.
21
22 */
23
24 #include "ptexlib.h"
25
26 /*tex
27
28 Control sequence names and diagnostic messages are variable-length strings of
29 eight-bit characters. Since PASCAL did not have a well-developed string
30 mechanism, \TeX\ did all of its string processing by homegrown methods.
31
32 Elaborate facilities for dynamic strings are not needed, so all of the necessary
33 operations can be handled with a simple data structure. The array |str_pool|
34 contains all of the (eight-bit) bytes off all of the strings, and the array
35 |str_start| contains indices of the starting points of each string. Strings are
36 referred to by integer numbers, so that string number |s| comprises the
37 characters |str_pool[j]| for |str_start_macro(s)<=j<str_start_macro(s+1)|.
38 Additional integer variables |pool_ptr| and |str_ptr| indicate the number of
39 entries used so far in |str_pool| and |str_start|, respectively; locations
40 |str_pool[pool_ptr]| and |str_start_macro(str_ptr)| are ready for the next string
41 to be allocated.
42
43 String numbers 0 to |biggest_char| are reserved for strings that correspond to
44 single UNICODE characters. This is in accordance with the conventions of \.{WEB}
45 which converts single-character strings into the ASCII code number of the single
46 character involved.
47
48 */
49
50 /*tex The array of strings: */
51
52 lstring *string_pool;
53
54 /*tex
55 This variable lives |STRING_OFFSET| below |string_pool| (handy for debugging:
56 |_string_pool[str_ptr] == str_string(str_ptr)|:
57 */
58
59 lstring *_string_pool;
60
61 /*tex The number of the current string being created: */
62
63 str_number str_ptr = (STRING_OFFSET + 1);
64
65 /*tex The starting value of |str_ptr|: */
66
67 str_number init_str_ptr;
68
69 /*tex
70 The current string buffer, the current index in that buffer, the malloced
71 size of |cur_string| and the occupied byte count:
72 */
73
74 unsigned char *cur_string;
75 unsigned cur_length;
76 unsigned cur_string_size;
77 unsigned pool_size;
78
79 /*tex
80
81 Once a sequence of characters has been appended to |cur_string|, it officially
82 becomes a string when the function |make_string| is called. This function returns
83 the identification number of the new string as its value.
84
85 */
86
87 void reset_cur_string(void)
88 {
89 cur_length = 0;
90 cur_string_size = 255;
91 cur_string = (unsigned char *) xmalloc(256);
92 memset(cur_string, 0, 256);
93 }
94
95 str_number make_string(void)
96 {
97 if (str_ptr == (max_strings + STRING_OFFSET)) {
98 overflow(
99 "number of strings",
100 (unsigned) (max_strings - init_str_ptr + STRING_OFFSET)
101 );
102 }
103 str_room(1);
104 cur_string[cur_length] = '\0'; /* now |lstring.s| is always a valid C string */
105 str_string(str_ptr) = (unsigned char *) cur_string;
106 str_length(str_ptr) = cur_length;
107 pool_size += cur_length;
108 reset_cur_string();
109 str_ptr++;
110 return (str_ptr - 1);
111 }
112
113 int pool_to_unichar(unsigned char *t)
114 {
115 return (int) str2uni(t);
116 }
117
118 /*tex
119
120 The following subroutine compares string |s| with another string of the same
121 length that appears in |buffer| starting at position |k|; the result is |true| if
122 and only if the strings are equal. Empirical tests indicate that |str_eq_buf| is
123 used in such a way that it tends to return |true| about 80 percent of the time.
124
125 */
126
127 boolean str_eq_buf(str_number s, int k)
128 {
129 int a;
130 if (s < STRING_OFFSET) {
131 a = buffer_to_unichar(k);
132 if (a != s)
133 return false;
134 } else {
135 unsigned char *j = str_string(s);
136 unsigned char *l = j + str_length(s);
137 while (j < l) {
138 if (*j++ != buffer[k++])
139 return false;
140 }
141 }
142 return true;
143 }
144
145 /*tex
146
147 Here is a similar routine, but it compares two strings in the string pool, and it
148 does not assume that they have the same length.
149
150 */
151
152 boolean str_eq_str(str_number s, str_number t)
153 {
154 int a = 0;
155 unsigned char *j, *k, *l;
156 if (s < STRING_OFFSET) {
157 if (t >= STRING_OFFSET) {
158 k = str_string(t);
159 if (s <= 0x7F && (str_length(t) == 1) && *k == s)
160 return true;
161 a = pool_to_unichar(k);
162 if (a != s)
163 return false;
164 } else {
165 if (t != s)
166 return false;
167 }
168 } else if (t < STRING_OFFSET) {
169 j = str_string(s);
170 if (t <= 0x7F && (str_length(s) == 1) && *j == t)
171 return true;
172 a = pool_to_unichar(j);
173 if (a != t)
174 return false;
175 } else {
176 if (str_length(s) != str_length(t))
177 return false;
178 k = str_string(t);
179 j = str_string(s);
180 l = j + str_length(s);
181 while (j < l) {
182 if (*j++ != *k++)
183 return false;
184 }
185 }
186 return true;
187 }
188
189 /*tex A string compare helper: */
190
191 boolean str_eq_cstr(str_number r, const char *s, size_t l)
192 {
193 if (l != (size_t) str_length(r))
194 return false;
195 return (strncmp((const char *) (str_string(r)), s, l) == 0);
196 }
197
198
199 /*tex
200
201 The initial values of |str_pool|, |str_start|, |pool_ptr|, and |str_ptr| are
202 computed by the \.{INITEX} program, based in part on the information that \.{WEB}
203 has output while processing \TeX.
204
205 The first |string_offset| strings are single-characters strings matching Unicode.
206 There is no point in generating all of these. But |str_ptr| has initialized
207 properly, otherwise |print_char| cannot see the difference between characters and
208 strings.
209
210 */
211
212 boolean get_strings_started(void)
213 {
214 reset_cur_string();
215 return true;
216 }
217
218 /*tex
219
220 The string recycling routines. \TeX{} uses 2 upto 4 {\it new\/} strings when
221 scanning a filename in an \.{\\input}, \.{\\openin}, or \.{\\openout} operation.
222 These strings are normally lost because the reference to them are not saved after
223 finishing the operation. |search_string| searches through the string pool for the
224 given string and returns either 0 or the found string number.
225
226 */
227
228 str_number search_string(str_number search)
229 {
230 str_number s;
231 size_t len;
232 len = str_length(search);
233 if (len == 0) {
234 return get_nullstr();
235 } else {
236 /*tex We start the search with newest string below |s|; |search>1|! */
237 s = search - 1;
238 while (s >= STRING_OFFSET) {
239 /* The first |string_offset| of strings depend on the implementation! */
240 if (str_length(s) == len)
241 if (str_eq_str(s, search))
242 return s;
243 s--;
244 }
245 }
246 return 0;
247 }
248
249 str_number maketexstring(const char *s)
250 {
251 if (s == NULL || *s == 0)
252 return get_nullstr();
253 return maketexlstring(s, strlen(s));
254 }
255
256 str_number maketexlstring(const char *s, size_t l)
257 {
258 if (s == NULL || l == 0)
259 return get_nullstr();
260 str_string(str_ptr) = xmalloc((unsigned) (l + 1));
261 memcpy(str_string(str_ptr), s, (l + 1));
262 str_length(str_ptr) = (unsigned) l;
263 str_ptr++;
264 return (str_ptr - 1);
265 }
266
267 /*tex
268
269 This appends a C string to a \TEX\ string:
270
271 */
272
273 void append_string(const unsigned char *s, unsigned l)
274 {
275 if (s == NULL || l == 0)
276 return;
277 str_room(l);
278 memcpy(cur_string + cur_length, s, l);
279 cur_length += l;
280 return;
281 }
282
283 char *makecstring(int s)
284 {
285 size_t l;
286 return makeclstring(s, &l);
287 }
288
289 char *makeclstring(int s, size_t * len)
290 {
291 if (s < STRING_OFFSET) {
292 *len = (size_t) utf8_size(s);
293 return (char *) uni2str((unsigned) s);
294 } else {
295 unsigned l = (unsigned) str_length(s);
296 char *cstrbuf = xmalloc(l + 1);
297 memcpy(cstrbuf, str_string(s), l);
298 cstrbuf[l] = '\0';
299 *len = (size_t) l;
300 return cstrbuf;
301 }
302 }
303
304 int dump_string_pool(void)
305 {
306 int j;
307 int l;
308 int k = str_ptr;
309 dump_int(k - STRING_OFFSET);
310 for (j = STRING_OFFSET + 1; j < k; j++) {
311 l = (int) str_length(j);
312 if (str_string(j) == NULL)
313 l = -1;
314 dump_int(l);
315 if (l > 0)
316 dump_things(*str_string(j), str_length(j));
317 }
318 return (k - STRING_OFFSET);
319 }
320
321 int undump_string_pool(void)
322 {
323 int j;
324 int x;
325 undump_int(str_ptr);
326 if (max_strings < str_ptr + strings_free)
327 max_strings = str_ptr + strings_free;
328 str_ptr += STRING_OFFSET;
329 if (ini_version)
330 libcfree(string_pool);
331 init_string_pool_array((unsigned) max_strings);
332 for (j = STRING_OFFSET + 1; j < str_ptr; j++) {
333 undump_int(x);
334 if (x >= 0) {
335 str_length(j) = (unsigned) x;
336 pool_size += (unsigned) x;
337 str_string(j) = xmallocarray(unsigned char, (unsigned) (x + 1));
338 undump_things(*str_string(j), (unsigned) x);
339 *(str_string(j) + str_length(j)) = '\0';
340 } else {
341 str_length(j) = 0;
342 }
343 }
344 init_str_ptr = str_ptr;
345 return str_ptr;
346 }
347
348 void init_string_pool_array(unsigned s)
349 {
350 string_pool = xmallocarray(lstring, s);
351 _string_pool = string_pool - STRING_OFFSET;
352 memset(string_pool, 0, s * sizeof(lstring));
353 /* seed the null string */
354 string_pool[0].s = xmalloc(1);
355 string_pool[0].s[0] = '\0';
356 }
357
358 /*tex
359
360 To destroy an already made string, we say |flush_str|.
361
362 */
363
364 void flush_str(str_number s)
365 {
366 if (s > STRING_OFFSET) {
367 /*tex Don't ever delete the null string! */
368 pool_size -= (unsigned) str_length(s);
369 str_length(s) = 0;
370 xfree(str_string(s));
371 }
372 while (str_string((str_ptr - 1)) == NULL)
373 str_ptr--;
374 }