get rid of cs_vector in most places

master
Daniel Kolesa 2021-03-19 22:49:29 +01:00
parent 0ca8561d5d
commit b81e419db6
4 changed files with 27 additions and 13 deletions

View File

@ -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<uint32_t *>(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<uint32_t *>(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;

View File

@ -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<T, cs_shared_state::allocator<T>> buf;
};
@ -229,7 +245,7 @@ struct cs_charbuf: cs_valbuf<char> {
cs_charbuf(cs_state &cs): cs_valbuf<char>(cs) {}
void append(char const *beg, char const *end) {
buf.insert(buf.end(), beg, end);
cs_valbuf<char>::append(beg, end);
}
void append(ostd::string_range v) {

View File

@ -128,14 +128,14 @@ struct cs_gen_state {
cs_state &cs;
cs_gen_state *prevps;
bool parsing = true;
cs_vector<uint32_t> code;
cs_valbuf<uint32_t> 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<uint32_t const *>(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<cs_int>);
code.append(c.u, c.u + CsTypeStorageSize<cs_int>);
}
}
@ -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<cs_float>);
code.append(c.u, c.u + CsTypeStorageSize<cs_float>);
}
}

View File

@ -543,7 +543,7 @@ static void cs_list_sort(
cs_alias *xa = static_cast<cs_alias *>(x), *ya = static_cast<cs_alias *>(y);
cs_vector<ListSortItem> items;
cs_valbuf<ListSortItem> 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();