void wstring::alloc(size_t size, bool save) { if (! check_realloc(size)) return; Rep *p = Rep::create(size); if (save) { p->copy(0, data(), length()); p->len = length(); } else p->len = 0; repup(p); }
wstring& wstring::replace(size_t pos, size_t n1, const wchar_t* s, size_t n2) { const size_t len = length(); if (pos > len) throw out_of_range("pos > len"); if (n1 > len - pos) n1 = len - pos; if (len - n1 > max_size() - n2) throw length_error("len - n1 > max_size() - n2"); size_t newlen = len - n1 + n2; if (check_realloc(newlen)) { Rep *p = Rep::create(newlen); p->copy(0, data(), pos); p->copy(pos + n2, data() + pos + n1, len -(pos + n1)); p->copy(pos, s, n2); repup(p); } else { rep()->move(pos + n2, data() + pos + n1, len -(pos + n1)); rep()->copy(pos, s, n2); } rep()->len = newlen; return *this; }
wchar_t* wstring::Rep::clone() { Rep* p = Rep::create(len); p->copy(0, data(), len); p->len = len; return p->data(); }