Annotation of parser3/src/lib/cord/cordbscs.c, revision 1.1.2.7
1.1.2.1 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 3, 1994 5:19 pm PDT */
16: # include "gc.h"
17: # include "cord.h"
18: # include <stdlib.h>
19: # include <stdio.h>
20: # include <string.h>
21:
22: /* An implementation of the cord primitives. These are the only */
23: /* Functions that understand the representation. We perform only */
24: /* minimal checks on arguments to these functions. Out of bounds */
25: /* arguments to the iteration functions may result in client functions */
26: /* invoked on garbage data. In most cases, client functions should be */
27: /* programmed defensively enough that this does not result in memory */
28: /* smashes. */
29:
30: typedef void (* oom_fn)(void);
31:
32: oom_fn CORD_oom_fn = (oom_fn) 0;
33:
34: # define OUT_OF_MEMORY { if (CORD_oom_fn != (oom_fn) 0) (*CORD_oom_fn)(); \
35: ABORT("Out of memory\n"); }
36: # define ABORT(msg) { fprintf(stderr, "%s\n", msg); abort(); }
37:
38: typedef unsigned long word;
39:
40: typedef union {
41: struct Concatenation {
42: char null;
43: char header;
44: char depth; /* concatenation nesting depth. */
45: unsigned char left_len;
46: /* Length of left child if it is sufficiently */
47: /* short; 0 otherwise. */
48: # define MAX_LEFT_LEN 255
49: word len;
50: CORD left; /* length(left) > 0 */
51: CORD right; /* length(right) > 0 */
52: } concatenation;
53: struct Function {
54: char null;
55: char header;
56: char depth; /* always 0 */
57: char left_len; /* always 0 */
58: word len;
59: CORD_fn fn;
60: void * client_data;
61: } function;
62: struct Generic {
63: char null;
64: char header;
65: char depth;
66: char left_len;
67: word len;
68: } generic;
69: char string[1];
70: } CordRep;
71:
72: # define CONCAT_HDR 1
73:
74: # define FN_HDR 4
75: # define SUBSTR_HDR 6
76: /* Substring nodes are a special case of function nodes. */
77: /* The client_data field is known to point to a substr_args */
78: /* structure, and the function is either CORD_apply_access_fn */
79: /* or CORD_index_access_fn. */
80:
81: /* The following may be applied only to function and concatenation nodes: */
82: #define IS_CONCATENATION(s) (((CordRep *)s)->generic.header == CONCAT_HDR)
83:
84: #define IS_FUNCTION(s) ((((CordRep *)s)->generic.header & FN_HDR) != 0)
85:
86: #define IS_SUBSTR(s) (((CordRep *)s)->generic.header == SUBSTR_HDR)
87:
88: #define LEN(s) (((CordRep *)s) -> generic.len)
89: #define DEPTH(s) (((CordRep *)s) -> generic.depth)
90: #define GEN_LEN(s) (CORD_IS_STRING(s) ? strlen(s) : LEN(s))
91:
92: #define LEFT_LEN(c) ((c) -> left_len != 0? \
93: (c) -> left_len \
94: : (CORD_IS_STRING((c) -> left) ? \
95: (c) -> len - GEN_LEN((c) -> right) \
96: : LEN((c) -> left)))
97:
98: #define SHORT_LIMIT (sizeof(CordRep) - 1)
99: /* Cords shorter than this are C strings */
100:
101:
102: /* Dump the internal representation of x to stdout, with initial */
103: /* indentation level n. */
104: void CORD_dump_inner(CORD x, unsigned n)
105: {
106: register size_t i;
107:
108: for (i = 0; i < (size_t)n; i++) {
109: fputs(" ", stdout);
110: }
111: if (x == 0) {
112: fputs("NIL\n", stdout);
113: } else if (CORD_IS_STRING(x)) {
114: for (i = 0; i <= SHORT_LIMIT; i++) {
115: if (x[i] == '\0') break;
116: putchar(x[i]);
117: }
118: if (x[i] != '\0') fputs("...", stdout);
119: putchar('\n');
120: } else if (IS_CONCATENATION(x)) {
121: register struct Concatenation * conc =
122: &(((CordRep *)x) -> concatenation);
123: printf("Concatenation: %p (len: %d, depth: %d)\n",
124: x, (int)(conc -> len), (int)(conc -> depth));
125: CORD_dump_inner(conc -> left, n+1);
126: CORD_dump_inner(conc -> right, n+1);
127: } else /* function */{
128: register struct Function * func =
129: &(((CordRep *)x) -> function);
130: if (IS_SUBSTR(x)) printf("(Substring) ");
131: printf("Function: %p (len: %d): ", x, (int)(func -> len));
132: for (i = 0; i < 20 && i < func -> len; i++) {
133: putchar((*(func -> fn))(i, func -> client_data));
134: }
135: if (i < func -> len) fputs("...", stdout);
136: putchar('\n');
137: }
138: }
139:
140: /* Dump the internal representation of x to stdout */
141: void CORD_dump(CORD x)
142: {
143: CORD_dump_inner(x, 0);
144: fflush(stdout);
145: }
146:
1.1.2.2 paf 147: CORD CORD_cat_char_star(CORD x, const char* y, size_t leny)
1.1.2.1 paf 148: {
149: register size_t result_len;
150: register size_t lenx;
151: register int depth;
1.1.2.3 paf 152:
1.1.2.1 paf 153: if (x == CORD_EMPTY) return(y);
1.1.2.6 paf 154: if (leny == 0) leny=strlen(y); // PAF
155: if (leny == 0) ABORT("CORD_cat_char_star(,y,) is empty"); // PAF
1.1.2.5 paf 156:
1.1.2.1 paf 157: if (CORD_IS_STRING(x)) {
158: lenx = strlen(x);
159: result_len = lenx + leny;
160: if (result_len <= SHORT_LIMIT) {
161: register char * result = GC_MALLOC_ATOMIC(result_len+1);
1.1.2.7 ! paf 162:
1.1.2.1 paf 163: if (result == 0) OUT_OF_MEMORY;
164: memcpy(result, x, lenx);
165: memcpy(result + lenx, y, leny);
166: result[result_len] = '\0';
167: return((CORD) result);
168: } else {
169: depth = 1;
170: }
171: } else {
172: register CORD right;
173: register CORD left;
174: register char * new_right;
175: register size_t right_len;
176:
177: lenx = LEN(x);
178:
179: if (leny <= SHORT_LIMIT/2
180: && IS_CONCATENATION(x)
181: && CORD_IS_STRING(right = ((CordRep *)x) -> concatenation.right)) {
182: /* Merge y into right part of x. */
183: if (!CORD_IS_STRING(left = ((CordRep *)x) -> concatenation.left)) {
184: right_len = lenx - LEN(left);
185: } else if (((CordRep *)x) -> concatenation.left_len != 0) {
186: right_len = lenx - ((CordRep *)x) -> concatenation.left_len;
187: } else {
188: right_len = strlen(right);
189: }
190: result_len = right_len + leny; /* length of new_right */
191: if (result_len <= SHORT_LIMIT) {
192: new_right = GC_MALLOC_ATOMIC(result_len + 1);
193: memcpy(new_right, right, right_len);
194: memcpy(new_right + right_len, y, leny);
195: new_right[result_len] = '\0';
196: y = new_right;
197: leny = result_len;
198: x = left;
199: lenx -= right_len;
200: /* Now fall through to concatenate the two pieces: */
201: }
202: if (CORD_IS_STRING(x)) {
203: depth = 1;
204: } else {
205: depth = DEPTH(x) + 1;
206: }
207: } else {
208: depth = DEPTH(x) + 1;
209: }
210: result_len = lenx + leny;
211: }
212: {
213: /* The general case; lenx, result_len is known: */
214: register struct Concatenation * result;
215:
216: result = GC_NEW(struct Concatenation);
217: if (result == 0) OUT_OF_MEMORY;
218: result->header = CONCAT_HDR;
219: result->depth = depth;
220: if (lenx <= MAX_LEFT_LEN) result->left_len = lenx;
221: result->len = result_len;
222: result->left = x;
223: result->right = y;
224: if (depth > MAX_DEPTH) {
225: return(CORD_balance((CORD)result));
226: } else {
227: return((CORD) result);
228: }
229: }
230: }
231:
232:
233: CORD CORD_cat(CORD x, CORD y)
234: {
235: register size_t result_len;
236: register int depth;
237: register size_t lenx;
238:
239: if (x == CORD_EMPTY) return(y);
240: if (y == CORD_EMPTY) return(x);
241: if (CORD_IS_STRING(y)) {
242: return(CORD_cat_char_star(x, y, strlen(y)));
243: } else if (CORD_IS_STRING(x)) {
244: lenx = strlen(x);
245: depth = DEPTH(y) + 1;
246: } else {
247: register int depthy = DEPTH(y);
248:
249: lenx = LEN(x);
250: depth = DEPTH(x) + 1;
251: if (depthy >= depth) depth = depthy + 1;
252: }
253: result_len = lenx + LEN(y);
254: {
255: register struct Concatenation * result;
256:
257: result = GC_NEW(struct Concatenation);
258: if (result == 0) OUT_OF_MEMORY;
259: result->header = CONCAT_HDR;
260: result->depth = depth;
261: if (lenx <= MAX_LEFT_LEN) result->left_len = lenx;
262: result->len = result_len;
263: result->left = x;
264: result->right = y;
265: return((CORD) result);
266: }
267: }
268:
269:
270:
271: CORD CORD_from_fn(CORD_fn fn, void * client_data, size_t len)
272: {
273: if (len <= 0) return(0);
274: if (len <= SHORT_LIMIT) {
275: register char * result;
276: register size_t i;
277: char buf[SHORT_LIMIT+1];
278: register char c;
279:
280: for (i = 0; i < len; i++) {
281: c = (*fn)(i, client_data);
282: if (c == '\0') goto gen_case;
283: buf[i] = c;
284: }
285: buf[i] = '\0';
286: result = GC_MALLOC_ATOMIC(len+1);
287: if (result == 0) OUT_OF_MEMORY;
288: strcpy(result, buf);
289: result[len] = '\0';
290: return((CORD) result);
291: }
292: gen_case:
293: {
294: register struct Function * result;
295:
296: result = GC_NEW(struct Function);
297: if (result == 0) OUT_OF_MEMORY;
298: result->header = FN_HDR;
299: /* depth is already 0 */
300: result->len = len;
301: result->fn = fn;
302: result->client_data = client_data;
303: return((CORD) result);
304: }
305: }
306:
307: size_t CORD_len(CORD x)
308: {
309: if (x == 0) {
310: return(0);
311: } else {
312: return(GEN_LEN(x));
313: }
314: }
315:
316: struct substr_args {
317: CordRep * sa_cord;
318: size_t sa_index;
319: };
320:
321: char CORD_index_access_fn(size_t i, void * client_data)
322: {
323: register struct substr_args *descr = (struct substr_args *)client_data;
324:
325: return(((char *)(descr->sa_cord))[i + descr->sa_index]);
326: }
327:
328: char CORD_apply_access_fn(size_t i, void * client_data)
329: {
330: register struct substr_args *descr = (struct substr_args *)client_data;
331: register struct Function * fn_cord = &(descr->sa_cord->function);
332:
333: return((*(fn_cord->fn))(i + descr->sa_index, fn_cord->client_data));
334: }
335:
336: /* A version of CORD_substr that simply returns a function node, thus */
337: /* postponing its work. The fourth argument is a function that may */
338: /* be used for efficient access to the ith character. */
339: /* Assumes i >= 0 and i + n < length(x). */
340: CORD CORD_substr_closure(CORD x, size_t i, size_t n, CORD_fn f)
341: {
342: register struct substr_args * sa = GC_NEW(struct substr_args);
343: CORD result;
344:
345: if (sa == 0) OUT_OF_MEMORY;
346: sa->sa_cord = (CordRep *)x;
347: sa->sa_index = i;
348: result = CORD_from_fn(f, (void *)sa, n);
349: ((CordRep *)result) -> function.header = SUBSTR_HDR;
350: return (result);
351: }
352:
353: # define SUBSTR_LIMIT (10 * SHORT_LIMIT)
354: /* Substrings of function nodes and flat strings shorter than */
355: /* this are flat strings. Othewise we use a functional */
356: /* representation, which is significantly slower to access. */
357:
358: /* A version of CORD_substr that assumes i >= 0, n > 0, and i + n < length(x).*/
359: CORD CORD_substr_checked(CORD x, size_t i, size_t n)
360: {
361: if (CORD_IS_STRING(x)) {
362: if (n > SUBSTR_LIMIT) {
363: return(CORD_substr_closure(x, i, n, CORD_index_access_fn));
364: } else {
365: register char * result = GC_MALLOC_ATOMIC(n+1);
366:
367: if (result == 0) OUT_OF_MEMORY;
368: strncpy(result, x+i, n);
369: result[n] = '\0';
370: return(result);
371: }
372: } else if (IS_CONCATENATION(x)) {
373: register struct Concatenation * conc
374: = &(((CordRep *)x) -> concatenation);
375: register size_t left_len;
376: register size_t right_len;
377:
378: left_len = LEFT_LEN(conc);
379: right_len = conc -> len - left_len;
380: if (i >= left_len) {
381: if (n == right_len) return(conc -> right);
382: return(CORD_substr_checked(conc -> right, i - left_len, n));
383: } else if (i+n <= left_len) {
384: if (n == left_len) return(conc -> left);
385: return(CORD_substr_checked(conc -> left, i, n));
386: } else {
387: /* Need at least one character from each side. */
388: register CORD left_part;
389: register CORD right_part;
390: register size_t left_part_len = left_len - i;
391:
392: if (i == 0) {
393: left_part = conc -> left;
394: } else {
395: left_part = CORD_substr_checked(conc -> left, i, left_part_len);
396: }
397: if (i + n == right_len + left_len) {
398: right_part = conc -> right;
399: } else {
400: right_part = CORD_substr_checked(conc -> right, 0,
401: n - left_part_len);
402: }
403: return(CORD_cat(left_part, right_part));
404: }
405: } else /* function */ {
406: if (n > SUBSTR_LIMIT) {
407: if (IS_SUBSTR(x)) {
408: /* Avoid nesting substring nodes. */
409: register struct Function * f = &(((CordRep *)x) -> function);
410: register struct substr_args *descr =
411: (struct substr_args *)(f -> client_data);
412:
413: return(CORD_substr_closure((CORD)descr->sa_cord,
414: i + descr->sa_index,
415: n, f -> fn));
416: } else {
417: return(CORD_substr_closure(x, i, n, CORD_apply_access_fn));
418: }
419: } else {
420: char * result;
421: register struct Function * f = &(((CordRep *)x) -> function);
422: char buf[SUBSTR_LIMIT+1];
423: register char * p = buf;
424: register char c;
425: register int j;
426: register int lim = i + n;
427:
428: for (j = i; j < lim; j++) {
429: c = (*(f -> fn))(j, f -> client_data);
430: if (c == '\0') {
431: return(CORD_substr_closure(x, i, n, CORD_apply_access_fn));
432: }
433: *p++ = c;
434: }
435: *p = '\0';
436: result = GC_MALLOC_ATOMIC(n+1);
437: if (result == 0) OUT_OF_MEMORY;
438: strcpy(result, buf);
439: return(result);
440: }
441: }
442: }
443:
444: CORD CORD_substr(CORD x, size_t i, size_t n)
445: {
446: register size_t len = CORD_len(x);
447:
448: if (i >= len || n <= 0) return(0);
449: /* n < 0 is impossible in a correct C implementation, but */
450: /* quite possible under SunOS 4.X. */
451: if (i + n > len) n = len - i;
452: # ifndef __STDC__
453: if (i < 0) ABORT("CORD_substr: second arg. negative");
454: /* Possible only if both client and C implementation are buggy. */
455: /* But empirically this happens frequently. */
456: # endif
457: return(CORD_substr_checked(x, i, n));
458: }
459:
460: /* See cord.h for definition. We assume i is in range. */
461: int CORD_iter5(CORD x, size_t i, CORD_iter_fn f1,
462: CORD_batched_iter_fn f2, void * client_data)
463: {
464: if (x == 0) return(0);
465: if (CORD_IS_STRING(x)) {
1.1.2.2 paf 466: register const char* p = x+i;
1.1.2.1 paf 467:
468: if (*p == '\0') ABORT("2nd arg to CORD_iter5 too big");
469: if (f2 != CORD_NO_FN) {
470: return((*f2)(p, client_data));
471: } else {
472: while (*p) {
473: if ((*f1)(*p, client_data)) return(1);
474: p++;
475: }
476: return(0);
477: }
478: } else if (IS_CONCATENATION(x)) {
479: register struct Concatenation * conc
480: = &(((CordRep *)x) -> concatenation);
481:
482:
483: if (i > 0) {
484: register size_t left_len = LEFT_LEN(conc);
485:
486: if (i >= left_len) {
487: return(CORD_iter5(conc -> right, i - left_len, f1, f2,
488: client_data));
489: }
490: }
491: if (CORD_iter5(conc -> left, i, f1, f2, client_data)) {
492: return(1);
493: }
494: return(CORD_iter5(conc -> right, 0, f1, f2, client_data));
495: } else /* function */ {
496: register struct Function * f = &(((CordRep *)x) -> function);
497: register size_t j;
498: register size_t lim = f -> len;
499:
500: for (j = i; j < lim; j++) {
501: if ((*f1)((*(f -> fn))(j, f -> client_data), client_data)) {
502: return(1);
503: }
504: }
505: return(0);
506: }
507: }
508:
509: #undef CORD_iter
510: int CORD_iter(CORD x, CORD_iter_fn f1, void * client_data)
511: {
512: return(CORD_iter5(x, 0, f1, CORD_NO_FN, client_data));
513: }
514:
515: int CORD_riter4(CORD x, size_t i, CORD_iter_fn f1, void * client_data)
516: {
517: if (x == 0) return(0);
518: if (CORD_IS_STRING(x)) {
1.1.2.2 paf 519: register const char* p = x + i;
1.1.2.1 paf 520: register char c;
521:
522: for(;;) {
523: c = *p;
524: if (c == '\0') ABORT("2nd arg to CORD_riter4 too big");
525: if ((*f1)(c, client_data)) return(1);
526: if (p == x) break;
527: p--;
528: }
529: return(0);
530: } else if (IS_CONCATENATION(x)) {
531: register struct Concatenation * conc
532: = &(((CordRep *)x) -> concatenation);
533: register CORD left_part = conc -> left;
534: register size_t left_len;
535:
536: left_len = LEFT_LEN(conc);
537: if (i >= left_len) {
538: if (CORD_riter4(conc -> right, i - left_len, f1, client_data)) {
539: return(1);
540: }
541: return(CORD_riter4(left_part, left_len - 1, f1, client_data));
542: } else {
543: return(CORD_riter4(left_part, i, f1, client_data));
544: }
545: } else /* function */ {
546: register struct Function * f = &(((CordRep *)x) -> function);
547: register size_t j;
548:
549: for (j = i; ; j--) {
550: if ((*f1)((*(f -> fn))(j, f -> client_data), client_data)) {
551: return(1);
552: }
553: if (j == 0) return(0);
554: }
555: }
556: }
557:
558: int CORD_riter(CORD x, CORD_iter_fn f1, void * client_data)
559: {
560: return(CORD_riter4(x, CORD_len(x) - 1, f1, client_data));
561: }
562:
563: /*
564: * The following functions are concerned with balancing cords.
565: * Strategy:
566: * Scan the cord from left to right, keeping the cord scanned so far
567: * as a forest of balanced trees of exponentialy decreasing length.
568: * When a new subtree needs to be added to the forest, we concatenate all
569: * shorter ones to the new tree in the appropriate order, and then insert
570: * the result into the forest.
571: * Crucial invariants:
572: * 1. The concatenation of the forest (in decreasing order) with the
573: * unscanned part of the rope is equal to the rope being balanced.
574: * 2. All trees in the forest are balanced.
575: * 3. forest[i] has depth at most i.
576: */
577:
578: typedef struct {
579: CORD c;
580: size_t len; /* Actual length of c */
581: } ForestElement;
582:
583: static size_t min_len [ MAX_DEPTH ];
584:
585: static int min_len_init = 0;
586:
587: int CORD_max_len;
588:
589: typedef ForestElement Forest [ MAX_DEPTH ];
590: /* forest[i].len >= fib(i+1) */
591: /* The string is the concatenation */
592: /* of the forest in order of DECREASING */
593: /* indices. */
594:
595: void CORD_init_min_len()
596: {
597: register int i;
598: register size_t last, previous, current;
599:
600: min_len[0] = previous = 1;
601: min_len[1] = last = 2;
602: for (i = 2; i < MAX_DEPTH; i++) {
603: current = last + previous;
604: if (current < last) /* overflow */ current = last;
605: min_len[i] = current;
606: previous = last;
607: last = current;
608: }
609: CORD_max_len = last - 1;
610: min_len_init = 1;
611: }
612:
613:
614: void CORD_init_forest(ForestElement * forest, size_t max_len)
615: {
616: register int i;
617:
618: for (i = 0; i < MAX_DEPTH; i++) {
619: forest[i].c = 0;
620: if (min_len[i] > max_len) return;
621: }
622: ABORT("Cord too long");
623: }
624:
625: /* Add a leaf to the appropriate level in the forest, cleaning */
626: /* out lower levels as necessary. */
627: /* Also works if x is a balanced tree of concatenations; however */
628: /* in this case an extra concatenation node may be inserted above x; */
629: /* This node should not be counted in the statement of the invariants. */
630: void CORD_add_forest(ForestElement * forest, CORD x, size_t len)
631: {
632: register int i = 0;
633: register CORD sum = CORD_EMPTY;
634: register size_t sum_len = 0;
635:
636: while (len > min_len[i + 1]) {
637: if (forest[i].c != 0) {
638: sum = CORD_cat(forest[i].c, sum);
639: sum_len += forest[i].len;
640: forest[i].c = 0;
641: }
642: i++;
643: }
644: /* Sum has depth at most 1 greter than what would be required */
645: /* for balance. */
646: sum = CORD_cat(sum, x);
647: sum_len += len;
648: /* If x was a leaf, then sum is now balanced. To see this */
649: /* consider the two cases in which forest[i-1] either is or is */
650: /* not empty. */
651: while (sum_len >= min_len[i]) {
652: if (forest[i].c != 0) {
653: sum = CORD_cat(forest[i].c, sum);
654: sum_len += forest[i].len;
655: /* This is again balanced, since sum was balanced, and has */
656: /* allowable depth that differs from i by at most 1. */
657: forest[i].c = 0;
658: }
659: i++;
660: }
661: i--;
662: forest[i].c = sum;
663: forest[i].len = sum_len;
664: }
665:
666: CORD CORD_concat_forest(ForestElement * forest, size_t expected_len)
667: {
668: register int i = 0;
669: CORD sum = 0;
670: size_t sum_len = 0;
671:
672: while (sum_len != expected_len) {
673: if (forest[i].c != 0) {
674: sum = CORD_cat(forest[i].c, sum);
675: sum_len += forest[i].len;
676: }
677: i++;
678: }
679: return(sum);
680: }
681:
682: /* Insert the frontier of x into forest. Balanced subtrees are */
683: /* treated as leaves. This potentially adds one to the depth */
684: /* of the final tree. */
685: void CORD_balance_insert(CORD x, size_t len, ForestElement * forest)
686: {
687: register int depth;
688:
689: if (CORD_IS_STRING(x)) {
690: CORD_add_forest(forest, x, len);
691: } else if (IS_CONCATENATION(x)
692: && ((depth = DEPTH(x)) >= MAX_DEPTH
693: || len < min_len[depth])) {
694: register struct Concatenation * conc
695: = &(((CordRep *)x) -> concatenation);
696: size_t left_len = LEFT_LEN(conc);
697:
698: CORD_balance_insert(conc -> left, left_len, forest);
699: CORD_balance_insert(conc -> right, len - left_len, forest);
700: } else /* function or balanced */ {
701: CORD_add_forest(forest, x, len);
702: }
703: }
704:
705:
706: CORD CORD_balance(CORD x)
707: {
708: Forest forest;
709: register size_t len;
710:
711: if (x == 0) return(0);
712: if (CORD_IS_STRING(x)) return(x);
713: if (!min_len_init) CORD_init_min_len();
714: len = LEN(x);
715: CORD_init_forest(forest, len);
716: CORD_balance_insert(x, len, forest);
717: return(CORD_concat_forest(forest, len));
718: }
719:
720:
721: /* Position primitives */
722:
723: /* Private routines to deal with the hard cases only: */
724:
725: /* P contains a prefix of the path to cur_pos. Extend it to a full */
726: /* path and set up leaf info. */
727: /* Return 0 if past the end of cord, 1 o.w. */
728: void CORD__extend_path(register CORD_pos p)
729: {
730: register struct CORD_pe * current_pe = &(p[0].path[p[0].path_len]);
731: register CORD top = current_pe -> pe_cord;
732: register size_t pos = p[0].cur_pos;
733: register size_t top_pos = current_pe -> pe_start_pos;
734: register size_t top_len = GEN_LEN(top);
735:
736: /* Fill in the rest of the path. */
737: while(!CORD_IS_STRING(top) && IS_CONCATENATION(top)) {
738: register struct Concatenation * conc =
739: &(((CordRep *)top) -> concatenation);
740: register size_t left_len;
741:
742: left_len = LEFT_LEN(conc);
743: current_pe++;
744: if (pos >= top_pos + left_len) {
745: current_pe -> pe_cord = top = conc -> right;
746: current_pe -> pe_start_pos = top_pos = top_pos + left_len;
747: top_len -= left_len;
748: } else {
749: current_pe -> pe_cord = top = conc -> left;
750: current_pe -> pe_start_pos = top_pos;
751: top_len = left_len;
752: }
753: p[0].path_len++;
754: }
755: /* Fill in leaf description for fast access. */
756: if (CORD_IS_STRING(top)) {
757: p[0].cur_leaf = top;
758: p[0].cur_start = top_pos;
759: p[0].cur_end = top_pos + top_len;
760: } else {
761: p[0].cur_end = 0;
762: }
763: if (pos >= top_pos + top_len) p[0].path_len = CORD_POS_INVALID;
764: }
765:
766: char CORD__pos_fetch(register CORD_pos p)
767: {
768: /* Leaf is a function node */
769: struct CORD_pe * pe = &((p)[0].path[(p)[0].path_len]);
770: CORD leaf = pe -> pe_cord;
771: register struct Function * f = &(((CordRep *)leaf) -> function);
772:
773: if (!IS_FUNCTION(leaf)) ABORT("CORD_pos_fetch: bad leaf");
774: return ((*(f -> fn))(p[0].cur_pos - pe -> pe_start_pos, f -> client_data));
775: }
776:
777: void CORD__next(register CORD_pos p)
778: {
779: register size_t cur_pos = p[0].cur_pos + 1;
780: register struct CORD_pe * current_pe = &((p)[0].path[(p)[0].path_len]);
781: register CORD leaf = current_pe -> pe_cord;
782:
783: /* Leaf is not a string or we're at end of leaf */
784: p[0].cur_pos = cur_pos;
785: if (!CORD_IS_STRING(leaf)) {
786: /* Function leaf */
787: register struct Function * f = &(((CordRep *)leaf) -> function);
788: register size_t start_pos = current_pe -> pe_start_pos;
789: register size_t end_pos = start_pos + f -> len;
790:
791: if (cur_pos < end_pos) {
792: /* Fill cache and return. */
793: register size_t i;
794: register size_t limit = cur_pos + FUNCTION_BUF_SZ;
795: register CORD_fn fn = f -> fn;
796: register void * client_data = f -> client_data;
797:
798: if (limit > end_pos) {
799: limit = end_pos;
800: }
801: for (i = cur_pos; i < limit; i++) {
802: p[0].function_buf[i - cur_pos] =
803: (*fn)(i - start_pos, client_data);
804: }
805: p[0].cur_start = cur_pos;
806: p[0].cur_leaf = p[0].function_buf;
807: p[0].cur_end = limit;
808: return;
809: }
810: }
811: /* End of leaf */
812: /* Pop the stack until we find two concatenation nodes with the */
813: /* same start position: this implies we were in left part. */
814: {
815: while (p[0].path_len > 0
816: && current_pe[0].pe_start_pos != current_pe[-1].pe_start_pos) {
817: p[0].path_len--;
818: current_pe--;
819: }
820: if (p[0].path_len == 0) {
821: p[0].path_len = CORD_POS_INVALID;
822: return;
823: }
824: }
825: p[0].path_len--;
826: CORD__extend_path(p);
827: }
828:
829: void CORD__prev(register CORD_pos p)
830: {
831: register struct CORD_pe * pe = &(p[0].path[p[0].path_len]);
832:
833: if (p[0].cur_pos == 0) {
834: p[0].path_len = CORD_POS_INVALID;
835: return;
836: }
837: p[0].cur_pos--;
838: if (p[0].cur_pos >= pe -> pe_start_pos) return;
839:
840: /* Beginning of leaf */
841:
842: /* Pop the stack until we find two concatenation nodes with the */
843: /* different start position: this implies we were in right part. */
844: {
845: register struct CORD_pe * current_pe = &((p)[0].path[(p)[0].path_len]);
846:
847: while (p[0].path_len > 0
848: && current_pe[0].pe_start_pos == current_pe[-1].pe_start_pos) {
849: p[0].path_len--;
850: current_pe--;
851: }
852: }
853: p[0].path_len--;
854: CORD__extend_path(p);
855: }
856:
857: #undef CORD_pos_fetch
858: #undef CORD_next
859: #undef CORD_prev
860: #undef CORD_pos_to_index
861: #undef CORD_pos_to_cord
862: #undef CORD_pos_valid
863:
864: char CORD_pos_fetch(register CORD_pos p)
865: {
866: if (p[0].cur_start <= p[0].cur_pos && p[0].cur_pos < p[0].cur_end) {
867: return(p[0].cur_leaf[p[0].cur_pos - p[0].cur_start]);
868: } else {
869: return(CORD__pos_fetch(p));
870: }
871: }
872:
873: void CORD_next(CORD_pos p)
874: {
875: if (p[0].cur_pos < p[0].cur_end - 1) {
876: p[0].cur_pos++;
877: } else {
878: CORD__next(p);
879: }
880: }
881:
882: void CORD_prev(CORD_pos p)
883: {
884: if (p[0].cur_end != 0 && p[0].cur_pos > p[0].cur_start) {
885: p[0].cur_pos--;
886: } else {
887: CORD__prev(p);
888: }
889: }
890:
891: size_t CORD_pos_to_index(CORD_pos p)
892: {
893: return(p[0].cur_pos);
894: }
895:
896: CORD CORD_pos_to_cord(CORD_pos p)
897: {
898: return(p[0].path[0].pe_cord);
899: }
900:
901: int CORD_pos_valid(CORD_pos p)
902: {
903: return(p[0].path_len != CORD_POS_INVALID);
904: }
905:
906: void CORD_set_pos(CORD_pos p, CORD x, size_t i)
907: {
908: if (x == CORD_EMPTY) {
909: p[0].path_len = CORD_POS_INVALID;
910: return;
911: }
912: p[0].path[0].pe_cord = x;
913: p[0].path[0].pe_start_pos = 0;
914: p[0].path_len = 0;
915: p[0].cur_pos = i;
916: CORD__extend_path(p);
917: }
E-mail: