* Return a printable representation of the provided Dstr, limited to a length * of roughly maxlen. * * This is NOT threadsafe. */
| 511 | * This is NOT threadsafe. |
| 512 | */ |
| 513 | const char *dStr_printable(Dstr *in, int maxlen) |
| 514 | { |
| 515 | int i; |
| 516 | static const char *const HEX = "0123456789ABCDEF"; |
| 517 | static Dstr *out = NULL; |
| 518 | |
| 519 | if (in == NULL) |
| 520 | return NULL; |
| 521 | |
| 522 | if (out) |
| 523 | dStr_truncate(out, 0); |
| 524 | else |
| 525 | out = dStr_sized_new(in->len); |
| 526 | |
| 527 | for (i = 0; (i < in->len) && (out->len < maxlen); ++i) { |
| 528 | if (isprint(in->str[i]) || (in->str[i] == '\n')) { |
| 529 | dStr_append_c(out, in->str[i]); |
| 530 | } else { |
| 531 | dStr_append_l(out, "\\x", 2); |
| 532 | dStr_append_c(out, HEX[(in->str[i] >> 4) & 15]); |
| 533 | dStr_append_c(out, HEX[in->str[i] & 15]); |
| 534 | } |
| 535 | } |
| 536 | if (out->len >= maxlen) |
| 537 | dStr_append(out, "..."); |
| 538 | return out->str; |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | *- dList --------------------------------------------------------------------- |
no test coverage detected