use c++20 std::span

master
Daniel Kolesa 2021-03-20 05:41:25 +01:00
parent 4068c96ec2
commit 320fdbaefd
8 changed files with 63 additions and 49 deletions

View File

@ -457,8 +457,8 @@ struct OSTD_EXPORT cs_state {
cs_alias *get_alias(std::string_view name); cs_alias *get_alias(std::string_view name);
bool have_ident(std::string_view name); bool have_ident(std::string_view name);
cs_ident_r get_idents(); std::span<cs_ident *> get_idents();
cs_const_ident_r get_idents() const; std::span<cs_ident const *> get_idents() const;
void reset_var(std::string_view name); void reset_var(std::string_view name);
void touch_var(std::string_view name); void touch_var(std::string_view name);
@ -466,12 +466,12 @@ struct OSTD_EXPORT cs_state {
void run(cs_bcode *code, cs_value &ret); void run(cs_bcode *code, cs_value &ret);
void run(std::string_view code, cs_value &ret); void run(std::string_view code, cs_value &ret);
void run(std::string_view code, cs_value &ret, std::string_view source); void run(std::string_view code, cs_value &ret, std::string_view source);
void run(cs_ident *id, cs_value_r args, cs_value &ret); void run(cs_ident *id, std::span<cs_value> args, cs_value &ret);
cs_value run(cs_bcode *code); cs_value run(cs_bcode *code);
cs_value run(std::string_view code); cs_value run(std::string_view code);
cs_value run(std::string_view code, std::string_view source); cs_value run(std::string_view code, std::string_view source);
cs_value run(cs_ident *id, cs_value_r args); cs_value run(cs_ident *id, std::span<cs_value> args);
cs_loop_state run_loop(cs_bcode *code, cs_value &ret); cs_loop_state run_loop(cs_bcode *code, cs_value &ret);
cs_loop_state run_loop(cs_bcode *code); cs_loop_state run_loop(cs_bcode *code);
@ -495,7 +495,7 @@ struct OSTD_EXPORT cs_state {
); );
void set_var_int_checked(cs_ivar *iv, cs_int v); void set_var_int_checked(cs_ivar *iv, cs_int v);
void set_var_int_checked(cs_ivar *iv, cs_value_r args); void set_var_int_checked(cs_ivar *iv, std::span<cs_value> args);
void set_var_float_checked(cs_fvar *fv, cs_float v); void set_var_float_checked(cs_fvar *fv, cs_float v);
void set_var_str_checked(cs_svar *fv, std::string_view v); void set_var_str_checked(cs_svar *fv, std::string_view v);
@ -658,7 +658,8 @@ OSTD_EXPORT cs_strref list_get_item(cs_list_parse_state &ps, cs_state &cs);
OSTD_EXPORT void list_find_item(cs_list_parse_state &ps); OSTD_EXPORT void list_find_item(cs_list_parse_state &ps);
OSTD_EXPORT cs_strref value_list_concat( OSTD_EXPORT cs_strref value_list_concat(
cs_state &cs, cs_value_r vals, std::string_view sep = std::string_view{} cs_state &cs, std::span<cs_value> vals,
std::string_view sep = std::string_view{}
); );
namespace util { namespace util {

View File

@ -3,6 +3,7 @@
#include <limits.h> #include <limits.h>
#include <functional> #include <functional>
#include <span>
#include <ostd/range.hh> #include <ostd/range.hh>
/* do not modify */ /* do not modify */
@ -11,10 +12,6 @@ namespace cscript {
struct cs_ident; struct cs_ident;
struct cs_value; struct cs_value;
struct cs_var; struct cs_var;
using cs_value_r = ostd::iterator_range<cs_value *>;
using cs_ident_r = ostd::iterator_range<cs_ident **>;
using cs_const_ident_r = ostd::iterator_range<cs_ident const **>;
} }
/* configurable section */ /* configurable section */
@ -33,7 +30,7 @@ namespace cscript {
*/ */
using cs_var_cb = std::function<void(cs_state &, cs_ident &)>; using cs_var_cb = std::function<void(cs_state &, cs_ident &)>;
using cs_vprint_cb = std::function<void(cs_state const &, cs_var const &)>; using cs_vprint_cb = std::function<void(cs_state const &, cs_var const &)>;
using cs_command_cb = std::function<void(cs_state &, cs_value_r, cs_value &)>; using cs_command_cb = std::function<void(cs_state &, std::span<cs_value>, cs_value &)>;
using cs_hook_cb = std::function<void(cs_state &)>; using cs_hook_cb = std::function<void(cs_state &)>;
using cs_alloc_cb = void *(*)(void *, void *, size_t, size_t); using cs_alloc_cb = void *(*)(void *, void *, size_t, size_t);

View File

@ -557,7 +557,7 @@ OSTD_EXPORT void list_find_item(cs_list_parse_state &ps) {
} }
OSTD_EXPORT cs_strref value_list_concat( OSTD_EXPORT cs_strref value_list_concat(
cs_state &cs, cs_value_r vals, std::string_view sep cs_state &cs, std::span<cs_value> vals, std::string_view sep
) { ) {
auto app = ostd::appender<cs_charbuf>(cs); auto app = ostd::appender<cs_charbuf>(cs);
for (std::size_t i = 0; i < vals.size(); ++i) { for (std::size_t i = 0; i < vals.size(); ++i) {

View File

@ -9,7 +9,7 @@ namespace cscript {
struct cs_cmd_internal { struct cs_cmd_internal {
static void call( static void call(
cs_state &cs, cs_command *c, cs_value_r args, cs_value &ret cs_state &cs, cs_command *c, std::span<cs_value> args, cs_value &ret
) { ) {
c->p_cb_cftv(cs, args, ret); c->p_cb_cftv(cs, args, ret);
} }
@ -424,13 +424,19 @@ static inline void callcommand(
case 'C': { case 'C': {
i = std::max(i + 1, numargs); i = std::max(i + 1, numargs);
cs_value tv{cs}; cs_value tv{cs};
tv.set_str(value_list_concat(cs, ostd::iter(args, args + i), " ")); tv.set_str(value_list_concat(
cs_cmd_internal::call(cs, id, cs_value_r(&tv, &tv + 1), res); cs, std::span{args, std::size_t(i)}, " "
));
cs_cmd_internal::call(
cs, id, std::span<cs_value>(&tv, &tv + 1), res
);
return; return;
} }
case 'V': case 'V':
i = std::max(i + 1, numargs); i = std::max(i + 1, numargs);
cs_cmd_internal::call(cs, id, ostd::iter(args, args + i), res); cs_cmd_internal::call(
cs, id, std::span{args, std::size_t(i)}, res
);
return; return;
case '1': case '1':
case '2': case '2':
@ -444,7 +450,9 @@ static inline void callcommand(
} }
} }
++i; ++i;
cs_cmd_internal::call(cs, id, cs_value_r(args, args + i), res); cs_cmd_internal::call(
cs, id, std::span<cs_value>{args, std::size_t(i)}, res
);
} }
static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result); static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result);
@ -1300,9 +1308,9 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) {
); );
int offset = numargs - id->get_num_args(); int offset = numargs - id->get_num_args();
result.force_none(); result.force_none();
cs_cmd_internal::call(cs, id, cs_value_r( cs_cmd_internal::call(cs, id, std::span<cs_value>{
&args[0] + offset, &args[0] + offset + id->get_num_args() &args[0] + offset, std::size_t(id->get_num_args())
), result); }, result);
force_arg(result, op & CS_CODE_RET_MASK); force_arg(result, op & CS_CODE_RET_MASK);
numargs = offset; numargs = offset;
continue; continue;
@ -1315,11 +1323,12 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) {
cs_command *id = static_cast<cs_command *>( cs_command *id = static_cast<cs_command *>(
cs.p_state->identmap[op >> 13] cs.p_state->identmap[op >> 13]
); );
int callargs = (op >> 8) & 0x1F, offset = numargs - callargs; std::size_t callargs = (op >> 8) & 0x1F,
offset = numargs - callargs;
result.force_none(); result.force_none();
cs_cmd_internal::call(cs, id, ostd::iter( cs_cmd_internal::call(
&args[offset], &args[offset + callargs] cs, id, std::span{&args[offset], callargs}, result
), result); );
force_arg(result, op & CS_CODE_RET_MASK); force_arg(result, op & CS_CODE_RET_MASK);
numargs = offset; numargs = offset;
continue; continue;
@ -1331,14 +1340,17 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) {
cs_command *id = static_cast<cs_command *>( cs_command *id = static_cast<cs_command *>(
cs.p_state->identmap[op >> 13] cs.p_state->identmap[op >> 13]
); );
int callargs = (op >> 8) & 0x1F, offset = numargs - callargs; std::size_t callargs = (op >> 8) & 0x1F,
offset = numargs - callargs;
result.force_none(); result.force_none();
{ {
cs_value tv{cs}; cs_value tv{cs};
tv.set_str(value_list_concat(cs, ostd::iter( tv.set_str(value_list_concat(cs, std::span{
&args[offset], &args[offset + callargs] &args[offset], callargs
), " ")); }, " "));
cs_cmd_internal::call(cs, id, cs_value_r(&tv, &tv + 1), result); cs_cmd_internal::call(
cs, id, std::span<cs_value>{&tv, 1}, result
);
} }
force_arg(result, op & CS_CODE_RET_MASK); force_arg(result, op & CS_CODE_RET_MASK);
numargs = offset; numargs = offset;
@ -1353,9 +1365,9 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) {
case CS_CODE_CONC_W | CS_RET_STRING: case CS_CODE_CONC_W | CS_RET_STRING:
case CS_CODE_CONC_W | CS_RET_FLOAT: case CS_CODE_CONC_W | CS_RET_FLOAT:
case CS_CODE_CONC_W | CS_RET_INT: { case CS_CODE_CONC_W | CS_RET_INT: {
int numconc = op >> 8; std::size_t numconc = op >> 8;
auto buf = value_list_concat( auto buf = value_list_concat(
cs, ostd::iter(&args[numargs - numconc], &args[numargs]), cs, std::span{&args[numargs - numconc], numconc},
((op & CS_CODE_OP_MASK) == CS_CODE_CONC) ? " " : "" ((op & CS_CODE_OP_MASK) == CS_CODE_CONC) ? " " : ""
); );
numargs = numargs - numconc; numargs = numargs - numconc;
@ -1369,9 +1381,9 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) {
case CS_CODE_CONC_M | CS_RET_STRING: case CS_CODE_CONC_M | CS_RET_STRING:
case CS_CODE_CONC_M | CS_RET_FLOAT: case CS_CODE_CONC_M | CS_RET_FLOAT:
case CS_CODE_CONC_M | CS_RET_INT: { case CS_CODE_CONC_M | CS_RET_INT: {
int numconc = op >> 8; std::size_t numconc = op >> 8;
auto buf = value_list_concat( auto buf = value_list_concat(
cs, ostd::iter(&args[numargs - numconc], &args[numargs]) cs, std::span{&args[numargs - numconc], numconc}
); );
numargs = numargs - numconc; numargs = numargs - numconc;
result.set_str(buf); result.set_str(buf);
@ -1501,7 +1513,7 @@ noid:
} else { } else {
cs.set_var_int_checked( cs.set_var_int_checked(
static_cast<cs_ivar *>(id), static_cast<cs_ivar *>(id),
ostd::iter(&args[offset], &args[offset + callargs]) std::span{&args[offset], std::size_t(callargs)}
); );
} }
numargs = offset - 1; numargs = offset - 1;
@ -1588,7 +1600,7 @@ void cs_state::run(
cs_run(*this, source, code, ret); cs_run(*this, source, code, ret);
} }
void cs_state::run(cs_ident *id, cs_value_r args, cs_value &ret) { void cs_state::run(cs_ident *id, std::span<cs_value> args, cs_value &ret) {
int nargs = int(args.size()); int nargs = int(args.size());
ret.set_none(); ret.set_none();
RunDepthRef level{*this}; /* incr and decr on scope exit */ RunDepthRef level{*this}; /* incr and decr on scope exit */
@ -1679,7 +1691,7 @@ cs_value cs_state::run(std::string_view code, std::string_view source) {
return ret; return ret;
} }
cs_value cs_state::run(cs_ident *id, cs_value_r args) { cs_value cs_state::run(cs_ident *id, std::span<cs_value> args) {
cs_value ret{*this}; cs_value ret{*this};
run(id, args, ret); run(id, args, ret);
return ret; return ret;

View File

@ -518,16 +518,16 @@ OSTD_EXPORT bool cs_state::have_ident(std::string_view name) {
return p_state->idents.find(name) != p_state->idents.end(); return p_state->idents.find(name) != p_state->idents.end();
} }
OSTD_EXPORT cs_ident_r cs_state::get_idents() { OSTD_EXPORT std::span<cs_ident *> cs_state::get_idents() {
return cs_ident_r( return std::span<cs_ident *>{
p_state->identmap.data(), p_state->identmap.data(),
p_state->identmap.data() + p_state->identmap.size() p_state->identmap.size()
); };
} }
OSTD_EXPORT cs_const_ident_r cs_state::get_idents() const { OSTD_EXPORT std::span<cs_ident const *> cs_state::get_idents() const {
auto ptr = const_cast<cs_ident const **>(p_state->identmap.data()); auto ptr = const_cast<cs_ident const **>(p_state->identmap.data());
return cs_const_ident_r(ptr, ptr + p_state->identmap.size()); return std::span<cs_ident const *>{ptr, p_state->identmap.size()};
} }
OSTD_EXPORT cs_ivar *cs_state::new_ivar( OSTD_EXPORT cs_ivar *cs_state::new_ivar(
@ -844,7 +844,9 @@ OSTD_EXPORT void cs_state::set_var_int_checked(cs_ivar *iv, cs_int v) {
iv->changed(*this); iv->changed(*this);
} }
OSTD_EXPORT void cs_state::set_var_int_checked(cs_ivar *iv, cs_value_r args) { OSTD_EXPORT void cs_state::set_var_int_checked(
cs_ivar *iv, std::span<cs_value> args
) {
cs_int v = args[0].force_int(); cs_int v = args[0].force_int();
if ((iv->get_flags() & CS_IDF_HEX) && (args.size() > 1)) { if ((iv->get_flags() & CS_IDF_HEX) && (args.size() > 1)) {
v = (v << 16) | (args[1].force_int() << 8); v = (v << 16) | (args[1].force_int() << 8);

View File

@ -31,7 +31,7 @@ struct cs_arg_val<std::string_view> {
template<typename T, typename F> template<typename T, typename F>
static inline void cs_list_find( static inline void cs_list_find(
cs_state &cs, cs_value_r args, cs_value &res, F cmp cs_state &cs, std::span<cs_value> args, cs_value &res, F cmp
) { ) {
cs_int n = 0, skip = args[2].get_int(); cs_int n = 0, skip = args[2].get_int();
T val = cs_arg_val<T>::get(args[1]); T val = cs_arg_val<T>::get(args[1]);
@ -53,7 +53,7 @@ notfound:
template<typename T, typename F> template<typename T, typename F>
static inline void cs_list_assoc( static inline void cs_list_assoc(
cs_state &cs, cs_value_r args, cs_value &res, F cmp cs_state &cs, std::span<cs_value> args, cs_value &res, F cmp
) { ) {
T val = cs_arg_val<T>::get(args[1]); T val = cs_arg_val<T>::get(args[1]);
for (cs_list_parse_state p{args[0].get_str()}; list_parse(p, cs);) { for (cs_list_parse_state p{args[0].get_str()}; list_parse(p, cs);) {
@ -115,7 +115,7 @@ int cs_list_includes(
template<bool PushList, bool Swap, typename F> template<bool PushList, bool Swap, typename F>
static inline void cs_list_merge( static inline void cs_list_merge(
cs_state &cs, cs_value_r args, cs_value &res, F cmp cs_state &cs, std::span<cs_value> args, cs_value &res, F cmp
) { ) {
std::string_view list = args[0].get_str(); std::string_view list = args[0].get_str();
std::string_view elems = args[1].get_str(); std::string_view elems = args[1].get_str();

View File

@ -43,7 +43,7 @@ struct cs_math_noop {
template<typename T, typename F1, typename F2> template<typename T, typename F1, typename F2>
static inline void cs_mathop( static inline void cs_mathop(
cs_value_r args, cs_value &res, T initval, std::span<cs_value> args, cs_value &res, T initval,
F1 binop, F2 unop F1 binop, F2 unop
) { ) {
T val; T val;
@ -59,7 +59,7 @@ static inline void cs_mathop(
} }
template<typename T, typename F> template<typename T, typename F>
static inline void cs_cmpop(cs_value_r args, cs_value &res, F cmp) { static inline void cs_cmpop(std::span<cs_value> args, cs_value &res, F cmp) {
bool val; bool val;
if (args.size() >= 2) { if (args.size() >= 2) {
val = cmp(cs_math_val<T>::get(args[0]), cs_math_val<T>::get(args[1])); val = cmp(cs_math_val<T>::get(args[0]), cs_math_val<T>::get(args[1]));

View File

@ -7,7 +7,9 @@
namespace cscript { namespace cscript {
template<typename F> template<typename F>
static inline void cs_strgcmp(cs_value_r args, cs_value &res, F cfunc) { static inline void cs_strgcmp(
std::span<cs_value> args, cs_value &res, F cfunc
) {
bool val; bool val;
if (args.size() >= 2) { if (args.size() >= 2) {
val = cfunc(args[0].get_str(), args[1].get_str()); val = cfunc(args[0].get_str(), args[1].get_str());