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