Annotation of parser3/src/lib/cord/cordbscs.c, revision 1.9

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: 
1.9     ! misha     618: CORD CORD_substr(CORD x, size_t i, size_t n, size_t len)
1.2       paf       619: {
1.9     ! misha     620:        if(0 == len) len = CORD_len(x);
1.2       paf       621:     if (i >= len || n <= 0) return(0);
                    622:        /* n < 0 is impossible in a correct C implementation, but       */
                    623:        /* quite possible  under SunOS 4.X.                             */
                    624:     if (i + n > len) n = len - i;
                    625: #   ifndef __STDC__
                    626:       if (i < 0) ABORT("CORD_substr: second arg. negative");
                    627:        /* Possible only if both client and C implementation are buggy. */
                    628:        /* But empirically this happens frequently.                     */
                    629: #   endif
                    630:     return(CORD_substr_checked(x, i, n));
                    631: }
                    632: 
                    633: /* See cord.h for definition.  We assume i is in range.        */
                    634: int CORD_iter5(CORD x, size_t i, CORD_iter_fn f1,
                    635:                         CORD_batched_iter_fn f2, void * client_data)
                    636: {
1.3       paf       637:        int result;
1.2       paf       638:     if (x == 0) return(0);
                    639:     if (CORD_IS_STRING(x)) {
                    640:        register const char* p = x+i;
                    641:        
                    642:        if (*p == '\0') ABORT("2nd arg to CORD_iter5 too big");
                    643:         if (f2 != CORD_NO_FN) {
                    644:             return((*f2)(p, client_data));
                    645:         } else {
                    646:            while (*p) {
1.3       paf       647:                 if (result=(*f1)(*p, client_data)) 
                    648:                                        return result;
1.2       paf       649:                 p++;
                    650:            }
                    651:            return(0);
                    652:         }
                    653:     } else if (IS_CONCATENATION(x)) {
1.3       paf       654:        register struct Concatenation * conc = &(((CordRep *)x) -> concatenation);
1.2       paf       655:        if (i > 0) {
                    656:            register size_t left_len = LEFT_LEN(conc);
                    657:            
                    658:            if (i >= left_len) {
                    659:                return(CORD_iter5(conc -> right, i - left_len, f1, f2,
                    660:                                  client_data));
                    661:            }
                    662:        }
1.3       paf       663:                result=CORD_iter5(conc -> left, i, f1, f2, client_data);
                    664:        if (result) return result;
1.2       paf       665:        return(CORD_iter5(conc -> right, 0, f1, f2, client_data));
                    666:     } else /* function */ {
                    667:         register struct Function * f = &(((CordRep *)x) -> function);
                    668:         register size_t j;
                    669:         register size_t lim = f -> len;
                    670:         
                    671:         for (j = i; j < lim; j++) {
1.3       paf       672:             if (result=(*f1)((*(f -> fn))(j, f -> client_data), client_data)) 
                    673:                                return result;
1.2       paf       674:         }
                    675:         return(0);
                    676:     }
                    677: }
