From b5f54b5cca9686f4a9386c44f7742cbf130dbc6b Mon Sep 17 00:00:00 2001 From: q66 Date: Fri, 29 May 2015 01:02:19 +0100 Subject: [PATCH] a few more string ops --- octa/string.h | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/octa/string.h b/octa/string.h index e0fc9b5..da173c5 100644 --- a/octa/string.h +++ b/octa/string.h @@ -70,6 +70,12 @@ namespace octa { return *this; } + void resize(size_t n, T v = T()) { + p_buf.pop(); + p_buf.resize(n, v); + terminate(); + } + void reserve(size_t n) { p_buf.reserve(n + 1); } @@ -104,17 +110,48 @@ namespace octa { p_buf.push('\0'); } - StringBase &operator+=(const StringBase &s) { + StringBase &append(const StringBase &s) { p_buf.pop(); p_buf.insert_range(p_buf.size(), s.p_buf.each()); return *this; } - StringBase &operator+=(const T *s) { + + StringBase &append(const StringBase &s, size_t idx, size_t len) { + p_buf.pop(); + p_buf.insert_range(p_buf.size(), PointerRange(&s[idx], + (len == npos) ? (s.size() - idx) : len)); + terminate(); + return *this; + } + + StringBase &append(const T *s) { p_buf.pop(); p_buf.insert_range(p_buf.size(), PointerRange(s, strlen(s) + 1)); return *this; } + + StringBase &append(size_t n, T c) { + p_buf.pop(); + for (size_t i = 0; i < n; ++i) p_buf.push(c); + p_buf.push('\0'); + return *this; + } + + template + StringBase &append_range(R range) { + p_buf.pop(); + p_buf.insert_range(p_buf.size(), range); + terminate(); + return *this; + } + + StringBase &operator+=(const StringBase &s) { + return append(s); + } + StringBase &operator+=(const T *s) { + return append(s); + } StringBase &operator+=(T c) { p_buf.pop(); p_buf.push(c);