diff --git a/src/cs_util.cc b/src/cs_util.cc index 3d3f108..138b680 100644 --- a/src/cs_util.cc +++ b/src/cs_util.cc @@ -519,9 +519,9 @@ OSTD_EXPORT std::size_t list_count(cs_list_parse_state &ps, cs_state &cs) { OSTD_EXPORT cs_strref list_get_item(cs_list_parse_state &ps, cs_state &cs) { if (!ps.quoted_item.empty() && (*ps.quoted_item == '"')) { - auto app = ostd::appender(); + auto app = ostd::appender(cs); util::unescape_string(app, ps.item); - return cs_strref{cs, app.get()}; + return cs_strref{cs, app.get().str()}; } return cs_strref{cs, ps.item}; } diff --git a/src/cs_util.hh b/src/cs_util.hh index affc376..f36c17e 100644 --- a/src/cs_util.hh +++ b/src/cs_util.hh @@ -82,6 +82,21 @@ struct cs_shared_state { v->~T(); alloc(v, len * sizeof(T), 0); } + + template + struct allocator { + using value_type = T; + + T *allocate(std::size_t n) { + return static_cast(state->alloc(nullptr, 0, n * sizeof(T))); + } + + void deallocate(T *p, std::size_t n) { + state->alloc(p, n, 0); + } + + cs_shared_state *state; + }; }; inline cs_shared_state *cs_get_sstate(cs_state &cs) { @@ -164,6 +179,31 @@ struct cs_strman { std::unordered_map counts{}; }; +struct cs_charbuf { + cs_charbuf(cs_shared_state &cs): + buf{cs_shared_state::allocator{&cs}} + {} + + cs_charbuf(cs_state &cs): + buf{cs_shared_state::allocator{cs_get_sstate(cs)}} + {} + + using size_type = std::size_t; + using value_type = char; + using reference = char &; + using const_reference = char const &; + + void reserve(std::size_t s) { buf.reserve(s); } + + void push_back(char c) { buf.push_back(c); } + + ostd::string_range str() { + return ostd::string_range{buf.data(), buf.data() + buf.size()}; + } + + std::vector> buf; +}; + } /* namespace cscript */ #endif /* LIBCUBESCRIPT_CS_UTIL_HH */ diff --git a/src/lib_str.cc b/src/lib_str.cc index bf53750..38a162b 100644 --- a/src/lib_str.cc +++ b/src/lib_str.cc @@ -82,16 +82,16 @@ void cs_init_lib_string(cs_state &cs) { res.set_str(sr); }); - cs.new_command("escape", "s", [](auto &, auto args, auto &res) { - auto s = ostd::appender(); + cs.new_command("escape", "s", [](auto &ccs, auto args, auto &res) { + auto s = ostd::appender(ccs); util::escape_string(s, args[0].get_str()); - res.set_str(s.get()); + res.set_str(s.get().str()); }); - cs.new_command("unescape", "s", [](auto &, auto args, auto &res) { - auto s = ostd::appender(); + cs.new_command("unescape", "s", [](auto &ccs, auto args, auto &res) { + auto s = ostd::appender(ccs); util::unescape_string(s, args[0].get_str()); - res.set_str(s.get()); + res.set_str(s.get().str()); }); cs.new_command("concat", "V", [](auto &ccs, auto args, auto &res) {