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