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

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

E-mail: