btc_bool cstr_append_buf(cstring* s, const void* buf, size_t sz) { if (!cstr_alloc_min_sz(s, s->len + sz)) return false; memcpy(s->str + s->len, buf, sz); s->len += sz; s->str[s->len] = 0; return true; }
int cstr_append_buf(cstring* s, const void* buf, size_t sz) { if (!cstr_alloc_min_sz(s, s->len + sz)) return 0; memcpy(s->str + s->len, buf, sz); s->len += sz; s->str[s->len] = 0; return 1; }
cstring* cstr_new_sz(size_t sz) { cstring* s = calloc(1, sizeof(cstring)); if (!s) return NULL; if (!cstr_alloc_min_sz(s, sz)) { free(s); return NULL; } return s; }
int cstr_alloc_minsize(cstring* s, size_t new_sz) { /* no change */ if (new_sz == s->len) return 1; /* truncate string */ if (new_sz <= s->len) { return 0; } /* increase string size */ if (!cstr_alloc_min_sz(s, new_sz)) return 0; /* contents of string tail undefined */ //s->len = new_sz; s->str[s->len] = 0; return 1; }
btc_bool cstr_resize(cstring* s, size_t new_sz) { // no change if (new_sz == s->len) return true; // truncate string if (new_sz <= s->len) { s->len = new_sz; s->str[s->len] = 0; return true; } // increase string size if (!cstr_alloc_min_sz(s, new_sz)) return false; // contents of string tail undefined s->len = new_sz; s->str[s->len] = 0; return true; }