From 320fdbaefdf339db08974fc976f794f85319ae47 Mon Sep 17 00:00:00 2001 From: Daniel Kolesa Date: Sat, 20 Mar 2021 05:41:25 +0100 Subject: [PATCH] use c++20 std::span --- include/cubescript/cubescript.hh | 13 +++--- include/cubescript/cubescript_conf.hh | 7 +--- src/cs_util.cc | 2 +- src/cs_vm.cc | 60 ++++++++++++++++----------- src/cubescript.cc | 16 +++---- src/lib_list.cc | 6 +-- src/lib_math.cc | 4 +- src/lib_str.cc | 4 +- 8 files changed, 63 insertions(+), 49 deletions(-) diff --git a/include/cubescript/cubescript.hh b/include/cubescript/cubescript.hh index d293f47..ec9f41c 100644 --- a/include/cubescript/cubescript.hh +++ b/include/cubescript/cubescript.hh @@ -457,8 +457,8 @@ struct OSTD_EXPORT cs_state { cs_alias *get_alias(std::string_view name); bool have_ident(std::string_view name); - cs_ident_r get_idents(); - cs_const_ident_r get_idents() const; + std::span get_idents(); + std::span get_idents() const; void reset_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(std::string_view code, cs_value &ret); 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 args, cs_value &ret); cs_value run(cs_bcode *code); cs_value run(std::string_view code); 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 args); cs_loop_state run_loop(cs_bcode *code, cs_value &ret); 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_value_r args); + void set_var_int_checked(cs_ivar *iv, std::span args); void set_var_float_checked(cs_fvar *fv, cs_float 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 cs_strref value_list_concat( - cs_state &cs, cs_value_r vals, std::string_view sep = std::string_view{} + cs_state &cs, std::span vals, + std::string_view sep = std::string_view{} ); namespace util { diff --git a/include/cubescript/cubescript_conf.hh b/include/cubescript/cubescript_conf.hh index dd6dcc5..5c6386d 100644 --- a/include/cubescript/cubescript_conf.hh +++ b/include/cubescript/cubescript_conf.hh @@ -3,6 +3,7 @@ #include #include +#include #include /* do not modify */ @@ -11,10 +12,6 @@ namespace cscript { struct cs_ident; struct cs_value; struct cs_var; - - using cs_value_r = ostd::iterator_range; - using cs_ident_r = ostd::iterator_range; - using cs_const_ident_r = ostd::iterator_range; } /* configurable section */ @@ -33,7 +30,7 @@ namespace cscript { */ using cs_var_cb = std::function; using cs_vprint_cb = std::function; - using cs_command_cb = std::function; + using cs_command_cb = std::function, cs_value &)>; using cs_hook_cb = std::function; using cs_alloc_cb = void *(*)(void *, void *, size_t, size_t); diff --git a/src/cs_util.cc b/src/cs_util.cc index f879916..fbe9246 100644 --- a/src/cs_util.cc +++ b/src/cs_util.cc @@ -557,7 +557,7 @@ OSTD_EXPORT void list_find_item(cs_list_parse_state &ps) { } OSTD_EXPORT cs_strref value_list_concat( - cs_state &cs, cs_value_r vals, std::string_view sep + cs_state &cs, std::span vals, std::string_view sep ) { auto app = ostd::appender(cs); for (std::size_t i = 0; i < vals.size(); ++i) { diff --git a/src/cs_vm.cc b/src/cs_vm.cc index 70305d9..c0a2c43 100644 --- a/src/cs_vm.cc +++ b/src/cs_vm.cc @@ -9,7 +9,7 @@ namespace cscript { struct cs_cmd_internal { static void call( - cs_state &cs, cs_command *c, cs_value_r args, cs_value &ret + cs_state &cs, cs_command *c, std::span args, cs_value &ret ) { c->p_cb_cftv(cs, args, ret); } @@ -424,13 +424,19 @@ static inline void callcommand( case 'C': { i = std::max(i + 1, numargs); cs_value tv{cs}; - tv.set_str(value_list_concat(cs, ostd::iter(args, args + i), " ")); - cs_cmd_internal::call(cs, id, cs_value_r(&tv, &tv + 1), res); + tv.set_str(value_list_concat( + cs, std::span{args, std::size_t(i)}, " " + )); + cs_cmd_internal::call( + cs, id, std::span(&tv, &tv + 1), res + ); return; } case 'V': 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; case '1': case '2': @@ -444,7 +450,9 @@ static inline void callcommand( } } ++i; - cs_cmd_internal::call(cs, id, cs_value_r(args, args + i), res); + cs_cmd_internal::call( + cs, id, std::span{args, std::size_t(i)}, res + ); } 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(); result.force_none(); - cs_cmd_internal::call(cs, id, cs_value_r( - &args[0] + offset, &args[0] + offset + id->get_num_args() - ), result); + cs_cmd_internal::call(cs, id, std::span{ + &args[0] + offset, std::size_t(id->get_num_args()) + }, result); force_arg(result, op & CS_CODE_RET_MASK); numargs = offset; continue; @@ -1315,11 +1323,12 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) { cs_command *id = static_cast( 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(); - cs_cmd_internal::call(cs, id, ostd::iter( - &args[offset], &args[offset + callargs] - ), result); + cs_cmd_internal::call( + cs, id, std::span{&args[offset], callargs}, result + ); force_arg(result, op & CS_CODE_RET_MASK); numargs = offset; continue; @@ -1331,14 +1340,17 @@ static uint32_t *runcode(cs_state &cs, uint32_t *code, cs_value &result) { cs_command *id = static_cast( 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(); { cs_value tv{cs}; - tv.set_str(value_list_concat(cs, ostd::iter( - &args[offset], &args[offset + callargs] - ), " ")); - cs_cmd_internal::call(cs, id, cs_value_r(&tv, &tv + 1), result); + tv.set_str(value_list_concat(cs, std::span{ + &args[offset], callargs + }, " ")); + cs_cmd_internal::call( + cs, id, std::span{&tv, 1}, result + ); } force_arg(result, op & CS_CODE_RET_MASK); 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_FLOAT: case CS_CODE_CONC_W | CS_RET_INT: { - int numconc = op >> 8; + std::size_t numconc = op >> 8; 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) ? " " : "" ); 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_FLOAT: case CS_CODE_CONC_M | CS_RET_INT: { - int numconc = op >> 8; + std::size_t numconc = op >> 8; auto buf = value_list_concat( - cs, ostd::iter(&args[numargs - numconc], &args[numargs]) + cs, std::span{&args[numargs - numconc], numconc} ); numargs = numargs - numconc; result.set_str(buf); @@ -1501,7 +1513,7 @@ noid: } else { cs.set_var_int_checked( static_cast(id), - ostd::iter(&args[offset], &args[offset + callargs]) + std::span{&args[offset], std::size_t(callargs)} ); } numargs = offset - 1; @@ -1588,7 +1600,7 @@ void cs_state::run( 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 args, cs_value &ret) { int nargs = int(args.size()); ret.set_none(); 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; } -cs_value cs_state::run(cs_ident *id, cs_value_r args) { +cs_value cs_state::run(cs_ident *id, std::span args) { cs_value ret{*this}; run(id, args, ret); return ret; diff --git a/src/cubescript.cc b/src/cubescript.cc index 6dd04e6..9bbbf8f 100644 --- a/src/cubescript.cc +++ b/src/cubescript.cc @@ -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(); } -OSTD_EXPORT cs_ident_r cs_state::get_idents() { - return cs_ident_r( +OSTD_EXPORT std::span cs_state::get_idents() { + return std::span{ 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_state::get_idents() const { auto ptr = const_cast(p_state->identmap.data()); - return cs_const_ident_r(ptr, ptr + p_state->identmap.size()); + return std::span{ptr, p_state->identmap.size()}; } 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); } -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 args +) { cs_int v = args[0].force_int(); if ((iv->get_flags() & CS_IDF_HEX) && (args.size() > 1)) { v = (v << 16) | (args[1].force_int() << 8); diff --git a/src/lib_list.cc b/src/lib_list.cc index e860cce..3789507 100644 --- a/src/lib_list.cc +++ b/src/lib_list.cc @@ -31,7 +31,7 @@ struct cs_arg_val { template static inline void cs_list_find( - cs_state &cs, cs_value_r args, cs_value &res, F cmp + cs_state &cs, std::span args, cs_value &res, F cmp ) { cs_int n = 0, skip = args[2].get_int(); T val = cs_arg_val::get(args[1]); @@ -53,7 +53,7 @@ notfound: template static inline void cs_list_assoc( - cs_state &cs, cs_value_r args, cs_value &res, F cmp + cs_state &cs, std::span args, cs_value &res, F cmp ) { T val = cs_arg_val::get(args[1]); for (cs_list_parse_state p{args[0].get_str()}; list_parse(p, cs);) { @@ -115,7 +115,7 @@ int cs_list_includes( template static inline void cs_list_merge( - cs_state &cs, cs_value_r args, cs_value &res, F cmp + cs_state &cs, std::span args, cs_value &res, F cmp ) { std::string_view list = args[0].get_str(); std::string_view elems = args[1].get_str(); diff --git a/src/lib_math.cc b/src/lib_math.cc index 42f45b0..a27ba18 100644 --- a/src/lib_math.cc +++ b/src/lib_math.cc @@ -43,7 +43,7 @@ struct cs_math_noop { template static inline void cs_mathop( - cs_value_r args, cs_value &res, T initval, + std::span args, cs_value &res, T initval, F1 binop, F2 unop ) { T val; @@ -59,7 +59,7 @@ static inline void cs_mathop( } template -static inline void cs_cmpop(cs_value_r args, cs_value &res, F cmp) { +static inline void cs_cmpop(std::span args, cs_value &res, F cmp) { bool val; if (args.size() >= 2) { val = cmp(cs_math_val::get(args[0]), cs_math_val::get(args[1])); diff --git a/src/lib_str.cc b/src/lib_str.cc index 9e71ded..3e86e8e 100644 --- a/src/lib_str.cc +++ b/src/lib_str.cc @@ -7,7 +7,9 @@ namespace cscript { template -static inline void cs_strgcmp(cs_value_r args, cs_value &res, F cfunc) { +static inline void cs_strgcmp( + std::span args, cs_value &res, F cfunc +) { bool val; if (args.size() >= 2) { val = cfunc(args[0].get_str(), args[1].get_str());