diff --git a/src/cs_gen.cc b/src/cs_gen.cc index 265644f..6f549ed 100644 --- a/src/cs_gen.cc +++ b/src/cs_gen.cc @@ -198,7 +198,7 @@ static inline void compileunescapestr(cs_gen_state &gs) { memset(&buf[len], 0, sizeof(uint32_t) - len % sizeof(uint32_t)); gs.code.back() |= len << 8; uint32_t *ubuf = reinterpret_cast(buf); - gs.code.insert(gs.code.end(), ubuf, ubuf + (len / sizeof(uint32_t) + 1)); + gs.code.append(ubuf, ubuf + (len / sizeof(uint32_t) + 1)); delete[] buf; } @@ -496,7 +496,7 @@ static bool compileblockstr(cs_gen_state &gs, ostd::string_range str) { done: memset(&buf[len], '\0', sizeof(uint32_t) - len % sizeof(uint32_t)); uint32_t *ubuf = reinterpret_cast(buf); - gs.code.insert(gs.code.end(), ubuf, ubuf + (len / sizeof(uint32_t) + 1)); + gs.code.append(ubuf, ubuf + (len / sizeof(uint32_t) + 1)); gs.code[startc] |= len << 8; delete[] buf; return true; diff --git a/src/cs_util.hh b/src/cs_util.hh index 36a19cb..256951c 100644 --- a/src/cs_util.hh +++ b/src/cs_util.hh @@ -212,15 +212,31 @@ struct cs_valbuf { using const_reference = T const &; void reserve(std::size_t s) { buf.reserve(s); } + void resize(std::size_t s) { buf.resize(s); } + + void append(T const *beg, T const *end) { + buf.insert(buf.end(), beg, end); + } void push_back(T const &v) { buf.push_back(v); } + void pop_back() { buf.pop_back(); } - size_t size() const { return buf.size(); } + T &back() { return buf.back(); } + T const &back() const { return buf.back(); } + + std::size_t size() const { return buf.size(); } + std::size_t capacity() const { return buf.capacity(); } bool empty() const { return buf.empty(); } void clear() { buf.clear(); } + T &operator[](std::size_t i) { return buf[i]; } + T const &operator[](std::size_t i) const { return buf[i]; } + + T *data() { return &buf[0]; } + T const *data() const { return &buf[0]; } + std::vector> buf; }; @@ -229,7 +245,7 @@ struct cs_charbuf: cs_valbuf { cs_charbuf(cs_state &cs): cs_valbuf(cs) {} void append(char const *beg, char const *end) { - buf.insert(buf.end(), beg, end); + cs_valbuf::append(beg, end); } void append(ostd::string_range v) { diff --git a/src/cs_vm.hh b/src/cs_vm.hh index f518363..1722b49 100644 --- a/src/cs_vm.hh +++ b/src/cs_vm.hh @@ -128,14 +128,14 @@ struct cs_gen_state { cs_state &cs; cs_gen_state *prevps; bool parsing = true; - cs_vector code; + cs_valbuf code; ostd::string_range source; size_t current_line; ostd::string_range src_name; cs_gen_state() = delete; cs_gen_state(cs_state &csr): - cs(csr), prevps(csr.p_pstate), code(), + cs(csr), prevps(csr.p_pstate), code{cs}, source(nullptr), current_line(1), src_name() { csr.p_pstate = this; @@ -171,9 +171,7 @@ struct cs_gen_state { } code.push_back(CS_CODE_VAL | CS_RET_STRING | (word.size() << 8)); auto it = reinterpret_cast(word.data()); - code.insert( - code.end(), it, it + (word.size() / sizeof(uint32_t)) - ); + code.append(it, it + (word.size() / sizeof(uint32_t))); size_t esz = word.size() % sizeof(uint32_t); union { char c[sizeof(uint32_t)]; @@ -202,7 +200,7 @@ struct cs_gen_state { } c; c.i = i; code.push_back(CS_CODE_VAL | CS_RET_INT); - code.insert(code.end(), c.u, c.u + CsTypeStorageSize); + code.append(c.u, c.u + CsTypeStorageSize); } } @@ -218,7 +216,7 @@ struct cs_gen_state { } c; c.f = f; code.push_back(CS_CODE_VAL | CS_RET_FLOAT); - code.insert(code.end(), c.u, c.u + CsTypeStorageSize); + code.append(c.u, c.u + CsTypeStorageSize); } } diff --git a/src/lib_list.cc b/src/lib_list.cc index 4622869..7269531 100644 --- a/src/lib_list.cc +++ b/src/lib_list.cc @@ -543,7 +543,7 @@ static void cs_list_sort( cs_alias *xa = static_cast(x), *ya = static_cast(y); - cs_vector items; + cs_valbuf items{cs}; size_t total = 0; for (cs_list_parse_state p{list}; list_parse(p, cs);) { @@ -567,7 +567,7 @@ static void cs_list_sort( size_t nuniq = items.size(); if (body) { ListSortFun f = { cs, xval, yval, body }; - ostd::sort_cmp(ostd::iter(items), f); + ostd::sort_cmp(ostd::iter(items.buf), f); if (!cs_code_is_empty(unique)) { f.body = unique; totaluniq = items[0].quote.size();