| 1000 | } |
| 1001 | |
| 1002 | void |
| 1003 | strappend (StringBuilder *dest, const char *src) |
| 1004 | { |
| 1005 | size_t len = strlen (src); |
| 1006 | size_t new_offset = xadd (dest->offset, len); |
| 1007 | |
| 1008 | if (new_offset >= dest->size) |
| 1009 | { |
| 1010 | dest->size = xmul (xadd (new_offset, 1), 2); |
| 1011 | dest->str = xrealloc (dest->str, dest->size); |
| 1012 | } |
| 1013 | |
| 1014 | /* Preserves the invariant that dest->str is always null-terminated, even |
| 1015 | * though the offset is positioned at the null byte for the next write. |
| 1016 | */ |
| 1017 | strncpy (dest->str + dest->offset, src, len + 1); |
| 1018 | dest->offset = new_offset; |
| 1019 | } |
| 1020 | |
| 1021 | __attribute__((format (printf, 2, 3))) |
| 1022 | void |