Exemple #1
0
void test_basics(void)
{
    CORD x = CORD_from_char_star("ab");
    register int i;
    char c;
    CORD y;
    CORD_pos p;

    x = CORD_cat(x,x);
    if (x == CORD_EMPTY) ABORT("CORD_cat(x,x) returned empty cord");
    if (!CORD_IS_STRING(x)) ABORT("short cord should usually be a string");
    if (strcmp(x, "abab") != 0) ABORT("bad CORD_cat result");

    for (i = 1; i < 16; i++) {
        x = CORD_cat(x,x);
    }
    x = CORD_cat(x,"c");
    if (CORD_len(x) != 128*1024+1) ABORT("bad length");

    count = 0;
    if (CORD_iter5(x, 64*1024-1, test_fn, CORD_NO_FN, (void *)13) == 0) {
        ABORT("CORD_iter5 failed");
    }
    if (count != 64*1024 + 2) ABORT("CORD_iter5 failed");

    count = 0;
    CORD_set_pos(p, x, 64*1024-1);
    while(CORD_pos_valid(p)) {
        (void) test_fn(CORD_pos_fetch(p), (void *)13);
    CORD_next(p);
    }
    if (count != 64*1024 + 2) ABORT("Position based iteration failed");

    y = CORD_substr(x, 1023, 5);
    if (!y) ABORT("CORD_substr returned NULL");
    if (!CORD_IS_STRING(y)) ABORT("short cord should usually be a string");
    if (strcmp(y, "babab") != 0) ABORT("bad CORD_substr result");

    y = CORD_substr(x, 1024, 8);
    if (!y) ABORT("CORD_substr returned NULL");
    if (!CORD_IS_STRING(y)) ABORT("short cord should usually be a string");
    if (strcmp(y, "abababab") != 0) ABORT("bad CORD_substr result");

    y = CORD_substr(x, 128*1024-1, 8);
    if (!y) ABORT("CORD_substr returned NULL");
    if (!CORD_IS_STRING(y)) ABORT("short cord should usually be a string");
    if (strcmp(y, "bc") != 0) ABORT("bad CORD_substr result");

    x = CORD_balance(x);
    if (CORD_len(x) != 128*1024+1) ABORT("bad length");

    count = 0;
    if (CORD_iter5(x, 64*1024-1, test_fn, CORD_NO_FN, (void *)13) == 0) {
        ABORT("CORD_iter5 failed");
    }
    if (count != 64*1024 + 2) ABORT("CORD_iter5 failed");

    y = CORD_substr(x, 1023, 5);
    if (!y) ABORT("CORD_substr returned NULL");
    if (!CORD_IS_STRING(y)) ABORT("short cord should usually be a string");
    if (strcmp(y, "babab") != 0) ABORT("bad CORD_substr result");
    y = CORD_from_fn(id_cord_fn, 0, 13);
    i = 0;
    CORD_set_pos(p, y, i);
    while(CORD_pos_valid(p)) {
        c = CORD_pos_fetch(p);
        if(c != i) ABORT("Traversal of function node failed");
    CORD_next(p); i++;
    }
    if (i != 13) ABORT("Bad apparent length for function node");
}
Exemple #2
0
CORD CORD_cat_char_star(CORD x, const char * y, size_t leny)
{
    register size_t result_len;
    register size_t lenx;
    register int depth;

    if (x == CORD_EMPTY) return(y);
    if (leny == 0) return(x);
    if (CORD_IS_STRING(x)) {
        lenx = strlen(x);
        result_len = lenx + leny;
        if (result_len <= SHORT_LIMIT) {
            register char * result = MK_GC_MALLOC_ATOMIC(result_len+1);

            if (result == 0) OUT_OF_MEMORY;
            memcpy(result, x, lenx);
            memcpy(result + lenx, y, leny);
            result[result_len] = '\0';
            return((CORD) result);
        } else {
            depth = 1;
        }
    } else {
        register CORD right;
        register CORD left;
        register char * new_right;
        register size_t right_len;

        lenx = LEN(x);

        if (leny <= SHORT_LIMIT/2
            && IS_CONCATENATION(x)
            && CORD_IS_STRING(right = ((CordRep *)x) -> concatenation.right)) {
            /* Merge y into right part of x. */
            if (!CORD_IS_STRING(left = ((CordRep *)x) -> concatenation.left)) {
                right_len = lenx - LEN(left);
            } else if (((CordRep *)x) -> concatenation.left_len != 0) {
                right_len = lenx - ((CordRep *)x) -> concatenation.left_len;
            } else {
                right_len = strlen(right);
            }
            result_len = right_len + leny;  /* length of new_right */
            if (result_len <= SHORT_LIMIT) {
                new_right = MK_GC_MALLOC_ATOMIC(result_len + 1);
                if (new_right == 0) OUT_OF_MEMORY;
                memcpy(new_right, right, right_len);
                memcpy(new_right + right_len, y, leny);
                new_right[result_len] = '\0';
                y = new_right;
                leny = result_len;
                x = left;
                lenx -= right_len;
                /* Now fall through to concatenate the two pieces: */
            }
            if (CORD_IS_STRING(x)) {
                depth = 1;
            } else {
                depth = DEPTH(x) + 1;
            }
        } else {
            depth = DEPTH(x) + 1;
        }
        result_len = lenx + leny;
    }
    {
      /* The general case; lenx, result_len is known: */
        register struct Concatenation * result;

        result = MK_GC_NEW(struct Concatenation);
        if (result == 0) OUT_OF_MEMORY;
        result->header = CONCAT_HDR;
        result->depth = depth;
        if (lenx <= MAX_LEFT_LEN) result->left_len = lenx;
        result->len = result_len;
        result->left = x;
        result->right = y;
        if (depth >= MAX_DEPTH) {
            return(CORD_balance((CORD)result));
        } else {
            return((CORD) result);
        }
    }
}
Exemple #3
0
/* A version of CORD_substr that assumes i >= 0, n > 0, and i + n < length(x).*/
CORD CORD_substr_checked(CORD x, size_t i, size_t n)
{
    if (CORD_IS_STRING(x)) {
        if (n > SUBSTR_LIMIT) {
            return(CORD_substr_closure(x, i, n, CORD_index_access_fn));
        } else {
            register char * result = MK_GC_MALLOC_ATOMIC(n+1);

            if (result == 0) OUT_OF_MEMORY;
            strncpy(result, x+i, n);
            result[n] = '\0';
            return(result);
        }
    } else if (IS_CONCATENATION(x)) {
        register struct Concatenation * conc
                        = &(((CordRep *)x) -> concatenation);
        register size_t left_len;
        register size_t right_len;

        left_len = LEFT_LEN(conc);
        right_len = conc -> len - left_len;
        if (i >= left_len) {
            if (n == right_len) return(conc -> right);
            return(CORD_substr_checked(conc -> right, i - left_len, n));
        } else if (i+n <= left_len) {
            if (n == left_len) return(conc -> left);
            return(CORD_substr_checked(conc -> left, i, n));
        } else {
            /* Need at least one character from each side. */
            register CORD left_part;
            register CORD right_part;
            register size_t left_part_len = left_len - i;

            if (i == 0) {
                left_part = conc -> left;
            } else {
                left_part = CORD_substr_checked(conc -> left, i, left_part_len);
            }
            if (i + n == right_len + left_len) {
                 right_part = conc -> right;
            } else {
                 right_part = CORD_substr_checked(conc -> right, 0,
                                                  n - left_part_len);
            }
            return(CORD_cat(left_part, right_part));
        }
    } else /* function */ {
        if (n > SUBSTR_LIMIT) {
            if (IS_SUBSTR(x)) {
                /* Avoid nesting substring nodes.       */
                register struct Function * f = &(((CordRep *)x) -> function);
                register struct substr_args *descr =
                                (struct substr_args *)(f -> client_data);

                return(CORD_substr_closure((CORD)descr->sa_cord,
                                           i + descr->sa_index,
                                           n, f -> fn));
            } else {
                return(CORD_substr_closure(x, i, n, CORD_apply_access_fn));
            }
        } else {
            char * result;
            register struct Function * f = &(((CordRep *)x) -> function);
            char buf[SUBSTR_LIMIT+1];
            register char * p = buf;
            register char c;
            register int j;
            register int lim = i + n;

            for (j = i; j < lim; j++) {
                c = (*(f -> fn))(j, f -> client_data);
                if (c == '\0') {
                    return(CORD_substr_closure(x, i, n, CORD_apply_access_fn));
                }
                *p++ = c;
            }
            result = MK_GC_MALLOC_ATOMIC(n+1);
            if (result == 0) OUT_OF_MEMORY;
            memcpy(result, buf, n);
            result[n] = '\0';
            return(result);
        }
    }
}
Exemple #4
0
const char * CORD_to_const_char_star(CORD x)
{
    if (x == 0) return("");
    if (CORD_IS_STRING(x)) return((const char *)x);
    return(CORD_to_char_star(x));
}