/* See cord.h for definition. We assume i is in range. */ int CORD_iter5(CORD x, size_t i, CORD_iter_fn f1, CORD_batched_iter_fn f2, void * client_data) { if (x == 0) return(0); if (CORD_IS_STRING(x)) { register const char *p = x+i; if (*p == '\0') ABORT("2nd arg to CORD_iter5 too big"); if (f2 != CORD_NO_FN) { return((*f2)(p, client_data)); } else { while (*p) { if ((*f1)(*p, client_data)) return(1); p++; } return(0); } } else if (IS_CONCATENATION(x)) { register struct Concatenation * conc = &(((CordRep *)x) -> concatenation); if (i > 0) { register size_t left_len = LEFT_LEN(conc); if (i >= left_len) { return(CORD_iter5(conc -> right, i - left_len, f1, f2, client_data)); } } if (CORD_iter5(conc -> left, i, f1, f2, client_data)) { return(1); } return(CORD_iter5(conc -> right, 0, f1, f2, client_data)); } else /* function */ { register struct Function * f = &(((CordRep *)x) -> function); register size_t j; register size_t lim = f -> len; for (j = i; j < lim; j++) { if ((*f1)((*(f -> fn))(j, f -> client_data), client_data)) { return(1); } } return(0); } }
int CORD_put(CORD x, FILE * f) { if (CORD_iter5(x, 0, CORD_put_proc, CORD_batched_put_proc, f)) { return(EOF); } else { return(1); } }
/* Assumes len characters are available. */ void CORD_fill_buf(CORD x, size_t i, size_t len, char * buf) { CORD_fill_data fd; fd.len = len; fd.buf = buf; fd.count = 0; (void)CORD_iter5(x, i, CORD_fill_proc, CORD_batched_fill_proc, &fd); }
size_t CORD_chr(CORD x, size_t i, int c) { chr_data d; d.pos = i; d.target = c; if (CORD_iter5(x, i, CORD_chr_proc, CORD_batched_chr_proc, &d)) { return(d.pos); } else { return(CORD_NOT_FOUND); } }
int CORD_iter(CORD x, CORD_iter_fn f1, void * client_data) { return(CORD_iter5(x, 0, f1, CORD_NO_FN, client_data)); }
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"); }