/* Assumes i >= 0 and i + n < length(x). */ CORD CORD_substr_closure(CORD x, size_t i, size_t n, CORD_fn f) { register struct substr_args * sa = MK_GC_NEW(struct substr_args); CORD result; if (sa == 0) OUT_OF_MEMORY; sa->sa_cord = (CordRep *)x; sa->sa_index = i; result = CORD_from_fn(f, (void *)sa, n); if (result == CORD_EMPTY) return CORD_EMPTY; /* n == 0 */ ((CordRep *)result) -> function.header = SUBSTR_HDR; return (result); }
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"); }
CORD CORD_chars(char c, size_t i) { return(CORD_from_fn(CORD_nul_func, (void *)(unsigned long)c, i)); }