beta-0.89.2
[luatex.git] / source / texk / web2c / luatexdir / tex / stringpool.h
bloba969547e7632f4cb9ffe929b6580b0d92740c622
1 /* stringpool.h
3 Copyright 2009 Taco Hoekwater <taco@luatex.org>
5 This file is part of LuaTeX.
7 LuaTeX is free software; you can redistribute it and/or modify it under
8 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
10 option) any later version.
12 LuaTeX is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License along
18 with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */
21 #ifndef STRINGPOOL_H
22 # define STRINGPOOL_H
24 /* Both lua and tex strings can contains null, but C strings cannot, so: */
26 typedef struct {
27 unsigned char *s;
28 size_t l;
29 } lstring;
31 typedef struct {
32 const char *s;
33 size_t l;
34 } const_lstring;
36 extern lstring *string_pool;
38 extern str_number str_ptr;
39 extern str_number init_str_ptr;
41 # define STRING_OFFSET 0x200000
42 # define STRING_OFFSET_BITS 21
44 # define get_nullstr() STRING_OFFSET
46 # define biggest_char 1114111
47 # define number_chars 1114112
48 # define special_char 1114113 /* |biggest_char+2| */
51 Several of the elementary string operations are performed using
52 macros instead of procedures, because many of the
53 operations are done quite frequently and we want to avoid the
54 overhead of procedure calls. For example, here is
55 a simple macro that computes the length of a string.
58 # define str_length(a) string_pool[(a)-STRING_OFFSET].l
59 # define str_string(a) string_pool[(a)-STRING_OFFSET].s
60 # define str_lstring(a) string_pool[(a)-STRING_OFFSET]
62 /* Strings are created by appending character codes to |str_pool|.
63 The |append_char| macro, defined here, does not check to see if the
64 value of |pool_ptr| has gotten too high; this test is supposed to be
65 made before |append_char| is used. There is also a |flush_char|
66 macro, which erases the last character appended.
68 To test if there is room to append |l| more characters to |str_pool|,
69 we shall write |str_room(l)|, which aborts \TeX\ and gives an
70 apologetic error message if there isn't enough room.
73 /* The length of the current string is called |cur_length|: */
75 extern unsigned char *cur_string;
76 extern unsigned cur_length;
77 extern unsigned cur_string_size;
78 extern unsigned pool_size;
80 # define EXTRA_STRING 500
82 /* put |ASCII_code| \# at the end of |str_pool| */
83 # define append_char(A) do { \
84 if (cur_string==NULL) reset_cur_string(); \
85 else str_room(1); \
86 cur_string[cur_length++]=(unsigned char)(A); \
87 } while (0)
89 # define str_room(wsize) do { \
90 unsigned nsize; \
91 if ((cur_length+wsize) > cur_string_size) { \
92 nsize = cur_string_size + cur_string_size / 5 + EXTRA_STRING; \
93 if (nsize < (unsigned)(wsize)) { \
94 nsize = wsize + EXTRA_STRING; \
95 } \
96 cur_string = (unsigned char *) xreallocarray(cur_string, unsigned char, nsize); \
97 memset (cur_string+cur_length,0,nsize-cur_length); \
98 cur_string_size = nsize; \
99 } \
100 } while (0)
102 # define flush_char() --cur_length /* forget the last character in the pool */
104 extern str_number make_string(void);
105 extern boolean str_eq_buf(str_number s, int k);
106 extern boolean str_eq_str(str_number s, str_number t);
107 extern boolean str_eq_cstr(str_number, const char *, size_t);
108 extern boolean get_strings_started(void);
109 extern void reset_cur_string(void);
111 # define save_cur_string() (cur_length>0 ? make_string() : 0)
113 # define restore_cur_string(u) if (u!=0) { \
114 unsigned l = (unsigned)str_length(u); \
115 xfree(cur_string); \
116 reset_cur_string(); \
117 str_room(l); \
118 memcpy(cur_string, str_string(u),l); \
119 cur_length = l; \
120 flush_str(u); \
121 u=0; \
125 extern str_number search_string(str_number search);
126 extern int pool_to_unichar(unsigned char *t);
128 extern str_number maketexstring(const char *);
129 extern str_number maketexlstring(const char *, size_t);
130 extern void append_string(const unsigned char *s, unsigned l);
132 extern char *makecstring(int);
133 extern char *makeclstring(int, size_t *);
135 extern int dump_string_pool(void);
136 extern int undump_string_pool(void);
138 extern void init_string_pool_array(unsigned s);
139 extern void flush_str(str_number s);
141 #endif