1.3       paf       678: 
                    679: /* See cord.h for definition.  We assume i is in range.        */
                    680: int CORD_block_iter(CORD x, size_t i, CORD_block_iter_fn fb, void * client_data)
                    681: {
                    682:        int result;
                    683:     if (x == 0) return(0);
                    684:     if (CORD_IS_STRING(x)) {
                    685:        register const char* p = x+i;
                    686:                const char *b=p;
                    687:                char bc=*b;
                    688:                int pc;
                    689:        
                    690:        if (bc == '\0') ABORT("2nd arg to CORD_iter5 too big");
                    691:            do {
                    692:                        pc=*++p;
                    693:                        if(pc!=bc) {
                    694:                                if (result=fb(bc, p-b, client_data)) 
                    695:                                        return result;
                    696:                                b=p; bc=pc;
                    697:                        }
                    698:            } while (pc);
                    699:            return(0);
                    700:     } else if (IS_CONCATENATION(x)) {
                    701:        register struct Concatenation * conc= &(((CordRep *)x) -> concatenation);
                    702:        if (i > 0) {
                    703:            register size_t left_len = LEFT_LEN(conc);
                    704:            
                    705:            if (i >= left_len) {
                    706:                return(CORD_block_iter(conc -> right, i - left_len, fb, client_data));
                    707:            }
                    708:        }
                    709:                result=CORD_block_iter(conc -> left, i, fb, client_data);
                    710:        if (result) return result;
                    711:        return(CORD_block_iter(conc -> right, 0, fb, client_data));
                    712:     } else /* function */ {
                    713:         register struct Function * f = &(((CordRep *)x) -> function);
                    714:         register size_t lim = f -> len;
                    715:         
                    716:                if(f->fn == CORD_nul_func ) {
                    717:                        if (result=fb((char)(unsigned long)f -> client_data, f -> len-i, client_data)) return result;
                    718:                } else if(f->fn == CORD_apply_access_fn) {
                    719:                        register struct substr_args *descr = (struct substr_args *)f->client_data;
                    720:                        register struct Function * fn_cord = &(descr->sa_cord->function);
                    721: 
                    722:                        if(fn_cord->fn == CORD_nul_func ) {
                    723:                                if (result=fb((char)(unsigned long)fn_cord->client_data, f -> len-i, client_data)) 
                    724:                                        return result;
                    725:                        } else
                    726:                                ABORT("CORD_block_iter:CORD_apply_access_fn:unknown_fn should not happen");
                    727:                } else {
                    728:                        if(f->fn == CORD_index_access_fn)
                    729:                                ABORT("CORD_block_iter:CORD_index_access_fn should not happen");
                    730:                        ABORT("CORD_block_iter:unknown_fn should not happen");
                    731:                }
                    732:     }
                    733:        return(0);
                    734: }
