From 4fb6b9a0bc4635bb1df5b056a2a3891283a6251a Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Sun, 16 May 2021 00:50:14 +0200 Subject: [PATCH] add ident getter by index, + ident count method on state --- include/cubescript/cubescript/state.hh | 26 ++++++++++++++++++++++++++ src/cs_state.cc | 23 +++++++++++++++++++++++ tools/edit_linenoise.hh | 7 ++++--- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/include/cubescript/cubescript/state.hh b/include/cubescript/cubescript/state.hh index 0045207..b9c3520 100644 --- a/include/cubescript/cubescript/state.hh +++ b/include/cubescript/cubescript/state.hh @@ -215,11 +215,37 @@ struct LIBCUBESCRIPT_EXPORT state { */ ident &new_ident(std::string_view n); + /** @brief Get the number of idents in the state + * + * This returns the number of idents that the main state has stored. It + * does not matter which thread you call this on. + */ + std::size_t ident_count() const; + /** @brief Get a specific cubescript::ident */ std::optional> get_ident( std::string_view name ); + /** @brief Get a specific cubescript::ident */ + std::optional> get_ident( + std::string_view name + ) const; + + /** @brief Get a specific cubescript::ident by index + * + * Keep in mind that no bounds checking is performed, so the index must + * be within range. + */ + ident &get_ident(std::size_t index); + + /** @brief Get a specific cubescript::ident by index + * + * Keep in mind that no bounds checking is performed, so the index must + * be within range. + */ + ident const &get_ident(std::size_t index) const; + /** @brief Assign a value to a name * * This will set something of the given name to the given value. The diff --git a/src/cs_state.cc b/src/cs_state.cc index 63a61aa..b70f14b 100644 --- a/src/cs_state.cc +++ b/src/cs_state.cc @@ -314,6 +314,10 @@ LIBCUBESCRIPT_EXPORT void *state::alloc(void *ptr, size_t os, size_t ns) { return p_tstate->istate->alloc(ptr, os, ns); } +LIBCUBESCRIPT_EXPORT std::size_t state::ident_count() const { + return p_tstate->istate->identmap.size(); +} + LIBCUBESCRIPT_EXPORT std::optional< std::reference_wrapper > state::get_ident(std::string_view name) { @@ -324,6 +328,25 @@ LIBCUBESCRIPT_EXPORT std::optional< return *id; } +LIBCUBESCRIPT_EXPORT std::optional< + std::reference_wrapper +> state::get_ident(std::string_view name) const { + auto *id = p_tstate->istate->get_ident(name); + if (!id) { + return std::nullopt; + } + return *id; +} + +LIBCUBESCRIPT_EXPORT ident &state::get_ident(std::size_t index) { + return *p_tstate->istate->identmap[index]; +} + +LIBCUBESCRIPT_EXPORT ident const &state::get_ident(std::size_t index) const { + return *p_tstate->istate->identmap[index]; +} + + LIBCUBESCRIPT_EXPORT span_type state::get_idents() { return span_type{ p_tstate->istate->identmap.data(), diff --git a/tools/edit_linenoise.hh b/tools/edit_linenoise.hh index 0dd6c5e..da69853 100644 --- a/tools/edit_linenoise.hh +++ b/tools/edit_linenoise.hh @@ -15,11 +15,12 @@ static cs::state *ln_cs = nullptr; inline void ln_complete(char const *buf, std::vector &lc) { std::string_view cmd = get_complete_cmd(buf); - for (auto id: ln_cs->get_idents()) { - if (id->type() != cs::ident_type::COMMAND) { + for (std::size_t i = 0; i < ln_cs->ident_count(); ++i) { + auto &id = ln_cs->get_ident(i); + if (id.type() != cs::ident_type::COMMAND) { continue; } - std::string_view idname = id->name(); + std::string_view idname = id.name(); if (idname.size() <= cmd.size()) { continue; }