CORD CORD_cat(CORD x, CORD y) { size_t result_len; int depth; size_t lenx; if (x == CORD_EMPTY) return(y); if (y == CORD_EMPTY) return(x); if (CORD_IS_STRING(y)) { return(CORD_cat_char_star(x, y, strlen(y))); } else if (CORD_IS_STRING(x)) { lenx = strlen(x); depth = DEPTH(y) + 1; } else { int depthy = DEPTH(y); lenx = LEN(x); depth = DEPTH(x) + 1; if (depthy >= depth) depth = depthy + 1; } result_len = lenx + LEN(y); { struct Concatenation * result = GC_NEW(struct Concatenation); if (NULL == result) OUT_OF_MEMORY; result->header = CONCAT_HDR; result->depth = (char)depth; if (lenx <= MAX_LEFT_LEN) result->left_len = (unsigned char)lenx; result->len = (word)result_len; result->left = x; GC_PTR_STORE_AND_DIRTY((void *)&result->right, y); GC_reachable_here(x); if (depth >= MAX_DEPTH) { return(CORD_balance((CORD)result)); } else { return((CORD) result); } } }
CORD CORD_cat(CORD x, CORD y) { register size_t result_len; register int depth; register size_t lenx; if (x == CORD_EMPTY) return(y); if (y == CORD_EMPTY) return(x); if (CORD_IS_STRING(y)) { return(CORD_cat_char_star(x, y, strlen(y))); } else if (CORD_IS_STRING(x)) { lenx = strlen(x); depth = DEPTH(y) + 1; } else { register int depthy = DEPTH(y); lenx = LEN(x); depth = DEPTH(x) + 1; if (depthy >= depth) depth = depthy + 1; } result_len = lenx + LEN(y); { 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); } } }
CORD CORD_from_file_eager(FILE * f) { register int c; CORD_ec ecord; CORD_ec_init(ecord); for(;;) { c = getc(f); if (c == 0) { /* Append the right number of NULs */ /* Note that any string of NULs is rpresented in 4 words, */ /* independent of its length. */ register size_t count = 1; CORD_ec_flush_buf(ecord); while ((c = getc(f)) == 0) count++; ecord[0].ec_cord = CORD_cat(ecord[0].ec_cord, CORD_nul(count)); } if (c == EOF) break; CORD_ec_append(ecord, c); } (void) fclose(f); return(CORD_balance(CORD_ec_to_cord(ecord))); }
CORD CORD_cat_char_star(CORD x, const char * y, size_t leny) { size_t result_len; size_t lenx; 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) { char * result = (char *)GC_MALLOC_ATOMIC(result_len + 1); if (result == 0) OUT_OF_MEMORY; # ifdef LINT2 memcpy(result, x, lenx + 1); # else memcpy(result, x, lenx); /* No need to copy the terminating zero */ /* as result[lenx] is written below. */ # endif memcpy(result + lenx, y, leny); result[result_len] = '\0'; return((CORD) result); } else { depth = 1; } } else { CORD right; CORD left; char * new_right; lenx = LEN(x); if (leny <= SHORT_LIMIT/2 && IS_CONCATENATION(x) && CORD_IS_STRING(right = ((CordRep *)x) -> concatenation.right)) { size_t right_len; /* 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 = (char *)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: */ struct Concatenation * result = GC_NEW(struct Concatenation); if (NULL == result) OUT_OF_MEMORY; result->header = CONCAT_HDR; result->depth = (char)depth; if (lenx <= MAX_LEFT_LEN) result->left_len = (unsigned char)lenx; result->len = (word)result_len; result->left = x; GC_PTR_STORE_AND_DIRTY((void *)&result->right, y); GC_reachable_here(x); if (depth >= MAX_DEPTH) { return(CORD_balance((CORD)result)); } else { return((CORD) 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"); }
void test_extras(void) { # define FNAME1 "cordtst1.tmp" /* short name (8+3) for portability */ # define FNAME2 "cordtst2.tmp" register int i; CORD y = "abcdefghijklmnopqrstuvwxyz0123456789"; CORD x = "{}"; CORD w, z; FILE *f; FILE *f1a, *f1b, *f2; w = CORD_cat(CORD_cat(y,y),y); z = CORD_catn(3,y,y,y); if (CORD_cmp(w,z) != 0) ABORT("CORD_catn comparison wrong"); for (i = 1; i < 100; i++) { x = CORD_cat(x, y); } z = CORD_balance(x); if (CORD_cmp(x,z) != 0) ABORT("balanced string comparison wrong"); if (CORD_cmp(x,CORD_cat(z, CORD_nul(13))) >= 0) ABORT("comparison 2"); if (CORD_cmp(CORD_cat(x, CORD_nul(13)), z) <= 0) ABORT("comparison 3"); if (CORD_cmp(x,CORD_cat(z, "13")) >= 0) ABORT("comparison 4"); if ((f = fopen(FNAME1, "w")) == 0) ABORT("open failed"); if (CORD_put(z,f) == EOF) ABORT("CORD_put failed"); if (fclose(f) == EOF) ABORT("fclose failed"); f1a = fopen(FNAME1, "rb"); if (!f1a) ABORT("Unable to open " FNAME1); w = CORD_from_file(f1a); if (CORD_len(w) != CORD_len(z)) ABORT("file length wrong"); if (CORD_cmp(w,z) != 0) ABORT("file comparison wrong"); if (CORD_cmp(CORD_substr(w, 50*36+2, 36), y) != 0) ABORT("file substr wrong"); f1b = fopen(FNAME1, "rb"); if (!f1b) ABORT("2nd open failed: " FNAME1); z = CORD_from_file_lazy(f1b); if (CORD_cmp(w,z) != 0) ABORT("File conversions differ"); if (CORD_chr(w, 0, '9') != 37) ABORT("CORD_chr failed 1"); if (CORD_chr(w, 3, 'a') != 38) ABORT("CORD_chr failed 2"); if (CORD_rchr(w, CORD_len(w) - 1, '}') != 1) ABORT("CORD_rchr failed"); x = y; for (i = 1; i < 14; i++) { x = CORD_cat(x,x); } if ((f = fopen(FNAME2, "w")) == 0) ABORT("2nd open failed"); # ifdef __DJGPP__ /* FIXME: DJGPP workaround. Why does this help? */ if (fflush(f) != 0) ABORT("fflush failed"); # endif if (CORD_put(x,f) == EOF) ABORT("CORD_put failed"); if (fclose(f) == EOF) ABORT("fclose failed"); f2 = fopen(FNAME2, "rb"); if (!f2) ABORT("Unable to open " FNAME2); w = CORD_from_file(f2); if (CORD_len(w) != CORD_len(x)) ABORT("file length wrong"); if (CORD_cmp(w,x) != 0) ABORT("file comparison wrong"); if (CORD_cmp(CORD_substr(w, 1000*36, 36), y) != 0) ABORT("file substr wrong"); if (strcmp(CORD_to_char_star(CORD_substr(w, 1000*36, 36)), y) != 0) ABORT("char * file substr wrong"); if (strcmp(CORD_substr(w, 1000*36, 2), "ab") != 0) ABORT("short file substr wrong"); if (CORD_str(x,1,"9a") != 35) ABORT("CORD_str failed 1"); if (CORD_str(x,0,"9abcdefghijk") != 35) ABORT("CORD_str failed 2"); if (CORD_str(x,0,"9abcdefghijx") != CORD_NOT_FOUND) ABORT("CORD_str failed 3"); if (CORD_str(x,0,"9>") != CORD_NOT_FOUND) ABORT("CORD_str failed 4"); if (remove(FNAME1) != 0) { /* On some systems, e.g. OS2, this may fail if f1 is still open. */ if ((fclose(f1a) == EOF) & (fclose(f1b) == EOF)) ABORT("fclose(f1) failed"); if (remove(FNAME1) != 0) ABORT("remove 1 failed"); } if (remove(FNAME2) != 0) { if (fclose(f2) == EOF) ABORT("fclose(f2) failed"); if (remove(FNAME2) != 0) ABORT("remove 2 failed"); } }
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); } } }
void test_extras() { # if defined(__OS2__) # define FNAME1 "tmp1" # define FNAME2 "tmp2" # elif defined(AMIGA) # define FNAME1 "T:tmp1" # define FNAME2 "T:tmp2" # else # define FNAME1 "/tmp/cord_test" # define FNAME2 "/tmp/cord_test2" # endif register int i; CORD y = "abcdefghijklmnopqrstuvwxyz0123456789"; CORD x = "{}"; CORD w, z; FILE *f; FILE *f1a, *f1b, *f2; w = CORD_cat(CORD_cat(y,y),y); z = CORD_catn(3,y,y,y); if (CORD_cmp(w,z) != 0) ABORT("CORD_catn comparison wrong"); for (i = 1; i < 100; i++) { x = CORD_cat(x, y); } z = CORD_balance(x); if (CORD_cmp(x,z) != 0) ABORT("balanced string comparison wrong"); if (CORD_cmp(x,CORD_cat(z, CORD_nul(13))) >= 0) ABORT("comparison 2"); if (CORD_cmp(CORD_cat(x, CORD_nul(13)), z) <= 0) ABORT("comparison 3"); if (CORD_cmp(x,CORD_cat(z, "13")) >= 0) ABORT("comparison 4"); if ((f = fopen(FNAME1, "w")) == 0) ABORT("open failed"); if (CORD_put(z,f) == EOF) ABORT("CORD_put failed"); if (fclose(f) == EOF) ABORT("fclose failed"); w = CORD_from_file(f1a = fopen(FNAME1, "rb")); if (CORD_len(w) != CORD_len(z)) ABORT("file length wrong"); if (CORD_cmp(w,z) != 0) ABORT("file comparison wrong"); if (CORD_cmp(CORD_substr(w, 50*36+2, 36), y) != 0) ABORT("file substr wrong"); z = CORD_from_file_lazy(f1b = fopen(FNAME1, "rb")); if (CORD_cmp(w,z) != 0) ABORT("File conversions differ"); if (CORD_chr(w, 0, '9') != 37) ABORT("CORD_chr failed 1"); if (CORD_chr(w, 3, 'a') != 38) ABORT("CORD_chr failed 2"); if (CORD_rchr(w, CORD_len(w) - 1, '}') != 1) ABORT("CORD_rchr failed"); x = y; for (i = 1; i < 14; i++) { x = CORD_cat(x,x); } if ((f = fopen(FNAME2, "w")) == 0) ABORT("2nd open failed"); if (CORD_put(x,f) == EOF) ABORT("CORD_put failed"); if (fclose(f) == EOF) ABORT("fclose failed"); w = CORD_from_file(f2 = fopen(FNAME2, "rb")); if (CORD_len(w) != CORD_len(x)) ABORT("file length wrong"); if (CORD_cmp(w,x) != 0) ABORT("file comparison wrong"); if (CORD_cmp(CORD_substr(w, 1000*36, 36), y) != 0) ABORT("file substr wrong"); if (strcmp(CORD_to_char_star(CORD_substr(w, 1000*36, 36)), y) != 0) ABORT("char * file substr wrong"); if (strcmp(CORD_substr(w, 1000*36, 2), "ab") != 0) ABORT("short file substr wrong"); if (CORD_str(x,1,"9a") != 35) ABORT("CORD_str failed 1"); if (CORD_str(x,0,"9abcdefghijk") != 35) ABORT("CORD_str failed 2"); if (CORD_str(x,0,"9abcdefghijx") != CORD_NOT_FOUND) ABORT("CORD_str failed 3"); if (CORD_str(x,0,"9>") != CORD_NOT_FOUND) ABORT("CORD_str failed 4"); if (remove(FNAME1) != 0) { /* On some systems, e.g. OS2, this may fail if f1 is still open. */ if ((fclose(f1a) == EOF) & (fclose(f1b) == EOF)) ABORT("fclose(f1) failed"); if (remove(FNAME1) != 0) ABORT("remove 1 failed"); } if (remove(FNAME2) != 0) { if (fclose(f2) == EOF) ABORT("fclose(f2) failed"); if (remove(FNAME2) != 0) ABORT("remove 2 failed"); } }
void test_extras(void) { # define FNAME1 "cordtst1.tmp" /* short name (8+3) for portability */ # define FNAME2 "cordtst2.tmp" register int i; CORD y = "abcdefghijklmnopqrstuvwxyz0123456789"; CORD x = "{}"; CORD w, z; FILE *f; FILE *f1a, *f1b, *f2; w = CORD_cat(CORD_cat(y,y),y); z = CORD_catn(3,y,y,y); if (CORD_cmp(w,z) != 0) ABORT("CORD_catn comparison wrong"); for (i = 1; i < 100; i++) { x = CORD_cat(x, y); } z = CORD_balance(x); if (CORD_cmp(x,z) != 0) ABORT("balanced string comparison wrong"); if (CORD_cmp(x,CORD_cat(z, CORD_nul(13))) >= 0) ABORT("comparison 2"); if (CORD_cmp(CORD_cat(x, CORD_nul(13)), z) <= 0) ABORT("comparison 3"); if (CORD_cmp(x,CORD_cat(z, "13")) >= 0) ABORT("comparison 4"); if ((f = fopen(FNAME1, "w")) == 0) ABORT("open failed"); if (CORD_put(z,f) == EOF) ABORT("CORD_put failed"); if (fclose(f) == EOF) ABORT("fclose failed"); f1a = fopen(FNAME1, "rb"); if (!f1a) ABORT("Unable to open " FNAME1); w = CORD_from_file(f1a); if (CORD_len(w) != CORD_len(z)) ABORT("file length wrong"); if (CORD_cmp(w,z) != 0) ABORT("file comparison wrong"); if (CORD_cmp(CORD_substr(w, 50*36+2, 36), y) != 0) ABORT("file substr wrong"); f1b = fopen(FNAME1, "rb"); if (!f1b) ABORT("2nd open failed: " FNAME1); z = CORD_from_file_lazy(f1b); if (CORD_cmp(w,z) != 0) ABORT("File conversions differ"); if (CORD_chr(w, 0, '9') != 37) ABORT("CORD_chr failed 1"); if (CORD_chr(w, 3, 'a') != 38) ABORT("CORD_chr failed 2"); if (CORD_rchr(w, CORD_len(w) - 1, '}') != 1) ABORT("CORD_rchr failed"); x = y; for (i = 1; i < 14; i++) { x = CORD_cat(x,x); } if ((f = fopen(FNAME2, "w")) == 0) ABORT("2nd open failed"); # ifdef __DJGPP__ /* FIXME: DJGPP workaround. Why does this help? */ if (fflush(f) != 0) ABORT("fflush failed"); # endif if (CORD_put(x,f) == EOF) ABORT("CORD_put failed"); if (fclose(f) == EOF) ABORT("fclose failed"); f2 = fopen(FNAME2, "rb"); if (!f2) ABORT("Unable to open " FNAME2); w = CORD_from_file(f2); if (CORD_len(w) != CORD_len(x)) ABORT("file length wrong"); if (CORD_cmp(w,x) != 0) ABORT("file comparison wrong"); if (CORD_cmp(CORD_substr(w, 1000*36, 36), y) != 0) ABORT("file substr wrong"); if (strcmp(CORD_to_char_star(CORD_substr(w, 1000*36, 36)), y) != 0) ABORT("char * file substr wrong"); if (strcmp(CORD_substr(w, 1000*36, 2), "ab") != 0) ABORT("short file substr wrong"); if (CORD_str(x,1,"9a") != 35) ABORT("CORD_str failed 1"); if (CORD_str(x,0,"9abcdefghijk") != 35) ABORT("CORD_str failed 2"); if (CORD_str(x,0,"9abcdefghijx") != CORD_NOT_FOUND) ABORT("CORD_str failed 3"); if (CORD_str(x,0,"9>") != CORD_NOT_FOUND) ABORT("CORD_str failed 4"); /* Note: f1a, f1b, f2 handles are closed lazily by CORD library. */ /* TODO: Propose and use CORD_fclose. */ *(CORD volatile *)&w = CORD_EMPTY; *(CORD volatile *)&z = CORD_EMPTY; GC_gcollect(); GC_invoke_finalizers(); /* Of course, this does not guarantee the files are closed. */ if (remove(FNAME1) != 0) { /* On some systems, e.g. OS2, this may fail if f1 is still open. */ /* But we cannot call fclose as it might lead to double close. */ fprintf(stderr, "WARNING: remove(FNAME1) failed\n"); } if (remove(FNAME2) != 0) { fprintf(stderr, "WARNING: remove(FNAME2) failed\n"); } }