| 1 |
/* stringpool.h |
| 2 |
|
| 3 |
Copyright 2009 Taco Hoekwater <[email protected]> |
| 4 |
|
| 5 |
This file is part of LuaTeX. |
| 6 |
|
| 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. |
| 11 |
|
| 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. |
| 16 |
|
| 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/>. */ |
| 19 |
|
| 20 |
|
| 21 |
#ifndef STRINGPOOL_H |
| 22 |
# define STRINGPOOL_H |
| 23 |
|
| 24 |
/* Both lua and tex strings can contains null, but C strings cannot, so: */ |
| 25 |
|
| 26 |
typedef struct { |
| 27 |
unsigned char *s; |
| 28 |
size_t l; |
| 29 |
} lstring; |
| 30 |
|
| 31 |
typedef struct { |
| 32 |
const char *s; |
| 33 |
size_t l; |
| 34 |
} const_lstring; |
| 35 |
|
| 36 |
extern lstring *string_pool; |
| 37 |
|
| 38 |
extern str_number str_ptr; |
| 39 |
extern str_number init_str_ptr; |
| 40 |
|
| 41 |
# define STRING_OFFSET 0x200000 |
| 42 |
# define STRING_OFFSET_BITS 21 |
| 43 |
|
| 44 |
# define get_nullstr() STRING_OFFSET |
| 45 |
|
| 46 |
# define biggest_char 1114111 /* 0x10FFFF, the largest allowed character number; must be |< max_halfword| */ |
| 47 |
# define too_big_char (biggest_char+1) /* 1114112, |biggest_char+1| */ |
| 48 |
# define special_char (biggest_char+2) /* 1114113, |biggest_char+2| */ |
| 49 |
# define number_chars (biggest_char+3) /* 1114112, |biggest_char+1| */ |
| 50 |
|
| 51 |
/* |
| 52 |
Several of the elementary string operations are performed using |
| 53 |
macros instead of procedures, because many of the |
| 54 |
operations are done quite frequently and we want to avoid the |
| 55 |
overhead of procedure calls. For example, here is |
| 56 |
a simple macro that computes the length of a string. |
| 57 |
*/ |
| 58 |
|
| 59 |
# define str_length(a) string_pool[(a)-STRING_OFFSET].l |
| 60 |
# define str_string(a) string_pool[(a)-STRING_OFFSET].s |
| 61 |
# define str_lstring(a) string_pool[(a)-STRING_OFFSET] |
| 62 |
|
| 63 |
/* Strings are created by appending character codes to |str_pool|. |
| 64 |
The |append_char| macro, defined here, does not check to see if the |
| 65 |
value of |pool_ptr| has gotten too high; this test is supposed to be |
| 66 |
made before |append_char| is used. There is also a |flush_char| |
| 67 |
macro, which erases the last character appended. |
| 68 |
|
| 69 |
To test if there is room to append |l| more characters to |str_pool|, |
| 70 |
we shall write |str_room(l)|, which aborts \TeX\ and gives an |
| 71 |
apologetic error message if there isn't enough room. |
| 72 |
*/ |
| 73 |
|
| 74 |
/* The length of the current string is called |cur_length|: */ |
| 75 |
|
| 76 |
extern unsigned char *cur_string; |
| 77 |
extern unsigned cur_length; |
| 78 |
extern unsigned cur_string_size; |
| 79 |
extern unsigned pool_size; |
| 80 |
|
| 81 |
# define EXTRA_STRING 500 |
| 82 |
|
| 83 |
/* put |ASCII_code| \# at the end of |str_pool| */ |
| 84 |
# define append_char(A) do { \ |
| 85 |
if (cur_string==NULL) reset_cur_string(); \ |
| 86 |
else str_room(1); \ |
| 87 |
cur_string[cur_length++]=(unsigned char)(A); \ |
| 88 |
} while (0) |
| 89 |
|
| 90 |
# define str_room(wsize) do { \ |
| 91 |
unsigned nsize; \ |
| 92 |
if ((cur_length+wsize) > cur_string_size) { \ |
| 93 |
nsize = cur_string_size + cur_string_size / 5 + EXTRA_STRING; \ |
| 94 |
if (nsize < (unsigned)(wsize)) { \ |
| 95 |
nsize = wsize + EXTRA_STRING; \ |
| 96 |
} \ |
| 97 |
cur_string = (unsigned char *) xreallocarray(cur_string, unsigned char, nsize); \ |
| 98 |
memset (cur_string+cur_length,0,nsize-cur_length); \ |
| 99 |
cur_string_size = nsize; \ |
| 100 |
} \ |
| 101 |
} while (0) |
| 102 |
|
| 103 |
# define flush_char() --cur_length /* forget the last character in the pool */ |
| 104 |
|
| 105 |
extern str_number make_string(void); |
| 106 |
extern boolean str_eq_buf(str_number s, int k); |
| 107 |
extern boolean str_eq_str(str_number s, str_number t); |
| 108 |
extern boolean str_eq_cstr(str_number, const char *, size_t); |
| 109 |
extern boolean get_strings_started(void); |
| 110 |
extern void reset_cur_string(void); |
| 111 |
|
| 112 |
# define save_cur_string() (cur_length>0 ? make_string() : 0) |
| 113 |
|
| 114 |
# define restore_cur_string(u) if (u!=0) { \ |
| 115 |
unsigned l = (unsigned)str_length(u); \ |
| 116 |
xfree(cur_string); \ |
| 117 |
reset_cur_string(); \ |
| 118 |
str_room(l); \ |
| 119 |
memcpy(cur_string, str_string(u),l); \ |
| 120 |
cur_length = l; \ |
| 121 |
flush_str(u); \ |
| 122 |
u=0; \ |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
extern str_number search_string(str_number search); |
| 127 |
extern int pool_to_unichar(unsigned char *t); |
| 128 |
|
| 129 |
extern str_number maketexstring(const char *); |
| 130 |
extern str_number maketexlstring(const char *, size_t); |
| 131 |
extern void append_string(const unsigned char *s, unsigned l); |
| 132 |
|
| 133 |
extern char *makecstring(int); |
| 134 |
extern char *makeclstring(int, size_t *); |
| 135 |
|
| 136 |
extern int dump_string_pool(void); |
| 137 |
extern int undump_string_pool(void); |
| 138 |
|
| 139 |
extern void init_string_pool_array(unsigned s); |
| 140 |
extern void flush_str(str_number s); |
| 141 |
|
| 142 |
#endif |