replace name by replacement at the beginning of buf of bufsize. returns an error message or NULL. C is not really a nice language for processing strings. */
| 329 | C is not really a nice language for processing strings. |
| 330 | */ |
| 331 | static char *substitute(char *buf, |
| 332 | const int bufsize, |
| 333 | const char *name, |
| 334 | const char *replacement, const int do_esc) |
| 335 | { |
| 336 | int lbuf = strlen(buf), |
| 337 | lname = strlen(name), |
| 338 | lrepl = strlen(replacement), |
| 339 | lsubs = lrepl + |
| 340 | (do_esc ? (2 + number_of_escapes(DELIM, replacement)) : 0), |
| 341 | shift = lsubs - lname, size = lbuf + shift, i, j; |
| 342 | |
| 343 | /* buf must starts with name */ |
| 344 | ap_assert(!strncmp(buf, name, lname)); |
| 345 | |
| 346 | /* hmmm??? */ |
| 347 | if (!strcmp(name, replacement)) |
| 348 | return NULL; |
| 349 | |
| 350 | debug(fprintf(stderr, |
| 351 | "substitute(%s,%s,%s,%d,sh=%d,lbuf=%d,lrepl=%d,lsubs=%d)\n", |
| 352 | buf, name, replacement, do_esc, shift, lbuf, lrepl, lsubs)); |
| 353 | |
| 354 | if (size >= bufsize) { |
| 355 | /* could/should I reallocate? */ |
| 356 | return "cannot substitute, buffer size too small"; |
| 357 | } |
| 358 | |
| 359 | /* cannot use strcpy as strings may overlap */ |
| 360 | if (shift != 0) { |
| 361 | memmove(buf + lname + shift, buf + lname, lbuf - lname + 1); |
| 362 | } |
| 363 | |
| 364 | /* insert the replacement with escapes */ |
| 365 | j = 0; |
| 366 | if (do_esc) |
| 367 | buf[j++] = DELIM; |
| 368 | for (i = 0; i < lrepl; i++, j++) { |
| 369 | if (do_esc && (replacement[i] == DELIM || replacement[i] == ESCAPE)) |
| 370 | buf[j++] = ESCAPE; |
| 371 | buf[j] = replacement[i]; |
| 372 | } |
| 373 | if (do_esc) |
| 374 | buf[j++] = DELIM; |
| 375 | |
| 376 | return NULL; |
| 377 | } |
| 378 | |
| 379 | /* |
| 380 | find first occurrence of args in buf. |
no test coverage detected