#include "internal.h" // new returns a new dynamic string object, initialized from the given c string. // len defines the length of the c substring that we will copy into our buffer. // the backing buffer will have capacity cap. string string·makecap(byte *s, vlong len, vlong cap) { struct Hdr* h; h = malloc(sizeof(*h) + cap + 1); if(s == nil) mem·set(h, sizeof(*h), 0); if(h == nil) return nil; // Allocation failed. h->len = (s == nil) ? 0 : len; h->cap = cap; if(cap < h->len) goto cleanup; if(s != nil && cap > 0){ mem·copy(h->buf, h->len, s); mem·set(h->buf + h->len, h->cap - h->len + 1, '\0'); } return h->buf; cleanup: free(h); panicf("Attempted to create a string with less capacity than length"); return nil; } // new returns a new dynamic string object, initialized from the given c string. // the backing buffer capacity is equivalent to the string length. string string·makelen(byte *s, vlong len) { vlong sl = (!s) ? 0 : str·len(s); if(sl < len) panicf("attempted to take a bigger substring than string length"); vlong cap = (len == 0) ? 1 : len; return string·makecap(s, len, cap); } // new returns a new dynamic string object, initialized from the given c string. // the backing buffer capacity is equivalent to the string length. string string·make(byte *s) { vlong len = (!s) ? 0 : str·len(s); return string·makelen(s, len); }