Skip to content

Commit

Permalink
add ident getter by index, + ident count method on state
Browse files Browse the repository at this point in the history
  • Loading branch information
q66 committed May 15, 2021
1 parent 8086c23 commit 4fb6b9a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
26 changes: 26 additions & 0 deletions include/cubescript/cubescript/state.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::reference_wrapper<ident>> get_ident(
std::string_view name
);

/** @brief Get a specific cubescript::ident */
std::optional<std::reference_wrapper<ident const>> 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
Expand Down
23 changes: 23 additions & 0 deletions src/cs_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<ident>
> state::get_ident(std::string_view name) {
Expand All @@ -324,6 +328,25 @@ LIBCUBESCRIPT_EXPORT std::optional<
return *id;
}

LIBCUBESCRIPT_EXPORT std::optional<
std::reference_wrapper<ident const>
> 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<ident *> state::get_idents() {
return span_type<ident *>{
p_tstate->istate->identmap.data(),
Expand Down
7 changes: 4 additions & 3 deletions tools/edit_linenoise.hh
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ static cs::state *ln_cs = nullptr;

inline void ln_complete(char const *buf, std::vector<std::string> &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;
}
Expand Down

0 comments on commit 4fb6b9a

Please sign in to comment.