Debian Codegraph @

external/bdwgc/include/cord.h

raw
1 /*
2 * Copyright (c) 1993-1994 by Xerox Corporation. All rights reserved.
3 *
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
6 *
7 * Permission is hereby granted to use or copy this program
8 * for any purpose, provided the above notices are retained on all copies.
9 * Permission to modify the code and to distribute modified code is granted,
10 * provided the above notices are retained, and a notice that the code was
11 * modified is included with the above copyright notice.
12 */
13
14 /*
15 * Cords are immutable character strings. A number of operations
16 * on long cords are much more efficient than their strings.h counterpart.
17 * In particular, concatenation takes constant time independent of the length
18 * of the arguments. (Cords are represented as trees, with internal
19 * nodes representing concatenation and leaves consisting of either C
20 * strings or a functional description of the string.)
21 *
22 * The following are reasonable applications of cords. They would perform
23 * unacceptably if C strings were used:
24 * - A compiler that produces assembly language output by repeatedly
25 * concatenating instructions onto a cord representing the output file.
26 * - A text editor that converts the input file to a cord, and then
27 * performs editing operations by producing a new cord representing
28 * the file after each character change (and keeping the old ones in an
29 * edit history)
30 *
31 * For optimal performance, cords should be built by
32 * concatenating short sections.
33 * This interface is designed for maximum compatibility with C strings.
34 * ASCII NUL characters may be embedded in cords using CORD_from_fn.
35 * This is handled correctly, but CORD_to_char_star will produce a string
36 * with embedded NULs when given such a cord.
37 *
38 * This interface is fairly big, largely for performance reasons.
39 * The most basic constants and functions:
40 *
41 * CORD - the type of a cord;
42 * CORD_EMPTY - empty cord;
43 * CORD_len(cord) - length of a cord;
44 * CORD_cat(cord1,cord2) - concatenation of two cords;
45 * CORD_substr(cord, start, len) - substring (or subcord);
46 * CORD_pos i; CORD_FOR(i, cord) { ... CORD_pos_fetch(i) ... } -
47 * examine each character in a cord. CORD_pos_fetch(i) is the char.
48 * CORD_fetch(int i) - Retrieve i'th character (slowly).
49 * CORD_cmp(cord1, cord2) - compare two cords.
50 * CORD_from_file(FILE * f) - turn a read-only file into a cord.
51 * CORD_to_char_star(cord) - convert to C string.
52 * (Non-NULL C constant strings are cords.)
53 * CORD_printf (etc.) - cord version of printf. Use %r for cords.
54 */
55 #ifndef CORD_H
56 #define CORD_H
57
58 #include <stddef.h>
59 #include <stdio.h>
60
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64
65 #if defined(GC_DLL) && !defined(CORD_NOT_DLL)
66 /* Same as for GC_API in gc_config_macros.h. */
67 # ifdef CORD_BUILD
68 # if defined(__MINGW32__) || defined(__CEGCC__)
69 # define CORD_API __declspec(dllexport)
70 # elif defined(_MSC_VER) || defined(__DMC__) || defined(__BORLANDC__) \
71 || defined(__CYGWIN__) || defined(__WATCOMC__)
72 # define CORD_API extern __declspec(dllexport)
73 # elif defined(__GNUC__) && !defined(GC_NO_VISIBILITY) \
74 && (__GNUC__ >= 4 || defined(GC_VISIBILITY_HIDDEN_SET))
75 /* Only matters if used in conjunction with -fvisibility=hidden option. */
76 # define CORD_API extern __attribute__((__visibility__("default")))
77 # endif
78 # else
79 # if defined(__MINGW32__) || defined(__CEGCC__) || defined(_MSC_VER) \
80 || defined(__DMC__) || defined(__BORLANDC__) || defined(__CYGWIN__)
81 # define CORD_API __declspec(dllimport)
82 # elif defined(__WATCOMC__)
83 # define CORD_API extern __declspec(dllimport)
84 # endif
85 # endif /* !CORD_BUILD */
86 #endif /* GC_DLL */
87
88 #ifndef CORD_API
89 # define CORD_API extern
90 #endif
91
92 /* Cords have type const char *. This is cheating quite a bit, and not */
93 /* 100% portable. But it means that nonempty character string */
94 /* constants may be used as cords directly, provided the string is */
95 /* never modified in place. The empty cord is represented by, and */
96 /* can be written as, 0. */
97
98 typedef const char * CORD;
99
100 /* An empty cord is always represented as nil */
101 #define CORD_EMPTY 0
102
103 /* Is a nonempty cord represented as a C string? */
104 #define CORD_IS_STRING(s) (*(s) != '\0')
105
106 /* Concatenate two cords. If the arguments are C strings, they may */
107 /* not be subsequently altered. */
108 CORD_API CORD CORD_cat(CORD x, CORD y);
109
110 /* Concatenate a cord and a C string with known length. Except for the */
111 /* empty string case, this is a special case of CORD_cat. Since the */
112 /* length is known, it can be faster. */
113 /* The string y is shared with the resulting CORD. Hence it should */
114 /* not be altered by the caller. */
115 CORD_API CORD CORD_cat_char_star(CORD x, const char * y, size_t leny);
116
117 /* Compute the length of a cord */
118 CORD_API size_t CORD_len(CORD x);
119
120 /* Cords may be represented by functions defining the ith character */
121 typedef char (* CORD_fn)(size_t i, void * client_data);
122
123 /* Turn a functional description into a cord. */
124 CORD_API CORD CORD_from_fn(CORD_fn fn, void * client_data, size_t len);
125
126 /* Return the substring (subcord really) of x with length at most n, */
127 /* starting at position i. (The initial character has position 0.) */
128 CORD_API CORD CORD_substr(CORD x, size_t i, size_t n);
129
130 /* Return the argument, but rebalanced to allow more efficient */
131 /* character retrieval, substring operations, and comparisons. */
132 /* This is useful only for cords that were built using repeated */
133 /* concatenation. Guarantees log time access to the result, unless */
134 /* x was obtained through a large number of repeated substring ops */
135 /* or the embedded functional descriptions take longer to evaluate. */
136 /* May reallocate significant parts of the cord. The argument is not */
137 /* modified; only the result is balanced. */
138 CORD_API CORD CORD_balance(CORD x);
139
140 /* The following traverse a cord by applying a function to each */
141 /* character. This is occasionally appropriate, especially where */
142 /* speed is crucial. But, since C doesn't have nested functions, */
143 /* clients of this sort of traversal are clumsy to write. Consider */
144 /* the functions that operate on cord positions instead. */
145
146 /* Function to iteratively apply to individual characters in cord. */
147 typedef int (* CORD_iter_fn)(char c, void * client_data);
148
149 /* Function to apply to substrings of a cord. Each substring is a */
150 /* a C character string, not a general cord. */
151 typedef int (* CORD_batched_iter_fn)(const char * s, void * client_data);
152 #define CORD_NO_FN ((CORD_batched_iter_fn)0)
153
154 /* Apply f1 to each character in the cord, in ascending order, */
155 /* starting at position i. If */
156 /* f2 is not CORD_NO_FN, then multiple calls to f1 may be replaced by */
157 /* a single call to f2. The parameter f2 is provided only to allow */
158 /* some optimization by the client. This terminates when the right */
159 /* end of this string is reached, or when f1 or f2 return != 0. In the */
160 /* latter case CORD_iter returns != 0. Otherwise it returns 0. */
161 /* The specified value of i must be < CORD_len(x). */
162 CORD_API int CORD_iter5(CORD x, size_t i, CORD_iter_fn f1,
163 CORD_batched_iter_fn f2, void * client_data);
164
165 /* A simpler version that starts at 0, and without f2: */
166 CORD_API int CORD_iter(CORD x, CORD_iter_fn f1, void * client_data);
167 #define CORD_iter(x, f1, cd) CORD_iter5(x, 0, f1, CORD_NO_FN, cd)
168
169 /* Similar to CORD_iter5, but end-to-beginning. No provisions for */
170 /* CORD_batched_iter_fn. */
171 CORD_API int CORD_riter4(CORD x, size_t i, CORD_iter_fn f1, void * client_data);
172
173 /* A simpler version that starts at the end: */
174 CORD_API int CORD_riter(CORD x, CORD_iter_fn f1, void * client_data);
175
176 #ifdef __cplusplus
177 } /* extern "C" */
178 #endif
179
180 /* Functions that operate on cord positions. The easy way to traverse */
181 /* cords. A cord position is logically a pair consisting of a cord */
182 /* and an index into that cord. But it is much faster to retrieve a */
183 /* character based on a position than on an index. Unfortunately, */
184 /* positions are big (order of a few 100 bytes), so allocate them with */
185 /* caution. */
186 /* Things in cord_pos.h should be treated as opaque, except as */
187 /* described below. Also note that */
188 /* CORD_pos_fetch, CORD_next and CORD_prev have both macro and function */
189 /* definitions. The former may evaluate their argument more than once. */
190 #include "cord_pos.h"
191
192 #ifdef __cplusplus
193 extern "C" {
194 #endif
195
196 /*
197 Visible definitions from above:
198
199 typedef <OPAQUE but fairly big> CORD_pos[1];
200
201 * Extract the cord from a position:
202 CORD CORD_pos_to_cord(CORD_pos p);
203
204 * Extract the current index from a position:
205 size_t CORD_pos_to_index(CORD_pos p);
206
207 * Fetch the character located at the given position:
208 char CORD_pos_fetch(CORD_pos p);
209
210 * Initialize the position to refer to the given cord and index.
211 * Note that this is the most expensive function on positions:
212 void CORD_set_pos(CORD_pos p, CORD x, size_t i);
213
214 * Advance the position to the next character.
215 * P must be initialized and valid.
216 * Invalidates p if past end:
217 void CORD_next(CORD_pos p);
218
219 * Move the position to the preceding character.
220 * P must be initialized and valid.
221 * Invalidates p if past beginning:
222 void CORD_prev(CORD_pos p);
223
224 * Is the position valid, i.e. inside the cord?
225 int CORD_pos_valid(CORD_pos p);
226 */
227 #define CORD_FOR(pos, cord) \
228 for (CORD_set_pos(pos, cord, 0); CORD_pos_valid(pos); CORD_next(pos))
229
230
231 /* An out of memory handler to call. May be supplied by client. */
232 /* Must not return. */
233 extern void (* CORD_oom_fn)(void);
234
235 /* Dump the representation of x to stdout in an implementation defined */
236 /* manner. Intended for debugging only. */
237 CORD_API void CORD_dump(CORD x);
238
239 /* The following could easily be implemented by the client. They are */
240 /* provided in cordxtra.c for convenience. */
241
242 /* Concatenate a character to the end of a cord. */
243 CORD_API CORD CORD_cat_char(CORD x, char c);
244
245 /* Concatenate n cords. */
246 CORD_API CORD CORD_catn(int n, /* CORD */ ...);
247
248 /* Return the character in CORD_substr(x, i, 1) */
249 CORD_API char CORD_fetch(CORD x, size_t i);
250
251 /* Return < 0, 0, or > 0, depending on whether x < y, x = y, x > y */
252 CORD_API int CORD_cmp(CORD x, CORD y);
253
254 /* A generalization that takes both starting positions for the */
255 /* comparison, and a limit on the number of characters to be compared. */
256 CORD_API int CORD_ncmp(CORD x, size_t x_start, CORD y, size_t y_start,
257 size_t len);
258
259 /* Find the first occurrence of s in x at position start or later. */
260 /* Return the position of the first character of s in x, or */
261 /* CORD_NOT_FOUND if there is none. */
262 CORD_API size_t CORD_str(CORD x, size_t start, CORD s);
263
264 /* Return a cord consisting of i copies of (possibly NUL) c. Dangerous */
265 /* in conjunction with CORD_to_char_star. */
266 /* The resulting representation takes constant space, independent of i. */
267 CORD_API CORD CORD_chars(char c, size_t i);
268 #define CORD_nul(i) CORD_chars('\0', (i))
269
270 /* Turn a file into cord. The file must be seekable. Its contents */
271 /* must remain constant. The file may be accessed as an immediate */
272 /* result of this call and/or as a result of subsequent accesses to */
273 /* the cord. Short files are likely to be immediately read, but */
274 /* long files are likely to be read on demand, possibly relying on */
275 /* stdio for buffering. */
276 /* We must have exclusive access to the descriptor f, i.e. we may */
277 /* read it at any time, and expect the file pointer to be */
278 /* where we left it. Normally this should be invoked as */
279 /* CORD_from_file(fopen(...)) */
280 /* CORD_from_file arranges to close the file descriptor when it is no */
281 /* longer needed (e.g. when the result becomes inaccessible). */
282 /* The file f must be such that ftell reflects the actual character */
283 /* position in the file, i.e. the number of characters that can be */
284 /* or were read with fread. On UNIX systems this is always true. On */
285 /* MS Windows systems, f must be opened in binary mode. */
286 CORD_API CORD CORD_from_file(FILE * f);
287
288 /* Equivalent to the above, except that the entire file will be read */
289 /* and the file pointer will be closed immediately. */
290 /* The binary mode restriction from above does not apply. */
291 CORD_API CORD CORD_from_file_eager(FILE * f);
292
293 /* Equivalent to the above, except that the file will be read on demand.*/
294 /* The binary mode restriction applies. */
295 CORD_API CORD CORD_from_file_lazy(FILE * f);
296
297 /* Turn a cord into a C string. The result shares no structure with */
298 /* x, and is thus modifiable. */
299 CORD_API char * CORD_to_char_star(CORD x);
300
301 /* Turn a C string into a CORD. The C string is copied, and so may */
302 /* subsequently be modified. */
303 CORD_API CORD CORD_from_char_star(const char *s);
304
305 /* Identical to the above, but the result may share structure with */
306 /* the argument and is thus not modifiable. */
307 CORD_API const char * CORD_to_const_char_star(CORD x);
308
309 /* Write a cord to a file, starting at the current position. No */
310 /* trailing NULs are newlines are added. */
311 /* Returns EOF if a write error occurs, 1 otherwise. */
312 CORD_API int CORD_put(CORD x, FILE * f);
313
314 /* "Not found" result for the following two functions. */
315 #define CORD_NOT_FOUND ((size_t)(-1))
316
317 /* A vague analog of strchr. Returns the position (an integer, not */
318 /* a pointer) of the first occurrence of (char) c inside x at position */
319 /* i or later. The value i must be < CORD_len(x). */
320 CORD_API size_t CORD_chr(CORD x, size_t i, int c);
321
322 /* A vague analog of strrchr. Returns index of the last occurrence */
323 /* of (char) c inside x at position i or earlier. The value i */
324 /* must be < CORD_len(x). */
325 CORD_API size_t CORD_rchr(CORD x, size_t i, int c);
326
327 #ifdef __cplusplus
328 } /* extern "C" */
329 #endif
330
331 /* The following are also not primitive, but are implemented in */
332 /* cordprnt.c. They provide functionality similar to the ANSI C */
333 /* functions with corresponding names, but with the following */
334 /* additions and changes: */
335 /* 1. A %r conversion specification specifies a CORD argument. Field */
336 /* width, precision, etc. have the same semantics as for %s. */
337 /* (Note that %c, %C, and %S were already taken.) */
338 /* 2. The format string is represented as a CORD. */
339 /* 3. CORD_sprintf and CORD_vsprintf assign the result through the 1st */
340 /* argument. Unlike their ANSI C versions, there is no need to guess */
341 /* the correct buffer size. */
342 /* 4. Most of the conversions are implement through the native */
343 /* vsprintf. Hence they are usually no faster, and */
344 /* idiosyncrasies of the native printf are preserved. However, */
345 /* CORD arguments to CORD_sprintf and CORD_vsprintf are NOT copied; */
346 /* the result shares the original structure. This may make them */
347 /* very efficient in some unusual applications. */
348 /* The format string is copied. */
349 /* All functions return the number of characters generated or -1 on */
350 /* error. This complies with the ANSI standard, but is inconsistent */
351 /* with some older implementations of sprintf. */
352
353 /* The implementation of these is probably less portable than the rest */
354 /* of this package. */
355
356 #ifndef CORD_NO_IO
357
358 #include <stdarg.h>
359
360 # ifdef __cplusplus
361 extern "C" {
362 # endif
363
364 CORD_API int CORD_sprintf(CORD * out, CORD format, ...);
365 CORD_API int CORD_vsprintf(CORD * out, CORD format, va_list args);
366 CORD_API int CORD_fprintf(FILE * f, CORD format, ...);
367 CORD_API int CORD_vfprintf(FILE * f, CORD format, va_list args);
368 CORD_API int CORD_printf(CORD format, ...);
369 CORD_API int CORD_vprintf(CORD format, va_list args);
370
371 # ifdef __cplusplus
372 } /* extern "C" */
373 # endif
374
375 #endif /* CORD_NO_IO */
376
377 #endif /* CORD_H */