diff --git a/include/cubescript/cubescript.hh b/include/cubescript/cubescript.hh index 0a76bde6..324e71e2 100644 --- a/include/cubescript/cubescript.hh +++ b/include/cubescript/cubescript.hh @@ -21,7 +21,7 @@ namespace cscript { -using CsString = ostd::String; +using CsString = std::string; static_assert(ostd::IsIntegral, "CsInt must be integral"); static_assert(ostd::IsSigned, "CsInt must be signed"); diff --git a/src/cs_val.cc b/src/cs_val.cc index bf442ad9..a64a68df 100644 --- a/src/cs_val.cc +++ b/src/cs_val.cc @@ -58,9 +58,7 @@ CsValue &CsValue::operator=(CsValue const &v) { case CsValueType::String: case CsValueType::Cstring: case CsValueType::Macro: - set_str( - ostd::ConstCharRange(csv_get(v.p_stor), v.p_len) - ); + set_str(CsString{csv_get(v.p_stor), v.p_len}); break; case CsValueType::Code: set_code(cs_copy_code(v.get_code())); @@ -100,15 +98,9 @@ void CsValue::set_str(CsString val) { csv_cleanup(p_type, p_stor); p_type = CsValueType::String; p_len = val.size(); - if (p_len == 0) { - /* ostd zero length strings cannot be releaseed */ - char *buf = new char[1]; - buf[0] = '\0'; - csv_get(p_stor) = buf; - return; - } - csv_get(p_stor) = val.data(); - val.release(); + char *buf = new char[p_len + 1]; + memcpy(buf, val.data(), p_len + 1); + csv_get(p_stor) = buf; } void CsValue::set_null() { @@ -197,10 +189,10 @@ ostd::ConstCharRange CsValue::force_str() { CsString rs; switch (get_type()) { case CsValueType::Float: - rs = std::move(floatstr(csv_get(p_stor))); + rs = floatstr(csv_get(p_stor)); break; case CsValueType::Int: - rs = std::move(intstr(csv_get(p_stor))); + rs = intstr(csv_get(p_stor)); break; case CsValueType::Macro: case CsValueType::Cstring: @@ -270,7 +262,7 @@ CsString CsValue::get_str() const { case CsValueType::String: case CsValueType::Macro: case CsValueType::Cstring: - return ostd::ConstCharRange(csv_get(p_stor), p_len); + return CsString{csv_get(p_stor), p_len}; case CsValueType::Int: return intstr(csv_get(p_stor)); case CsValueType::Float: @@ -299,7 +291,7 @@ void CsValue::get_val(CsValue &r) const { case CsValueType::Macro: case CsValueType::Cstring: r.set_str( - ostd::ConstCharRange(csv_get(p_stor), p_len) + CsString{csv_get(p_stor), p_len} ); break; case CsValueType::Int: diff --git a/src/cs_vm.cc b/src/cs_vm.cc index d133f544..6f730709 100644 --- a/src/cs_vm.cc +++ b/src/cs_vm.cc @@ -777,9 +777,9 @@ static uint32_t *runcode(CsState &cs, uint32_t *code, CsValue &result) { case CsCodeVal | CsRetString: { uint32_t len = op >> 8; - args[numargs++].set_str(ostd::ConstCharRange( + args[numargs++].set_str(CsString{ reinterpret_cast(code), len - )); + }); code += len / sizeof(uint32_t) + 1; continue; } @@ -829,7 +829,7 @@ static uint32_t *runcode(CsState &cs, uint32_t *code, CsValue &result) { numargs++; continue; case CsCodeDup | CsRetString: - args[numargs].set_str(std::move(args[numargs - 1].get_str())); + args[numargs].set_str(args[numargs - 1].get_str()); numargs++; continue; @@ -994,22 +994,24 @@ static uint32_t *runcode(CsState &cs, uint32_t *code, CsValue &result) { CsValue &arg = args[numargs - 1]; switch (cs_get_lookupu_type(cs, arg, id, op)) { case CsIdAlias: - arg.set_str(std::move( + arg.set_str( static_cast(id)->get_value().get_str() - )); + ); continue; case CsIdSvar: - arg.set_str(static_cast(id)->get_value()); + arg.set_str(CsString{ + static_cast(id)->get_value() + }); continue; case CsIdIvar: - arg.set_str(std::move( + arg.set_str( intstr(static_cast(id)->get_value()) - )); + ); continue; case CsIdFvar: - arg.set_str(std::move( + arg.set_str( floatstr(static_cast(id)->get_value()) - )); + ); continue; case CsIdUnknown: arg.set_str(""); @@ -1020,7 +1022,7 @@ static uint32_t *runcode(CsState &cs, uint32_t *code, CsValue &result) { } case CsCodeLookup | CsRetString: args[numargs++].set_str( - std::move(cs_get_lookup_id(cs, op)->get_value().get_str()) + cs_get_lookup_id(cs, op)->get_value().get_str() ); continue; case CsCodeLookupArg | CsRetString: { @@ -1028,9 +1030,7 @@ static uint32_t *runcode(CsState &cs, uint32_t *code, CsValue &result) { if (!a) { args[numargs++].set_str(""); } else { - args[numargs++].set_str( - std::move(a->get_value().get_str()) - ); + args[numargs++].set_str(a->get_value().get_str()); } continue; } @@ -1130,7 +1130,9 @@ static uint32_t *runcode(CsState &cs, uint32_t *code, CsValue &result) { static_cast(id)->get_value().get_val(arg); continue; case CsIdSvar: - arg.set_str(static_cast(id)->get_value()); + arg.set_str(CsString{ + static_cast(id)->get_value() + }); continue; case CsIdIvar: arg.set_int(static_cast(id)->get_value()); @@ -1171,14 +1173,14 @@ static uint32_t *runcode(CsState &cs, uint32_t *code, CsValue &result) { arg.set_cstr(static_cast(id)->get_value()); continue; case CsIdIvar: - arg.set_str(std::move( + arg.set_str( intstr(static_cast(id)->get_value()) - )); + ); continue; case CsIdFvar: - arg.set_str(std::move( + arg.set_str( floatstr(static_cast(id)->get_value()) - )); + ); continue; case CsIdUnknown: arg.set_cstr(""); @@ -1237,9 +1239,9 @@ static uint32_t *runcode(CsState &cs, uint32_t *code, CsValue &result) { case CsCodeSvar | CsRetString: case CsCodeSvar | CsRetNull: - args[numargs++].set_str(static_cast( + args[numargs++].set_str(CsString{static_cast( cs.p_state->identmap[op >> 8] - )->get_value()); + )->get_value()}); continue; case CsCodeSvar | CsRetInt: args[numargs++].set_int(cs_parse_int(static_cast( @@ -1270,9 +1272,9 @@ static uint32_t *runcode(CsState &cs, uint32_t *code, CsValue &result) { )->get_value()); continue; case CsCodeIvar | CsRetString: - args[numargs++].set_str(std::move(intstr(static_cast( + args[numargs++].set_str(intstr(static_cast( cs.p_state->identmap[op >> 8] - )->get_value()))); + )->get_value())); continue; case CsCodeIvar | CsRetFloat: args[numargs++].set_float(CsFloat(static_cast( @@ -1309,11 +1311,11 @@ static uint32_t *runcode(CsState &cs, uint32_t *code, CsValue &result) { )->get_value()); continue; case CsCodeFvar | CsRetString: - args[numargs++].set_str(std::move(floatstr( + args[numargs++].set_str(floatstr( static_cast( cs.p_state->identmap[op >> 8] )->get_value() - ))); + )); continue; case CsCodeFvar | CsRetInt: args[numargs++].set_int(int(static_cast( @@ -1833,7 +1835,7 @@ ostd::Maybe CsState::run_file_str(ostd::ConstCharRange fname) { if (!cs_run_file(*this, fname, ret)) { return ostd::nothing; } - return std::move(ret.get_str()); + return ret.get_str(); } ostd::Maybe CsState::run_file_int(ostd::ConstCharRange fname) { diff --git a/src/cubescript.cc b/src/cubescript.cc index 65b94d23..99391d08 100644 --- a/src/cubescript.cc +++ b/src/cubescript.cc @@ -259,7 +259,7 @@ CsString CsFvar::to_printable() const { } ostd::ConstCharRange CsSvar::get_value() const { - return p_storage.iter(); + return ostd::iter(p_storage); } void CsSvar::set_value(CsString val) { p_storage = std::move(val); @@ -619,7 +619,7 @@ void CsState::set_alias(ostd::ConstCharRange name, CsValue v) { } void CsState::print_var(CsVar *v) { - writeln(v->to_printable()); + ostd::writeln(v->to_printable()); } void CsAlias::get_cstr(CsValue &v) const { @@ -632,10 +632,10 @@ void CsAlias::get_cstr(CsValue &v) const { v.set_cstr(p_val.get_strr()); break; case CsValueType::Int: - v.set_str(std::move(intstr(p_val.get_int()))); + v.set_str(intstr(p_val.get_int())); break; case CsValueType::Float: - v.set_str(std::move(floatstr(p_val.get_float()))); + v.set_str(floatstr(p_val.get_float())); break; default: v.set_cstr(""); @@ -758,7 +758,7 @@ void CsState::set_var_str( *this, sv, sv->p_flags, [&sv]() { sv->p_overrideval = sv->get_value(); } ); - sv->set_value(v); + sv->set_value(CsString{v}); if (dofunc) { sv->changed(*this); } @@ -829,7 +829,7 @@ CsState::get_alias_val(ostd::ConstCharRange name) { if ((a->get_index() < MaxArguments) && !cs_is_arg_used(*this, a)) { return ostd::nothing; } - return std::move(a->get_value().get_str()); + return a->get_value().get_str(); } CsInt cs_clamp_var(CsState &cs, CsIvar *iv, CsInt v) { @@ -923,7 +923,7 @@ void CsState::set_var_str_checked(CsSvar *sv, ostd::ConstCharRange v) { *this, sv, sv->p_flags, [&sv]() { sv->p_overrideval = sv->get_value(); } ); - sv->set_value(v); + sv->set_value(CsString{v}); sv->changed(*this); } @@ -1051,7 +1051,7 @@ void cs_init_lib_base(CsState &gcs) { try { cs.run(args[0].get_code(), result); } catch (CsErrorException const &e) { - result.set_str(e.what()); + result.set_str(CsString{e.what()}); if (e.get_stack().get()) { auto app = ostd::appender(); cscript::util::print_stack(app, e.get_stack()); diff --git a/src/lib_list.cc b/src/lib_list.cc index c5f93837..9270d556 100644 --- a/src/lib_list.cc +++ b/src/lib_list.cc @@ -180,7 +180,7 @@ void cs_init_lib_list(CsState &gcs) { if (offset > 0) { p.skip(); } - res.set_str(p.get_input()); + res.set_str(CsString{p.get_input()}); return; } @@ -191,7 +191,7 @@ void cs_init_lib_list(CsState &gcs) { } ostd::ConstCharRange quote = p.get_raw_item(true); char const *qend = !quote.empty() ? "e[quote.size()] : list; - res.set_str(ostd::ConstCharRange(list, qend - list)); + res.set_str(CsString{list, size_t(qend - list)}); }); gcs.new_command("listfind", "rse", [](auto &cs, auto args, auto &res) { @@ -204,7 +204,7 @@ void cs_init_lib_list(CsState &gcs) { int n = -1; for (util::ListParser p(cs, args[1].get_strr()); p.parse();) { ++n; - idv.set_str(p.get_raw_item()); + idv.set_str(CsString{p.get_raw_item()}); idv.push(); if (cs.run_bool(body)) { res.set_int(CsInt(n)); @@ -223,7 +223,7 @@ void cs_init_lib_list(CsState &gcs) { int n = -1; for (util::ListParser p(cs, args[1].get_strr()); p.parse();) { ++n; - idv.set_str(p.get_raw_item()); + idv.set_str(CsString{p.get_raw_item()}); idv.push(); if (cs.run_bool(body)) { if (p.parse()) { @@ -389,7 +389,7 @@ end: CsString r; int n = 0; for (util::ListParser p(cs, args[1].get_strr()); p.parse(); ++n) { - idv.set_str(p.get_raw_item()); + idv.set_str(CsString{p.get_raw_item()}); idv.push(); if (cs.run_bool(body)) { if (r.size()) { @@ -409,7 +409,7 @@ end: auto body = args[2].get_code(); int n = 0, r = 0; for (util::ListParser p(cs, args[1].get_strr()); p.parse(); ++n) { - idv.set_str(p.get_raw_item()); + idv.set_str(CsString{p.get_raw_item()}); idv.push(); if (cs.run_bool(body)) { r++; @@ -549,7 +549,7 @@ static void cs_list_sort( } if (items.empty()) { - res.set_str(list); + res.set_str(CsString{list}); return; } diff --git a/src/lib_str.cc b/src/lib_str.cc index 2b381130..81207445 100644 --- a/src/lib_str.cc +++ b/src/lib_str.cc @@ -99,7 +99,7 @@ void cs_init_lib_string(CsState &cs) { } CsString s; CsString fs = args[0].get_str(); - ostd::ConstCharRange f = fs.iter(); + ostd::ConstCharRange f = ostd::iter(fs); while (!f.empty()) { char c = *f; ++f; @@ -135,12 +135,12 @@ void cs_init_lib_string(CsState &cs) { CsInt start = args[1].get_int(), count = args[2].get_int(); CsInt numargs = args[3].get_int(); CsInt len = CsInt(s.size()), offset = ostd::clamp(start, CsInt(0), len); - res.set_str(ostd::ConstCharRange( + res.set_str(CsString{ &s[offset], (numargs >= 3) - ? ostd::clamp(count, CsInt(0), len - offset) - : (len - offset) - )); + ? size_t(ostd::clamp(count, CsInt(0), len - offset)) + : size_t(len - offset) + }); }); cs.new_command("strcmp", "s1V", [](auto &, auto args, auto &res) { @@ -175,7 +175,7 @@ void cs_init_lib_string(CsState &cs) { } CsString buf; if (!oldval.size()) { - res.set_str(s); + res.set_str(CsString{s}); return; } for (size_t i = 0;; ++i) { diff --git a/tools/edit_fallback.hh b/tools/edit_fallback.hh index 1609de26..da9742fb 100644 --- a/tools/edit_fallback.hh +++ b/tools/edit_fallback.hh @@ -7,9 +7,9 @@ static void init_lineedit(CsState &, ostd::ConstCharRange) { } -static ostd::Maybe read_line(CsState &, CsSvar *pr) { +static ostd::Maybe read_line(CsState &, CsSvar *pr) { ostd::write(pr->get_value()); - ostd::String ret; + std::string ret; /* i really need to implement some sort of get_line for ostd streams */ for (char c = ostd::in.getchar(); c && (c != '\n'); c = ostd::in.getchar()) { ret += c; diff --git a/tools/edit_linenoise.hh b/tools/edit_linenoise.hh index ffc160a9..b9603b6b 100644 --- a/tools/edit_linenoise.hh +++ b/tools/edit_linenoise.hh @@ -36,7 +36,7 @@ static char *ln_hint(char const *buf, int *color, int *bold) { if (!cmd) { return nullptr; } - ostd::String args = " ["; + std::string args = " ["; fill_cmd_args(args, cmd->get_args()); args += ']'; *color = 35; @@ -59,7 +59,7 @@ static void init_lineedit(CsState &cs, ostd::ConstCharRange) { linenoiseSetFreeHintsCallback(ln_hint_free); } -static ostd::Maybe read_line(CsState &, CsSvar *pr) { +static ostd::Maybe read_line(CsState &, CsSvar *pr) { auto line = linenoise(pr->get_value().data()); if (!line) { /* linenoise traps ctrl-c, detect it and let the user exit */ @@ -67,15 +67,15 @@ static ostd::Maybe read_line(CsState &, CsSvar *pr) { raise(SIGINT); return ostd::nothing; } - return ostd::String(); + return std::string{}; } - ostd::String ret = line; + std::string ret = line; linenoiseFree(line); return std::move(ret); } static void add_history(CsState &, ostd::ConstCharRange line) { - /* backed by ostd::String so it's terminated */ + /* backed by std::string so it's terminated */ linenoiseHistoryAdd(line.data()); } diff --git a/tools/edit_readline.hh b/tools/edit_readline.hh index 6aee5a8e..4aa81459 100644 --- a/tools/edit_readline.hh +++ b/tools/edit_readline.hh @@ -51,8 +51,8 @@ void ln_hint() { rl_redisplay(); return; } - ostd::String old = rl_line_buffer; - ostd::String args = old; + std::string old = rl_line_buffer; + std::string args = old; args += " ["; fill_cmd_args(args, cmd->get_args()); args += "] "; @@ -68,18 +68,18 @@ static void init_lineedit(CsState &cs, ostd::ConstCharRange) { rl_redisplay_function = ln_hint; } -static ostd::Maybe read_line(CsState &, CsSvar *pr) { +static ostd::Maybe read_line(CsState &, CsSvar *pr) { auto line = readline(pr->get_value().data()); if (!line) { - return ostd::String(); + return std::string(); } - ostd::String ret = line; + std::string ret = line; free(line); return std::move(ret); } static void add_history(CsState &, ostd::ConstCharRange line) { - /* backed by ostd::String so it's terminated */ + /* backed by std::string so it's terminated */ add_history(line.data()); } diff --git a/tools/repl.cc b/tools/repl.cc index a6e4d29c..ed76f790 100644 --- a/tools/repl.cc +++ b/tools/repl.cc @@ -70,7 +70,7 @@ static inline ostd::ConstCharRange get_arg_type(char arg) { return "illegal"; } -static inline void fill_cmd_args(ostd::String &writer, ostd::ConstCharRange args) { +static inline void fill_cmd_args(std::string &writer, ostd::ConstCharRange args) { char variadic = '\0'; int nrep = 0; if (!args.empty() && ((args.back() == 'V') || (args.back() == 'C'))) { @@ -368,7 +368,7 @@ endargs: do_tty(gcs); return 0; } else { - ostd::String str; + std::string str; for (char c = '\0'; (c = ostd::in.getchar()) != EOF;) { str += c; }