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