texstringpool.h /size: 4682 b    last modification: 2024-01-16 10:22
1/*
2    See license.txt in the root of this project.
3*/
4
5# ifndef LMT_STRINGPOOL_H
6# define LMT_STRINGPOOL_H
7
8/*tex
9
10    Both \LUA\ and |TEX\ strings can contain |nul| characters, but \CCODE\ strings cannot. The pool
11    is implemented differently anyway. The |init_str_ptr| is an offset that indicates how many strings
12    are in the format. Does it still make sense to have that distinction? Do we care?
13
14    We store the used bytes (in strings) in the |real| field so that it is carried with the data blob
15    (and ends up in statistics).
16
17*/
18
19typedef struct lstring {
20    union {
21        unsigned char *s;
22        const    char *c;
23    };
24    size_t l; /* could be int, but this way we padd */
25} lstring;
26
27typedef struct string_pool_info {
28    lstring       *string_pool;
29    memory_data    string_pool_data;
30    memory_data    string_body_data;
31    strnumber      reserved;
32    /*tex only when format is made and loaded */
33    int            string_max_length;
34    /*tex used for temporary string building: */
35    unsigned char *string_temp;
36    int            string_temp_allocated;
37    int            string_temp_top;
38} string_pool_info;
39
40extern string_pool_info lmt_string_pool_state;
41
42# define STRING_EXTRA_AMOUNT 512
43
44/*tex This is the reference of the empty string: */
45
46# define get_nullstr() cs_offset_value
47
48/*tex
49
50    Several of the elementary string operations are performed using macros instead of procedures,
51    because many of the operations are done quite frequently and we want to avoid the overhead of
52    procedure calls. For example, here is a simple macro that computes the length of a string.
53
54    Keep in mind that we are talking of a |string_pool| table that officially starts with the
55    unicode characters (as in \TEX\ with \ASCII) but that we use an offset to jump ove that. So the
56    real size doesn't include those single character code points.
57
58*/
59
60# define str_length(a)  (lmt_string_pool_state.string_pool[(a) - cs_offset_value].l)
61# define str_string(a)  (lmt_string_pool_state.string_pool[(a) - cs_offset_value].s)
62# define str_lstring(a) (lmt_string_pool_state.string_pool[(a) - cs_offset_value])
63
64/*tex
65
66    Strings are created by appending character codes to |str_pool|. The |append_char| macro,
67    defined here, does not check to see if the value of |pool_ptr| has gotten too high; this test
68    is supposed to be made before |append_char| is used. There is also a |flush_char| macro, which
69    erases the last character appended.
70
71    To test if there is room to append |l| more characters to |str_pool|, we shall write |str_room
72    (l)|, which aborts \TEX\ and gives an apologetic error message if there isn't enough room. The
73    length of the current string is called |cur_length|.
74
75*/
76
77/*tex Forget the last character in the pool. */
78
79static inline void  tex_flush_char(void)       { --lmt_string_pool_state.string_temp_top; }
80                   
81extern strnumber  tex_make_string            (void);
82extern strnumber  tex_push_string            (const unsigned char *s, int l);
83extern char      *tex_take_string            (int *len);
84extern int        tex_str_eq_buf             (strnumber s, int k, int n);
85extern int        tex_str_eq_str             (strnumber s, strnumber t);
86extern int        tex_str_eq_cstr            (strnumber s, const char *, size_t);
87extern int        tex_get_strings_started    (void);
88extern void       tex_reset_cur_string       (void);
89/*     strnumber  tex_search_string          (strnumber search); */
90/*     int        tex_used_strings           (void); */
91extern strnumber  tex_maketexstring          (const char *s);
92extern strnumber  tex_maketexlstring         (const char *s, size_t);
93extern void       tex_append_char            (unsigned char c);
94extern void       tex_append_string          (const unsigned char *s, unsigned l);
95extern char      *tex_makecstring            (int s, int *allocated);
96extern char      *tex_makeclstring           (int s, size_t *len);
97extern void       tex_dump_string_pool       (dumpstream f);
98extern void       tex_undump_string_pool     (dumpstream f);
99extern void       tex_initialize_string_pool (void);
100extern void       tex_initialize_string_mem  (void);
101extern void       tex_flush_str              (strnumber s);
102extern strnumber  tex_save_cur_string        (void);
103extern void       tex_restore_cur_string     (strnumber u);
104extern void       tex_compact_string_pool    (void);
105/*     void       tex_increment_pool_string  (int n); */
106/*     void       tex_decrement_pool_string  (int n); */
107                                   
108static inline const char *tex_to_cstring (int s) { return str_length(s) > 0 ? (char *) str_string(s) : ""; }
109
110# endif
111