| 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 |
|
| 14 |
#ifdef HAVE_CONFIG_H |
| 15 |
# include "config.h" |
| 16 |
#endif |
| 17 |
#ifndef CORD_BUILD |
| 18 |
# define CORD_BUILD |
| 19 |
#endif |
| 20 |
|
| 21 |
# include "gc.h" |
| 22 |
# include "cord.h" |
| 23 |
# include <stdlib.h> |
| 24 |
# include <stdio.h> |
| 25 |
# include <string.h> |
| 26 |
|
| 27 |
/* An implementation of the cord primitives. These are the only */ |
| 28 |
/* Functions that understand the representation. We perform only */ |
| 29 |
/* minimal checks on arguments to these functions. Out of bounds */ |
| 30 |
/* arguments to the iteration functions may result in client functions */ |
| 31 |
/* invoked on garbage data. In most cases, client functions should be */ |
| 32 |
/* programmed defensively enough that this does not result in memory */ |
| 33 |
/* smashes. */ |
| 34 |
|
| 35 |
typedef void (* oom_fn)(void); |
| 36 |
|
| 37 |
oom_fn CORD_oom_fn = (oom_fn) 0; |
| 38 |
|
| 39 |
# define OUT_OF_MEMORY { if (CORD_oom_fn != (oom_fn) 0) (*CORD_oom_fn)(); \ |
| 40 |
ABORT("Out of memory"); } |
| 41 |
# define ABORT(msg) { fprintf(stderr, "%s\n", msg); abort(); } |
| 42 |
|
| 43 |
typedef unsigned long word; |
| 44 |
|
| 45 |
struct Concatenation { |
| 46 |
char null; |
| 47 |
char header; |
| 48 |
char depth; /* concatenation nesting depth. */ |
| 49 |
unsigned char left_len; |
| 50 |
/* Length of left child if it is sufficiently */ |
| 51 |
/* short; 0 otherwise. */ |
| 52 |
# define MAX_LEFT_LEN 255 |
| 53 |
word len; |
| 54 |
CORD left; /* length(left) > 0 */ |
| 55 |
CORD right; /* length(right) > 0 */ |
| 56 |
}; |
| 57 |
|
| 58 |
struct Function { |
| 59 |
char null; |
| 60 |
char header; |
| 61 |
char depth; /* always 0 */ |
| 62 |
char left_len; /* always 0 */ |
| 63 |
word len; |
| 64 |
CORD_fn fn; |
| 65 |
void * client_data; |
| 66 |
}; |
| 67 |
|
| 68 |
struct Generic { |
| 69 |
char null; |
| 70 |
char header; |
| 71 |
char depth; |
| 72 |
char left_len; |
| 73 |
word len; |
| 74 |
}; |
| 75 |
|
| 76 |
typedef union { |
| 77 |
struct Concatenation concatenation; |
| 78 |
struct Function function; |
| 79 |
struct Generic generic; |
| 80 |
char string[1]; |
| 81 |
} CordRep; |
| 82 |
|
| 83 |
# define CONCAT_HDR 1 |
| 84 |
|
| 85 |
# define FN_HDR 4 |
| 86 |
# define SUBSTR_HDR 6 |
| 87 |
/* Substring nodes are a special case of function nodes. */ |
| 88 |
/* The client_data field is known to point to a substr_args */ |
| 89 |
/* structure, and the function is either CORD_apply_access_fn */ |
| 90 |
/* or CORD_index_access_fn. */ |
| 91 |
|
| 92 |
/* The following may be applied only to function and concatenation nodes: */ |
| 93 |
#define IS_CONCATENATION(s) (((CordRep *)s)->generic.header == CONCAT_HDR) |
| 94 |
|
| 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 |
|
| 112 |
|
| 113 |
/* Dump the internal representation of x to stdout, with initial */ |
| 114 |
/* indentation level n. */ |
| 115 |
void CORD_dump_inner(CORD x, unsigned n) |
| 116 |
{ |
| 117 |
size_t i; |
| 118 |
|
| 119 |
for (i = 0; i < (size_t)n; i++) { |
| 120 |
fputs(" ", stdout); |
| 121 |
} |
| 122 |
if (x == 0) { |
| 123 |
fputs("NIL\n", stdout); |
| 124 |
} else if (CORD_IS_STRING(x)) { |
| 125 |
for (i = 0; i <= SHORT_LIMIT; i++) { |
| 126 |
if (x[i] == '\0') break; |
| 127 |
putchar(x[i]); |
| 128 |
} |
| 129 |
if (x[i] != '\0') fputs("...", stdout); |
| 130 |
putchar('\n'); |
| 131 |
} else if (IS_CONCATENATION(x)) { |
| 132 |
struct Concatenation * conc = &(((CordRep *)x) -> concatenation); |
| 133 |
|
| 134 |
printf("Concatenation: %p (len: %d, depth: %d)\n", |
| 135 |
(void *)x, (int)(conc -> len), (int)(conc -> depth)); |
| 136 |
CORD_dump_inner(conc -> left, n+1); |
| 137 |
CORD_dump_inner(conc -> right, n+1); |
| 138 |
} else /* function */ { |
| 139 |
struct Function * func = &(((CordRep *)x) -> function); |
| 140 |
|
| 141 |
if (IS_SUBSTR(x)) printf("(Substring) "); |
| 142 |
printf("Function: %p (len: %d): ", (void *)x, (int)(func -> len)); |
| 143 |
for (i = 0; i < 20 && i < func -> len; i++) { |
| 144 |
putchar((*(func -> fn))(i, func -> client_data)); |
| 145 |
} |
| 146 |
if (i < func -> len) fputs("...", stdout); |
| 147 |
putchar('\n'); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/* Dump the internal representation of x to stdout */ |
| 152 |
void CORD_dump(CORD x) |
| 153 |
{ |
| 154 |
CORD_dump_inner(x, 0); |
| 155 |
fflush(stdout); |
| 156 |
} |
| 157 |
|
| 158 |
CORD CORD_cat_char_star(CORD x, const char * y, size_t leny) |
| 159 |
{ |
| 160 |
size_t result_len; |
| 161 |
size_t lenx; |
| 162 |
int depth; |
| 163 |
|
| 164 |
if (x == CORD_EMPTY) return(y); |
| 165 |
if (leny == 0) return(x); |
| 166 |
if (CORD_IS_STRING(x)) { |
| 167 |
lenx = strlen(x); |
| 168 |
result_len = lenx + leny; |
| 169 |
if (result_len <= SHORT_LIMIT) { |
| 170 |
char * result = (char *)GC_MALLOC_ATOMIC(result_len + 1); |
| 171 |
|
| 172 |
if (result == 0) OUT_OF_MEMORY; |
| 173 |
# ifdef LINT2 |
| 174 |
memcpy(result, x, lenx + 1); |
| 175 |
# else |
| 176 |
memcpy(result, x, lenx); |
| 177 |
/* No need to copy the terminating zero */ |
| 178 |
/* as result[lenx] is written below. */ |
| 179 |
# endif |
| 180 |
memcpy(result + lenx, y, leny); |
| 181 |
result[result_len] = '\0'; |
| 182 |
return((CORD) result); |
| 183 |
} else { |
| 184 |
depth = 1; |
| 185 |
} |
| 186 |
} else { |
| 187 |
CORD right; |
| 188 |
CORD left; |
| 189 |
char * new_right; |
| 190 |
|
| 191 |
lenx = LEN(x); |
| 192 |
|
| 193 |
if (leny <= SHORT_LIMIT/2 |
| 194 |
&& IS_CONCATENATION(x) |
| 195 |
&& CORD_IS_STRING(right = ((CordRep *)x) -> concatenation.right)) { |
| 196 |
size_t right_len; |
| 197 |
|
| 198 |
/* Merge y into right part of x. */ |
| 199 |
if (!CORD_IS_STRING(left = ((CordRep *)x) -> concatenation.left)) { |
| 200 |
right_len = lenx - LEN(left); |
| 201 |
} else if (((CordRep *)x) -> concatenation.left_len != 0) { |
| 202 |
right_len = lenx - ((CordRep *)x) -> concatenation.left_len; |
| 203 |
} else { |
| 204 |
right_len = strlen(right); |
| 205 |
} |
| 206 |
result_len = right_len + leny; /* length of new_right */ |
| 207 |
if (result_len <= SHORT_LIMIT) { |
| 208 |
new_right = (char *)GC_MALLOC_ATOMIC(result_len + 1); |
| 209 |
if (new_right == 0) OUT_OF_MEMORY; |
| 210 |
memcpy(new_right, right, right_len); |
| 211 |
memcpy(new_right + right_len, y, leny); |
| 212 |
new_right[result_len] = '\0'; |
| 213 |
y = new_right; |
| 214 |
leny = result_len; |
| 215 |
x = left; |
| 216 |
lenx -= right_len; |
| 217 |
/* Now fall through to concatenate the two pieces: */ |
| 218 |
} |
| 219 |
if (CORD_IS_STRING(x)) { |
| 220 |
depth = 1; |
| 221 |
} else { |
| 222 |
depth = DEPTH(x) + 1; |
| 223 |
} |
| 224 |
} else { |
| 225 |
depth = DEPTH(x) + 1; |
| 226 |
} |
| 227 |
result_len = lenx + leny; |
| 228 |
} |
| 229 |
{ |
| 230 |
/* The general case; lenx, result_len is known: */ |
| 231 |
struct Concatenation * result = GC_NEW(struct Concatenation); |
| 232 |
|
| 233 |
if (NULL == result) OUT_OF_MEMORY; |
| 234 |
result->header = CONCAT_HDR; |
| 235 |
result->depth = (char)depth; |
| 236 |
if (lenx <= MAX_LEFT_LEN) |
| 237 |
result->left_len = (unsigned char)lenx; |
| 238 |
result->len = (word)result_len; |
| 239 |
result->left = x; |
| 240 |
GC_PTR_STORE_AND_DIRTY((void *)&result->right, y); |
| 241 |
GC_reachable_here(x); |
| 242 |
if (depth >= MAX_DEPTH) { |
| 243 |
return(CORD_balance((CORD)result)); |
| 244 |
} else { |
| 245 |
return((CORD) result); |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
CORD CORD_cat(CORD x, CORD y) |
| 252 |
{ |
| 253 |
size_t result_len; |
| 254 |
int depth; |
| 255 |
size_t lenx; |
| 256 |
|
| 257 |
if (x == CORD_EMPTY) return(y); |
| 258 |
if (y == CORD_EMPTY) return(x); |
| 259 |
if (CORD_IS_STRING(y)) { |
| 260 |
return(CORD_cat_char_star(x, y, strlen(y))); |
| 261 |
} else if (CORD_IS_STRING(x)) { |
| 262 |
lenx = strlen(x); |
| 263 |
depth = DEPTH(y) + 1; |
| 264 |
} else { |
| 265 |
int depthy = DEPTH(y); |
| 266 |
|
| 267 |
lenx = LEN(x); |
| 268 |
depth = DEPTH(x) + 1; |
| 269 |
if (depthy >= depth) depth = depthy + 1; |
| 270 |
} |
| 271 |
result_len = lenx + LEN(y); |
| 272 |
{ |
| 273 |
struct Concatenation * result = GC_NEW(struct Concatenation); |
| 274 |
|
| 275 |
if (NULL == result) OUT_OF_MEMORY; |
| 276 |
result->header = CONCAT_HDR; |
| 277 |
result->depth = (char)depth; |
| 278 |
if (lenx <= MAX_LEFT_LEN) |
| 279 |
result->left_len = (unsigned char)lenx; |
| 280 |
result->len = (word)result_len; |
| 281 |
result->left = x; |
| 282 |
GC_PTR_STORE_AND_DIRTY((void *)&result->right, y); |
| 283 |
GC_reachable_here(x); |
| 284 |
if (depth >= MAX_DEPTH) { |
| 285 |
return(CORD_balance((CORD)result)); |
| 286 |
} else { |
| 287 |
return((CORD) result); |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
static CordRep *CORD_from_fn_inner(CORD_fn fn, void * client_data, size_t len) |
| 294 |
{ |
| 295 |
if (len == 0) return(0); |
| 296 |
if (len <= SHORT_LIMIT) { |
| 297 |
char * result; |
| 298 |
size_t i; |
| 299 |
char buf[SHORT_LIMIT+1]; |
| 300 |
|
| 301 |
for (i = 0; i < len; i++) { |
| 302 |
char c = (*fn)(i, client_data); |
| 303 |
|
| 304 |
if (c == '\0') goto gen_case; |
| 305 |
buf[i] = c; |
| 306 |
} |
| 307 |
|
| 308 |
result = (char *)GC_MALLOC_ATOMIC(len + 1); |
| 309 |
if (result == 0) OUT_OF_MEMORY; |
| 310 |
memcpy(result, buf, len); |
| 311 |
result[len] = '\0'; |
| 312 |
return (CordRep *)result; |
| 313 |
} |
| 314 |
gen_case: |
| 315 |
{ |
| 316 |
struct Function * result = GC_NEW(struct Function); |
| 317 |
|
| 318 |
if (NULL == result) OUT_OF_MEMORY; |
| 319 |
result->header = FN_HDR; |
| 320 |
/* depth is already 0 */ |
| 321 |
result->len = (word)len; |
| 322 |
result->fn = fn; |
| 323 |
GC_PTR_STORE_AND_DIRTY(&result->client_data, client_data); |
| 324 |
return (CordRep *)result; |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
CORD CORD_from_fn(CORD_fn fn, void * client_data, size_t len) |
| 329 |
{ |
| 330 |
return (/* const */ CORD) CORD_from_fn_inner(fn, client_data, len); |
| 331 |
} |
| 332 |
|
| 333 |
size_t CORD_len(CORD x) |
| 334 |
{ |
| 335 |
if (x == 0) { |
| 336 |
return(0); |
| 337 |
} else { |
| 338 |
return(GEN_LEN(x)); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
struct substr_args { |
| 343 |
CordRep * sa_cord; |
| 344 |
size_t sa_index; |
| 345 |
}; |
| 346 |
|
| 347 |
char CORD_index_access_fn(size_t i, void * client_data) |
| 348 |
{ |
| 349 |
struct substr_args *descr = (struct substr_args *)client_data; |
| 350 |
|
| 351 |
return(((char *)(descr->sa_cord))[i + descr->sa_index]); |
| 352 |
} |
| 353 |
|
| 354 |
char CORD_apply_access_fn(size_t i, void * client_data) |
| 355 |
{ |
| 356 |
struct substr_args *descr = (struct substr_args *)client_data; |
| 357 |
struct Function * fn_cord = &(descr->sa_cord->function); |
| 358 |
|
| 359 |
return((*(fn_cord->fn))(i + descr->sa_index, fn_cord->client_data)); |
| 360 |
} |
| 361 |
|
| 362 |
/* A version of CORD_substr that simply returns a function node, thus */ |
| 363 |
/* postponing its work. The fourth argument is a function that may */ |
| 364 |
/* be used for efficient access to the ith character. */ |
| 365 |
/* Assumes i >= 0 and i + n < length(x). */ |
| 366 |
CORD CORD_substr_closure(CORD x, size_t i, size_t n, CORD_fn f) |
| 367 |
{ |
| 368 |
struct substr_args * sa = GC_NEW(struct substr_args); |
| 369 |
CordRep * result; |
| 370 |
|
| 371 |
if (sa == 0) OUT_OF_MEMORY; |
| 372 |
sa->sa_index = i; |
| 373 |
GC_PTR_STORE_AND_DIRTY(&sa->sa_cord, x); |
| 374 |
result = CORD_from_fn_inner(f, (void *)sa, n); |
| 375 |
if ((CORD)result != CORD_EMPTY && 0 == result -> function.null) |
| 376 |
result -> function.header = SUBSTR_HDR; |
| 377 |
return (CORD)result; |
| 378 |
} |
| 379 |
|
| 380 |
# define SUBSTR_LIMIT (10 * SHORT_LIMIT) |
| 381 |
/* Substrings of function nodes and flat strings shorter than */ |
| 382 |
/* this are flat strings. Othewise we use a functional */ |
| 383 |
/* representation, which is significantly slower to access. */ |
| 384 |
|
| 385 |
/* A version of CORD_substr that assumes i >= 0, n > 0, and i + n < length(x).*/ |
| 386 |
CORD CORD_substr_checked(CORD x, size_t i, size_t n) |
| 387 |
{ |
| 388 |
if (CORD_IS_STRING(x)) { |
| 389 |
if (n > SUBSTR_LIMIT) { |
| 390 |
return(CORD_substr_closure(x, i, n, CORD_index_access_fn)); |
| 391 |
} else { |
| 392 |
char * result = (char *)GC_MALLOC_ATOMIC(n + 1); |
| 393 |
|
| 394 |
if (result == 0) OUT_OF_MEMORY; |
| 395 |
strncpy(result, x+i, n); |
| 396 |
result[n] = '\0'; |
| 397 |
return(result); |
| 398 |
} |
| 399 |
} else if (IS_CONCATENATION(x)) { |
| 400 |
struct Concatenation * conc = &(((CordRep *)x) -> concatenation); |
| 401 |
size_t left_len = LEFT_LEN(conc); |
| 402 |
size_t right_len = conc -> len - left_len; |
| 403 |
|
| 404 |
if (i >= left_len) { |
| 405 |
if (n == right_len) return(conc -> right); |
| 406 |
return(CORD_substr_checked(conc -> right, i - left_len, n)); |
| 407 |
} else if (i+n <= left_len) { |
| 408 |
if (n == left_len) return(conc -> left); |
| 409 |
return(CORD_substr_checked(conc -> left, i, n)); |
| 410 |
} else { |
| 411 |
/* Need at least one character from each side. */ |
| 412 |
CORD left_part; |
| 413 |
CORD right_part; |
| 414 |
size_t left_part_len = left_len - i; |
| 415 |
|
| 416 |
if (i == 0) { |
| 417 |
left_part = conc -> left; |
| 418 |
} else { |
| 419 |
left_part = CORD_substr_checked(conc -> left, i, left_part_len); |
| 420 |
} |
| 421 |
if (i + n == right_len + left_len) { |
| 422 |
right_part = conc -> right; |
| 423 |
} else { |
| 424 |
right_part = CORD_substr_checked(conc -> right, 0, |
| 425 |
n - left_part_len); |
| 426 |
} |
| 427 |
return(CORD_cat(left_part, right_part)); |
| 428 |
} |
| 429 |
} else /* function */ { |
| 430 |
if (n > SUBSTR_LIMIT) { |
| 431 |
if (IS_SUBSTR(x)) { |
| 432 |
/* Avoid nesting substring nodes. */ |
| 433 |
struct Function * f = &(((CordRep *)x) -> function); |
| 434 |
struct substr_args *descr = |
| 435 |
(struct substr_args *)(f -> client_data); |
| 436 |
|
| 437 |
return(CORD_substr_closure((CORD)descr->sa_cord, |
| 438 |
i + descr->sa_index, |
| 439 |
n, f -> fn)); |
| 440 |
} else { |
| 441 |
return(CORD_substr_closure(x, i, n, CORD_apply_access_fn)); |
| 442 |
} |
| 443 |
} else { |
| 444 |
char * result; |
| 445 |
struct Function * f = &(((CordRep *)x) -> function); |
| 446 |
char buf[SUBSTR_LIMIT+1]; |
| 447 |
char * p = buf; |
| 448 |
size_t j; |
| 449 |
size_t lim = i + n; |
| 450 |
|
| 451 |
for (j = i; j < lim; j++) { |
| 452 |
char c = (*(f -> fn))(j, f -> client_data); |
| 453 |
|
| 454 |
if (c == '\0') { |
| 455 |
return(CORD_substr_closure(x, i, n, CORD_apply_access_fn)); |
| 456 |
} |
| 457 |
*p++ = c; |
| 458 |
} |
| 459 |
result = (char *)GC_MALLOC_ATOMIC(n + 1); |
| 460 |
if (result == 0) OUT_OF_MEMORY; |
| 461 |
memcpy(result, buf, n); |
| 462 |
result[n] = '\0'; |
| 463 |
return(result); |
| 464 |
} |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
CORD CORD_substr(CORD x, size_t i, size_t n) |
| 469 |
{ |
| 470 |
size_t len = CORD_len(x); |
| 471 |
|
| 472 |
if (i >= len || n == 0) return(0); |
| 473 |
if (i + n > len) n = len - i; |
| 474 |
return(CORD_substr_checked(x, i, n)); |
| 475 |
} |
| 476 |
|
| 477 |
/* See cord.h for definition. We assume i is in range. */ |
| 478 |
int CORD_iter5(CORD x, size_t i, CORD_iter_fn f1, |
| 479 |
CORD_batched_iter_fn f2, void * client_data) |
| 480 |
{ |
| 481 |
if (x == 0) return(0); |
| 482 |
if (CORD_IS_STRING(x)) { |
| 483 |
const char *p = x+i; |
| 484 |
|
| 485 |
if (*p == '\0') ABORT("2nd arg to CORD_iter5 too big"); |
| 486 |
if (f2 != CORD_NO_FN) { |
| 487 |
return((*f2)(p, client_data)); |
| 488 |
} else { |
| 489 |
while (*p) { |
| 490 |
if ((*f1)(*p, client_data)) return(1); |
| 491 |
p++; |
| 492 |
} |
| 493 |
return(0); |
| 494 |
} |
| 495 |
} else if (IS_CONCATENATION(x)) { |
| 496 |
struct Concatenation * conc = &(((CordRep *)x) -> concatenation); |
| 497 |
|
| 498 |
if (i > 0) { |
| 499 |
size_t left_len = LEFT_LEN(conc); |
| 500 |
|
| 501 |
if (i >= left_len) { |
| 502 |
return(CORD_iter5(conc -> right, i - left_len, f1, f2, |
| 503 |
client_data)); |
| 504 |
} |
| 505 |
} |
| 506 |
if (CORD_iter5(conc -> left, i, f1, f2, client_data)) { |
| 507 |
return(1); |
| 508 |
} |
| 509 |
return(CORD_iter5(conc -> right, 0, f1, f2, client_data)); |
| 510 |
} else /* function */ { |
| 511 |
struct Function * f = &(((CordRep *)x) -> function); |
| 512 |
size_t j; |
| 513 |
size_t lim = f -> len; |
| 514 |
|
| 515 |
for (j = i; j < lim; j++) { |
| 516 |
if ((*f1)((*(f -> fn))(j, f -> client_data), client_data)) { |
| 517 |
return(1); |
| 518 |
} |
| 519 |
} |
| 520 |
return(0); |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
#undef CORD_iter |
| 525 |
int CORD_iter(CORD x, CORD_iter_fn f1, void * client_data) |
| 526 |
{ |
| 527 |
return(CORD_iter5(x, 0, f1, CORD_NO_FN, client_data)); |
| 528 |
} |
| 529 |
|
| 530 |
int CORD_riter4(CORD x, size_t i, CORD_iter_fn f1, void * client_data) |
| 531 |
{ |
| 532 |
if (x == 0) return(0); |
| 533 |
if (CORD_IS_STRING(x)) { |
| 534 |
const char *p = x + i; |
| 535 |
|
| 536 |
for(;;) { |
| 537 |
char c = *p; |
| 538 |
|
| 539 |
if (c == '\0') ABORT("2nd arg to CORD_riter4 too big"); |
| 540 |
if ((*f1)(c, client_data)) return(1); |
| 541 |
if (p == x) break; |
| 542 |
p--; |
| 543 |
} |
| 544 |
return(0); |
| 545 |
} else if (IS_CONCATENATION(x)) { |
| 546 |
struct Concatenation * conc = &(((CordRep *)x) -> concatenation); |
| 547 |
CORD left_part = conc -> left; |
| 548 |
size_t left_len = LEFT_LEN(conc); |
| 549 |
|
| 550 |
if (i >= left_len) { |
| 551 |
if (CORD_riter4(conc -> right, i - left_len, f1, client_data)) { |
| 552 |
return(1); |
| 553 |
} |
| 554 |
return(CORD_riter4(left_part, left_len - 1, f1, client_data)); |
| 555 |
} else { |
| 556 |
return(CORD_riter4(left_part, i, f1, client_data)); |
| 557 |
} |
| 558 |
} else /* function */ { |
| 559 |
struct Function * f = &(((CordRep *)x) -> function); |
| 560 |
size_t j; |
| 561 |
|
| 562 |
for (j = i; ; j--) { |
| 563 |
if ((*f1)((*(f -> fn))(j, f -> client_data), client_data)) { |
| 564 |
return(1); |
| 565 |
} |
| 566 |
if (j == 0) return(0); |
| 567 |
} |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
int CORD_riter(CORD x, CORD_iter_fn f1, void * client_data) |
| 572 |
{ |
| 573 |
size_t len = CORD_len(x); |
| 574 |
if (len == 0) return(0); |
| 575 |
return(CORD_riter4(x, len - 1, f1, client_data)); |
| 576 |
} |
| 577 |
|
| 578 |
/* |
| 579 |
* The following functions are concerned with balancing cords. |
| 580 |
* Strategy: |
| 581 |
* Scan the cord from left to right, keeping the cord scanned so far |
| 582 |
* as a forest of balanced trees of exponentially decreasing length. |
| 583 |
* When a new subtree needs to be added to the forest, we concatenate all |
| 584 |
* shorter ones to the new tree in the appropriate order, and then insert |
| 585 |
* the result into the forest. |
| 586 |
* Crucial invariants: |
| 587 |
* 1. The concatenation of the forest (in decreasing order) with the |
| 588 |
* unscanned part of the rope is equal to the rope being balanced. |
| 589 |
* 2. All trees in the forest are balanced. |
| 590 |
* 3. forest[i] has depth at most i. |
| 591 |
*/ |
| 592 |
|
| 593 |
typedef struct { |
| 594 |
CORD c; |
| 595 |
size_t len; /* Actual length of c */ |
| 596 |
} ForestElement; |
| 597 |
|
| 598 |
static size_t min_len [ MAX_DEPTH ]; |
| 599 |
|
| 600 |
static int min_len_init = 0; |
| 601 |
|
| 602 |
int CORD_max_len; |
| 603 |
|
| 604 |
typedef ForestElement Forest [ MAX_DEPTH ]; |
| 605 |
/* forest[i].len >= fib(i+1) */ |
| 606 |
/* The string is the concatenation */ |
| 607 |
/* of the forest in order of DECREASING */ |
| 608 |
/* indices. */ |
| 609 |
|
| 610 |
void CORD_init_min_len(void) |
| 611 |
{ |
| 612 |
int i; |
| 613 |
size_t last, previous; |
| 614 |
|
| 615 |
min_len[0] = previous = 1; |
| 616 |
min_len[1] = last = 2; |
| 617 |
for (i = 2; i < MAX_DEPTH; i++) { |
| 618 |
size_t current = last + previous; |
| 619 |
|
| 620 |
if (current < last) /* overflow */ current = last; |
| 621 |
min_len[i] = current; |
| 622 |
previous = last; |
| 623 |
last = current; |
| 624 |
} |
| 625 |
CORD_max_len = (int)last - 1; |
| 626 |
min_len_init = 1; |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
void CORD_init_forest(ForestElement * forest, size_t max_len) |
| 631 |
{ |
| 632 |
int i; |
| 633 |
|
| 634 |
for (i = 0; i < MAX_DEPTH; i++) { |
| 635 |
forest[i].c = 0; |
| 636 |
if (min_len[i] > max_len) return; |
| 637 |
} |
| 638 |
ABORT("Cord too long"); |
| 639 |
} |
| 640 |
|
| 641 |
/* Add a leaf to the appropriate level in the forest, cleaning */ |
| 642 |
/* out lower levels as necessary. */ |
| 643 |
/* Also works if x is a balanced tree of concatenations; however */ |
| 644 |
/* in this case an extra concatenation node may be inserted above x; */ |
| 645 |
/* This node should not be counted in the statement of the invariants. */ |
| 646 |
void CORD_add_forest(ForestElement * forest, CORD x, size_t len) |
| 647 |
{ |
| 648 |
int i = 0; |
| 649 |
CORD sum = CORD_EMPTY; |
| 650 |
size_t sum_len = 0; |
| 651 |
|
| 652 |
while (len > min_len[i + 1]) { |
| 653 |
if (forest[i].c != 0) { |
| 654 |
sum = CORD_cat(forest[i].c, sum); |
| 655 |
sum_len += forest[i].len; |
| 656 |
forest[i].c = 0; |
| 657 |
} |
| 658 |
i++; |
| 659 |
} |
| 660 |
/* Sum has depth at most 1 greter than what would be required */ |
| 661 |
/* for balance. */ |
| 662 |
sum = CORD_cat(sum, x); |
| 663 |
sum_len += len; |
| 664 |
/* If x was a leaf, then sum is now balanced. To see this */ |
| 665 |
/* consider the two cases in which forest[i-1] either is or is */ |
| 666 |
/* not empty. */ |
| 667 |
while (sum_len >= min_len[i]) { |
| 668 |
if (forest[i].c != 0) { |
| 669 |
sum = CORD_cat(forest[i].c, sum); |
| 670 |
sum_len += forest[i].len; |
| 671 |
/* This is again balanced, since sum was balanced, and has */ |
| 672 |
/* allowable depth that differs from i by at most 1. */ |
| 673 |
forest[i].c = 0; |
| 674 |
} |
| 675 |
i++; |
| 676 |
} |
| 677 |
i--; |
| 678 |
forest[i].c = sum; |
| 679 |
forest[i].len = sum_len; |
| 680 |
} |
| 681 |
|
| 682 |
CORD CORD_concat_forest(ForestElement * forest, size_t expected_len) |
| 683 |
{ |
| 684 |
int i = 0; |
| 685 |
CORD sum = 0; |
| 686 |
size_t sum_len = 0; |
| 687 |
|
| 688 |
while (sum_len != expected_len) { |
| 689 |
if (forest[i].c != 0) { |
| 690 |
sum = CORD_cat(forest[i].c, sum); |
| 691 |
sum_len += forest[i].len; |
| 692 |
} |
| 693 |
i++; |
| 694 |
} |
| 695 |
return(sum); |
| 696 |
} |
| 697 |
|
| 698 |
/* Insert the frontier of x into forest. Balanced subtrees are */ |
| 699 |
/* treated as leaves. This potentially adds one to the depth */ |
| 700 |
/* of the final tree. */ |
| 701 |
void CORD_balance_insert(CORD x, size_t len, ForestElement * forest) |
| 702 |
{ |
| 703 |
int depth; |
| 704 |
|
| 705 |
if (CORD_IS_STRING(x)) { |
| 706 |
CORD_add_forest(forest, x, len); |
| 707 |
} else if (IS_CONCATENATION(x) |
| 708 |
&& ((depth = DEPTH(x)) >= MAX_DEPTH |
| 709 |
|| len < min_len[depth])) { |
| 710 |
struct Concatenation * conc = &(((CordRep *)x) -> concatenation); |
| 711 |
size_t left_len = LEFT_LEN(conc); |
| 712 |
|
| 713 |
CORD_balance_insert(conc -> left, left_len, forest); |
| 714 |
CORD_balance_insert(conc -> right, len - left_len, forest); |
| 715 |
} else /* function or balanced */ { |
| 716 |
CORD_add_forest(forest, x, len); |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
CORD CORD_balance(CORD x) |
| 722 |
{ |
| 723 |
Forest forest; |
| 724 |
size_t len; |
| 725 |
|
| 726 |
if (x == 0) return(0); |
| 727 |
if (CORD_IS_STRING(x)) return(x); |
| 728 |
if (!min_len_init) CORD_init_min_len(); |
| 729 |
len = LEN(x); |
| 730 |
CORD_init_forest(forest, len); |
| 731 |
CORD_balance_insert(x, len, forest); |
| 732 |
return(CORD_concat_forest(forest, len)); |
| 733 |
} |
| 734 |
|
| 735 |
|
| 736 |
/* Position primitives */ |
| 737 |
|
| 738 |
/* Private routines to deal with the hard cases only: */ |
| 739 |
|
| 740 |
/* P contains a prefix of the path to cur_pos. Extend it to a full */ |
| 741 |
/* path and set up leaf info. */ |
| 742 |
/* Return 0 if past the end of cord, 1 o.w. */ |
| 743 |
void CORD__extend_path(CORD_pos p) |
| 744 |
{ |
| 745 |
struct CORD_pe * current_pe = &(p[0].path[p[0].path_len]); |
| 746 |
CORD top = current_pe -> pe_cord; |
| 747 |
size_t pos = p[0].cur_pos; |
| 748 |
size_t top_pos = current_pe -> pe_start_pos; |
| 749 |
size_t top_len = GEN_LEN(top); |
| 750 |
|
| 751 |
/* Fill in the rest of the path. */ |
| 752 |
while(!CORD_IS_STRING(top) && IS_CONCATENATION(top)) { |
| 753 |
struct Concatenation * conc = &(((CordRep *)top) -> concatenation); |
| 754 |
size_t left_len; |
| 755 |
|
| 756 |
left_len = LEFT_LEN(conc); |
| 757 |
current_pe++; |
| 758 |
if (pos >= top_pos + left_len) { |
| 759 |
current_pe -> pe_cord = top = conc -> right; |
| 760 |
current_pe -> pe_start_pos = top_pos = top_pos + left_len; |
| 761 |
top_len -= left_len; |
| 762 |
} else { |
| 763 |
current_pe -> pe_cord = top = conc -> left; |
| 764 |
current_pe -> pe_start_pos = top_pos; |
| 765 |
top_len = left_len; |
| 766 |
} |
| 767 |
p[0].path_len++; |
| 768 |
} |
| 769 |
/* Fill in leaf description for fast access. */ |
| 770 |
if (CORD_IS_STRING(top)) { |
| 771 |
p[0].cur_leaf = top; |
| 772 |
p[0].cur_start = top_pos; |
| 773 |
p[0].cur_end = top_pos + top_len; |
| 774 |
} else { |
| 775 |
p[0].cur_end = 0; |
| 776 |
} |
| 777 |
if (pos >= top_pos + top_len) p[0].path_len = CORD_POS_INVALID; |
| 778 |
} |
| 779 |
|
| 780 |
char CORD__pos_fetch(CORD_pos p) |
| 781 |
{ |
| 782 |
/* Leaf is a function node */ |
| 783 |
struct CORD_pe * pe; |
| 784 |
CORD leaf; |
| 785 |
struct Function * f; |
| 786 |
|
| 787 |
if (!CORD_pos_valid(p)) |
| 788 |
ABORT("CORD_pos_fetch: invalid argument"); |
| 789 |
pe = &p[0].path[p[0].path_len]; |
| 790 |
leaf = pe -> pe_cord; |
| 791 |
if (!IS_FUNCTION(leaf)) |
| 792 |
ABORT("CORD_pos_fetch: bad leaf"); |
| 793 |
f = &((CordRep *)leaf)->function; |
| 794 |
return ((*(f -> fn))(p[0].cur_pos - pe -> pe_start_pos, f -> client_data)); |
| 795 |
} |
| 796 |
|
| 797 |
void CORD__next(CORD_pos p) |
| 798 |
{ |
| 799 |
size_t cur_pos = p[0].cur_pos + 1; |
| 800 |
struct CORD_pe * current_pe; |
| 801 |
CORD leaf; |
| 802 |
|
| 803 |
if (!CORD_pos_valid(p)) |
| 804 |
ABORT("CORD_next: invalid argument"); |
| 805 |
current_pe = &p[0].path[p[0].path_len]; |
| 806 |
leaf = current_pe -> pe_cord; |
| 807 |
|
| 808 |
/* Leaf is not a string or we're at end of leaf */ |
| 809 |
p[0].cur_pos = cur_pos; |
| 810 |
if (!CORD_IS_STRING(leaf)) { |
| 811 |
/* Function leaf */ |
| 812 |
struct Function * f = &(((CordRep *)leaf) -> function); |
| 813 |
size_t start_pos = current_pe -> pe_start_pos; |
| 814 |
size_t end_pos = start_pos + f -> len; |
| 815 |
|
| 816 |
if (cur_pos < end_pos) { |
| 817 |
/* Fill cache and return. */ |
| 818 |
size_t i; |
| 819 |
size_t limit = FUNCTION_BUF_SZ; |
| 820 |
CORD_fn fn = f -> fn; |
| 821 |
void * client_data = f -> client_data; |
| 822 |
|
| 823 |
if (end_pos - cur_pos < FUNCTION_BUF_SZ) { |
| 824 |
limit = end_pos - cur_pos; |
| 825 |
} |
| 826 |
for (i = 0; i < limit; i++) { |
| 827 |
p[0].function_buf[i] = (*fn)(i + cur_pos - start_pos, |
| 828 |
client_data); |
| 829 |
} |
| 830 |
p[0].cur_start = cur_pos; |
| 831 |
p[0].cur_leaf = p[0].function_buf; |
| 832 |
p[0].cur_end = cur_pos + limit; |
| 833 |
return; |
| 834 |
} |
| 835 |
} |
| 836 |
/* End of leaf */ |
| 837 |
/* Pop the stack until we find two concatenation nodes with the */ |
| 838 |
/* same start position: this implies we were in left part. */ |
| 839 |
{ |
| 840 |
while (p[0].path_len > 0 |
| 841 |
&& current_pe[0].pe_start_pos != current_pe[-1].pe_start_pos) { |
| 842 |
p[0].path_len--; |
| 843 |
current_pe--; |
| 844 |
} |
| 845 |
if (p[0].path_len == 0) { |
| 846 |
p[0].path_len = CORD_POS_INVALID; |
| 847 |
return; |
| 848 |
} |
| 849 |
} |
| 850 |
p[0].path_len--; |
| 851 |
CORD__extend_path(p); |
| 852 |
} |
| 853 |
|
| 854 |
void CORD__prev(CORD_pos p) |
| 855 |
{ |
| 856 |
struct CORD_pe * pe = &(p[0].path[p[0].path_len]); |
| 857 |
|
| 858 |
if (p[0].cur_pos == 0) { |
| 859 |
p[0].path_len = CORD_POS_INVALID; |
| 860 |
return; |
| 861 |
} |
| 862 |
p[0].cur_pos--; |
| 863 |
if (p[0].cur_pos >= pe -> pe_start_pos) return; |
| 864 |
|
| 865 |
/* Beginning of leaf */ |
| 866 |
|
| 867 |
/* Pop the stack until we find two concatenation nodes with the */ |
| 868 |
/* different start position: this implies we were in right part. */ |
| 869 |
{ |
| 870 |
struct CORD_pe * current_pe = &((p)[0].path[(p)[0].path_len]); |
| 871 |
|
| 872 |
while (p[0].path_len > 0 |
| 873 |
&& current_pe[0].pe_start_pos == current_pe[-1].pe_start_pos) { |
| 874 |
p[0].path_len--; |
| 875 |
current_pe--; |
| 876 |
} |
| 877 |
} |
| 878 |
p[0].path_len--; |
| 879 |
CORD__extend_path(p); |
| 880 |
} |
| 881 |
|
| 882 |
#undef CORD_pos_fetch |
| 883 |
#undef CORD_next |
| 884 |
#undef CORD_prev |
| 885 |
#undef CORD_pos_to_index |
| 886 |
#undef CORD_pos_to_cord |
| 887 |
#undef CORD_pos_valid |
| 888 |
|
| 889 |
char CORD_pos_fetch(CORD_pos p) |
| 890 |
{ |
| 891 |
if (p[0].cur_end != 0) { |
| 892 |
return(p[0].cur_leaf[p[0].cur_pos - p[0].cur_start]); |
| 893 |
} else { |
| 894 |
return(CORD__pos_fetch(p)); |
| 895 |
} |
| 896 |
} |
| 897 |
|
| 898 |
void CORD_next(CORD_pos p) |
| 899 |
{ |
| 900 |
if (p[0].cur_pos + 1 < p[0].cur_end) { |
| 901 |
p[0].cur_pos++; |
| 902 |
} else { |
| 903 |
CORD__next(p); |
| 904 |
} |
| 905 |
} |
| 906 |
|
| 907 |
void CORD_prev(CORD_pos p) |
| 908 |
{ |
| 909 |
if (p[0].cur_end != 0 && p[0].cur_pos > p[0].cur_start) { |
| 910 |
p[0].cur_pos--; |
| 911 |
} else { |
| 912 |
CORD__prev(p); |
| 913 |
} |
| 914 |
} |
| 915 |
|
| 916 |
size_t CORD_pos_to_index(CORD_pos p) |
| 917 |
{ |
| 918 |
return(p[0].cur_pos); |
| 919 |
} |
| 920 |
|
| 921 |
CORD CORD_pos_to_cord(CORD_pos p) |
| 922 |
{ |
| 923 |
return(p[0].path[0].pe_cord); |
| 924 |
} |
| 925 |
|
| 926 |
int CORD_pos_valid(CORD_pos p) |
| 927 |
{ |
| 928 |
return(p[0].path_len != CORD_POS_INVALID); |
| 929 |
} |
| 930 |
|
| 931 |
void CORD_set_pos(CORD_pos p, CORD x, size_t i) |
| 932 |
{ |
| 933 |
if (x == CORD_EMPTY) { |
| 934 |
p[0].path_len = CORD_POS_INVALID; |
| 935 |
return; |
| 936 |
} |
| 937 |
p[0].path[0].pe_cord = x; |
| 938 |
p[0].path[0].pe_start_pos = 0; |
| 939 |
p[0].path_len = 0; |
| 940 |
p[0].cur_pos = i; |
| 941 |
CORD__extend_path(p); |
| 942 |
} |