1.2       paf       735:                        
1.3       paf       736: 
1.2       paf       737: #undef CORD_iter
                    738: int CORD_iter(CORD x, CORD_iter_fn f1, void * client_data)
                    739: {
                    740:     return(CORD_iter5(x, 0, f1, CORD_NO_FN, client_data));
                    741: }
                    742: 
                    743: int CORD_riter4(CORD x, size_t i, CORD_iter_fn f1, void * client_data)
                    744: {
                    745:     if (x == 0) return(0);
                    746:     if (CORD_IS_STRING(x)) {
                    747:        register const char* p = x + i;
                    748:        register char c;
                    749:                
                    750:        for(;;) {
                    751:            c = *p;
                    752:            if (c == '\0') ABORT("2nd arg to CORD_riter4 too big");
                    753:             if ((*f1)(c, client_data)) return(1);
                    754:            if (p == x) break;
                    755:             p--;
                    756:        }
                    757:        return(0);
                    758:     } else if (IS_CONCATENATION(x)) {
                    759:        register struct Concatenation * conc
                    760:                        = &(((CordRep *)x) -> concatenation);
                    761:        register CORD left_part = conc -> left;
                    762:        register size_t left_len;
                    763:        
                    764:        left_len = LEFT_LEN(conc);
                    765:        if (i >= left_len) {
                    766:            if (CORD_riter4(conc -> right, i - left_len, f1, client_data)) {
                    767:                return(1);
                    768:            }
                    769:            return(CORD_riter4(left_part, left_len - 1, f1, client_data));
                    770:        } else {
                    771:            return(CORD_riter4(left_part, i, f1, client_data));
                    772:        }
                    773:     } else /* function */ {
                    774:         register struct Function * f = &(((CordRep *)x) -> function);
                    775:         register size_t j;
                    776:         
                    777:         for (j = i; ; j--) {
                    778:             if ((*f1)((*(f -> fn))(j, f -> client_data), client_data)) {
                    779:                 return(1);
                    780:             }
                    781:             if (j == 0) return(0);
                    782:         }
                    783:     }
                    784: }
                    785: 
                    786: int CORD_riter(CORD x, CORD_iter_fn f1, void * client_data)
                    787: {
                    788:     return(CORD_riter4(x, CORD_len(x) - 1, f1, client_data));
                    789: }
                    790: 
                    791: /*
                    792:  * The following functions are concerned with balancing cords.
                    793:  * Strategy:
                    794:  * Scan the cord from left to right, keeping the cord scanned so far
                    795:  * as a forest of balanced trees of exponentialy decreasing length.
                    796:  * When a new subtree needs to be added to the forest, we concatenate all
                    797:  * shorter ones to the new tree in the appropriate order, and then insert
                    798:  * the result into the forest.
                    799:  * Crucial invariants:
                    800:  * 1. The concatenation of the forest (in decreasing order) with the
                    801:  *     unscanned part of the rope is equal to the rope being balanced.
                    802:  * 2. All trees in the forest are balanced.
                    803:  * 3. forest[i] has depth at most i.
                    804:  */
                    805: 
                    806: typedef struct {
                    807:     CORD c;
                    808:     size_t len;                /* Actual length of c   */
                    809: } ForestElement;
                    810: 
                    811: static size_t min_len [ MAX_DEPTH ];
                    812: 
                    813: static int min_len_init = 0;
                    814: 
                    815: int CORD_max_len;
                    816: 
                    817: typedef ForestElement Forest [ MAX_DEPTH ];
                    818:                        /* forest[i].len >= fib(i+1)            */
                    819:                        /* The string is the concatenation      */
                    820:                        /* of the forest in order of DECREASING */
                    821:                        /* indices.                             */
                    822: 
                    823: void CORD_init_min_len()
                    824: {
                    825:     register int i;
                    826:     register size_t last, previous, current;
                    827:         
                    828:     min_len[0] = previous = 1;
                    829:     min_len[1] = last = 2;
                    830:     for (i = 2; i < MAX_DEPTH; i++) {
                    831:        current = last + previous;
                    832:        if (current < last) /* overflow */ current = last;
                    833:        min_len[i] = current;
                    834:        previous = last;
                    835:        last = current;
                    836:     }
                    837:     CORD_max_len = last - 1;
                    838:     min_len_init = 1;
                    839: }
                    840: 
                    841: 
                    842: void CORD_init_forest(ForestElement * forest, size_t max_len)
                    843: {
                    844:     register int i;
                    845:     
                    846:     for (i = 0; i < MAX_DEPTH; i++) {
                    847:        forest[i].c = 0;
                    848:        if (min_len[i] > max_len) return;
                    849:     }
                    850:     ABORT("Cord too long");
                    851: }
                    852: 
                    853: /* Add a leaf to the appropriate level in the forest, cleaning         */
                    854: /* out lower levels as necessary.                                      */
                    855: /* Also works if x is a balanced tree of concatenations; however       */
                    856: /* in this case an extra concatenation node may be inserted above x;   */
                    857: /* This node should not be counted in the statement of the invariants. */
                    858: void CORD_add_forest(ForestElement * forest, CORD x, size_t len)
                    859: {
                    860:     register int i = 0;
                    861:     register CORD sum = CORD_EMPTY;
                    862:     register size_t sum_len = 0;
                    863:     
                    864:     while (len > min_len[i + 1]) {
                    865:        if (forest[i].c != 0) {
                    866:            sum = CORD_cat(forest[i].c, sum);
                    867:            sum_len += forest[i].len;
                    868:            forest[i].c = 0;
                    869:        }
                    870:         i++;
                    871:     }
                    872:     /* Sum has depth at most 1 greter than what would be required      */
                    873:     /* for balance.                                                    */
                    874:     sum = CORD_cat(sum, x);
                    875:     sum_len += len;
                    876:     /* If x was a leaf, then sum is now balanced.  To see this         */
                    877:     /* consider the two cases in which forest[i-1] either is or is     */
                    878:     /* not empty.                                                      */
                    879:     while (sum_len >= min_len[i]) {
                    880:        if (forest[i].c != 0) {
                    881:            sum = CORD_cat(forest[i].c, sum);
                    882:            sum_len += forest[i].len;
                    883:            /* This is again balanced, since sum was balanced, and has  */
                    884:            /* allowable depth that differs from i by at most 1.        */
                    885:            forest[i].c = 0;
                    886:        }
                    887:         i++;
                    888:     }
                    889:     i--;
                    890:     forest[i].c = sum;
                    891:     forest[i].len = sum_len;
                    892: }
                    893: 
                    894: CORD CORD_concat_forest(ForestElement * forest, size_t expected_len)
                    895: {
                    896:     register int i = 0;
                    897:     CORD sum = 0;
                    898:     size_t sum_len = 0;
                    899:     
                    900:     while (sum_len != expected_len) {
                    901:        if (forest[i].c != 0) {
                    902:            sum = CORD_cat(forest[i].c, sum);
                    903:            sum_len += forest[i].len;
                    904:        }
                    905:         i++;
                    906:     }
                    907:     return(sum);
                    908: }
                    909: 
                    910: /* Insert the frontier of x into forest.  Balanced subtrees are        */
                    911: /* treated as leaves.  This potentially adds one to the depth  */
                    912: /* of the final tree.                                          */
                    913: void CORD_balance_insert(CORD x, size_t len, ForestElement * forest)
                    914: {
                    915:     register int depth;
                    916:     
                    917:     if (CORD_IS_STRING(x)) {
                    918:         CORD_add_forest(forest, x, len);
                    919:     } else if (IS_CONCATENATION(x)
                    920:                && ((depth = DEPTH(x)) >= MAX_DEPTH
                    921:                    || len < min_len[depth])) {
                    922:        register struct Concatenation * conc
                    923:                        = &(((CordRep *)x) -> concatenation);
                    924:        size_t left_len = LEFT_LEN(conc);
                    925:        
                    926:        CORD_balance_insert(conc -> left, left_len, forest);
                    927:        CORD_balance_insert(conc -> right, len - left_len, forest);
                    928:     } else /* function or balanced */ {
                    929:        CORD_add_forest(forest, x, len);
                    930:     }
                    931: }
                    932: 
                    933: 
                    934: CORD CORD_balance(CORD x)
                    935: {
                    936:     Forest forest;
                    937:     register size_t len;
                    938:     
                    939:     if (x == 0) return(0);
                    940:     if (CORD_IS_STRING(x)) return(x);
                    941:     if (!min_len_init) CORD_init_min_len();
                    942:     len = LEN(x);
                    943:     CORD_init_forest(forest, len);
                    944:     CORD_balance_insert(x, len, forest);
                    945:     return(CORD_concat_forest(forest, len));
                    946: }
                    947: 
                    948: 
                    949: /* Position primitives */
                    950: 
                    951: /* Private routines to deal with the hard cases only: */
                    952: 
                    953: /* P contains a prefix of the  path to cur_pos.        Extend it to a full     */
                    954: /* path and set up leaf info.                                          */
                    955: /* Return 0 if past the end of cord, 1 o.w.                            */
                    956: void CORD__extend_path(register CORD_pos p)
                    957: {
                    958:      register struct CORD_pe * current_pe = &(p[0].path[p[0].path_len]);
                    959:      register CORD top = current_pe -> pe_cord;
                    960:      register size_t pos = p[0].cur_pos;
                    961:      register size_t top_pos = current_pe -> pe_start_pos;
                    962:      register size_t top_len = GEN_LEN(top);
                    963:      
                    964:      /* Fill in the rest of the path. */
                    965:        while(!CORD_IS_STRING(top) && IS_CONCATENATION(top)) {
                    966:         register struct Concatenation * conc =
                    967:                        &(((CordRep *)top) -> concatenation);
                    968:         register size_t left_len;
                    969:         
                    970:         left_len = LEFT_LEN(conc);
                    971:         current_pe++;
                    972:         if (pos >= top_pos + left_len) {
                    973:             current_pe -> pe_cord = top = conc -> right;
                    974:             current_pe -> pe_start_pos = top_pos = top_pos + left_len;
                    975:             top_len -= left_len;
                    976:         } else {
                    977:             current_pe -> pe_cord = top = conc -> left;
                    978:             current_pe -> pe_start_pos = top_pos;
                    979:             top_len = left_len;
                    980:         }
                    981:         p[0].path_len++;
                    982:        }
                    983:      /* Fill in leaf description for fast access. */
                    984:        if (CORD_IS_STRING(top)) {
                    985:          p[0].cur_leaf = top;
                    986:          p[0].cur_start = top_pos;
                    987:          p[0].cur_end = top_pos + top_len;
                    988:        } else {
                    989:          p[0].cur_end = 0;
                    990:        }
                    991:        if (pos >= top_pos + top_len) p[0].path_len = CORD_POS_INVALID;
                    992: }
                    993: 
                    994: char CORD__pos_fetch(register CORD_pos p)
                    995: {
                    996:     /* Leaf is a function node */
                    997:     struct CORD_pe * pe = &((p)[0].path[(p)[0].path_len]);
                    998:     CORD leaf = pe -> pe_cord;
                    999:     register struct Function * f = &(((CordRep *)leaf) -> function);
                   1000:     
                   1001:     if (!IS_FUNCTION(leaf)) ABORT("CORD_pos_fetch: bad leaf");
                   1002:     return ((*(f -> fn))(p[0].cur_pos - pe -> pe_start_pos, f -> client_data));
                   1003: }
                   1004: 
                   1005: void CORD__next(register CORD_pos p)
                   1006: {
                   1007:     register size_t cur_pos = p[0].cur_pos + 1;
                   1008:     register struct CORD_pe * current_pe = &((p)[0].path[(p)[0].path_len]);
                   1009:     register CORD leaf = current_pe -> pe_cord;
                   1010:     
                   1011:     /* Leaf is not a string or we're at end of leaf */
                   1012:     p[0].cur_pos = cur_pos;
                   1013:     if (!CORD_IS_STRING(leaf)) {
                   1014:        /* Function leaf        */
                   1015:        register struct Function * f = &(((CordRep *)leaf) -> function);
                   1016:        register size_t start_pos = current_pe -> pe_start_pos;
                   1017:        register size_t end_pos = start_pos + f -> len;
                   1018:        
                   1019:        if (cur_pos < end_pos) {
                   1020:          /* Fill cache and return. */
                   1021:            register size_t i;
                   1022:            register size_t limit = cur_pos + FUNCTION_BUF_SZ;
                   1023:            register CORD_fn fn = f -> fn;
                   1024:            register void * client_data = f -> client_data;
                   1025:            
                   1026:            if (limit > end_pos) {
                   1027:                limit = end_pos;
                   1028:            }
                   1029:            for (i = cur_pos; i < limit; i++) {
                   1030:                p[0].function_buf[i - cur_pos] =
                   1031:                        (*fn)(i - start_pos, client_data);
                   1032:            }
                   1033:            p[0].cur_start = cur_pos;
                   1034:            p[0].cur_leaf = p[0].function_buf;
                   1035:            p[0].cur_end = limit;
                   1036:            return;
                   1037:        }
                   1038:     }
                   1039:     /* End of leaf     */
                   1040:     /* Pop the stack until we find two concatenation nodes with the    */
                   1041:     /* same start position: this implies we were in left part.         */
                   1042:     {
                   1043:        while (p[0].path_len > 0
                   1044:               && current_pe[0].pe_start_pos != current_pe[-1].pe_start_pos) {
                   1045:            p[0].path_len--;
                   1046:            current_pe--;
                   1047:        }
                   1048:        if (p[0].path_len == 0) {
                   1049:            p[0].path_len = CORD_POS_INVALID;
                   1050:             return;
                   1051:        }
                   1052:     }
                   1053:     p[0].path_len--;
                   1054:     CORD__extend_path(p);
                   1055: }
                   1056: 
                   1057: void CORD__prev(register CORD_pos p)
                   1058: {
                   1059:     register struct CORD_pe * pe = &(p[0].path[p[0].path_len]);
                   1060:     
                   1061:     if (p[0].cur_pos == 0) {
                   1062:         p[0].path_len = CORD_POS_INVALID;
                   1063:         return;
                   1064:     }
                   1065:     p[0].cur_pos--;
                   1066:     if (p[0].cur_pos >= pe -> pe_start_pos) return;
                   1067:     
                   1068:     /* Beginning of leaf       */
                   1069:     
                   1070:     /* Pop the stack until we find two concatenation nodes with the    */
                   1071:     /* different start position: this implies we were in right part.   */
                   1072:     {
                   1073:        register struct CORD_pe * current_pe = &((p)[0].path[(p)[0].path_len]);
                   1074:        
                   1075:        while (p[0].path_len > 0
                   1076:               && current_pe[0].pe_start_pos == current_pe[-1].pe_start_pos) {
                   1077:            p[0].path_len--;
                   1078:            current_pe--;
                   1079:        }
                   1080:     }
                   1081:     p[0].path_len--;
                   1082:     CORD__extend_path(p);
                   1083: }
                   1084: 
                   1085: #undef CORD_pos_fetch
                   1086: #undef CORD_next
                   1087: #undef CORD_prev
                   1088: #undef CORD_pos_to_index
                   1089: #undef CORD_pos_to_cord
                   1090: #undef CORD_pos_valid
                   1091: 
                   1092: char CORD_pos_fetch(register CORD_pos p)
                   1093: {
                   1094:     if (p[0].cur_start <= p[0].cur_pos && p[0].cur_pos < p[0].cur_end) {
                   1095:        return(p[0].cur_leaf[p[0].cur_pos - p[0].cur_start]);
                   1096:     } else {
                   1097:         return(CORD__pos_fetch(p));
                   1098:     }
                   1099: }
                   1100: 
                   1101: void CORD_next(CORD_pos p)
                   1102: {
                   1103:     if (p[0].cur_pos < p[0].cur_end - 1) {
                   1104:        p[0].cur_pos++;
                   1105:     } else {
                   1106:        CORD__next(p);
                   1107:     }
                   1108: }
                   1109: 
                   1110: void CORD_prev(CORD_pos p)
                   1111: {
                   1112:     if (p[0].cur_end != 0 && p[0].cur_pos > p[0].cur_start) {
                   1113:        p[0].cur_pos--;
                   1114:     } else {
                   1115:        CORD__prev(p);
                   1116:     }
                   1117: }
                   1118: 
                   1119: size_t CORD_pos_to_index(CORD_pos p)
                   1120: {
                   1121:     return(p[0].cur_pos);
                   1122: }
                   1123: 
                   1124: CORD CORD_pos_to_cord(CORD_pos p)
                   1125: {
                   1126:     return(p[0].path[0].pe_cord);
                   1127: }
                   1128: 
                   1129: int CORD_pos_valid(CORD_pos p)
                   1130: {
                   1131:     return(p[0].path_len != CORD_POS_INVALID);
                   1132: }
                   1133: 
                   1134: void CORD_set_pos(CORD_pos p, CORD x, size_t i)
                   1135: {
                   1136:     if (x == CORD_EMPTY) {
                   1137:        p[0].path_len = CORD_POS_INVALID;
                   1138:        return;
                   1139:     }
                   1140:     p[0].path[0].pe_cord = x;
                   1141:     p[0].path[0].pe_start_pos = 0;
                   1142:     p[0].path_len = 0;
                   1143:     p[0].cur_pos = i;
                   1144:     CORD__extend_path(p);
                   1145: }

E-mail: