update according to ostd

master
Daniel Kolesa 2017-01-30 01:18:55 +01:00
parent 70f4e0742e
commit 8dc423dcaa
10 changed files with 73 additions and 79 deletions

View File

@ -21,7 +21,7 @@
namespace cscript { namespace cscript {
using CsString = ostd::String; using CsString = std::string;
static_assert(ostd::IsIntegral<CsInt>, "CsInt must be integral"); static_assert(ostd::IsIntegral<CsInt>, "CsInt must be integral");
static_assert(ostd::IsSigned<CsInt>, "CsInt must be signed"); static_assert(ostd::IsSigned<CsInt>, "CsInt must be signed");

View File

@ -58,9 +58,7 @@ CsValue &CsValue::operator=(CsValue const &v) {
case CsValueType::String: case CsValueType::String:
case CsValueType::Cstring: case CsValueType::Cstring:
case CsValueType::Macro: case CsValueType::Macro:
set_str( set_str(CsString{csv_get<char const *>(v.p_stor), v.p_len});
ostd::ConstCharRange(csv_get<char const *>(v.p_stor), v.p_len)
);
break; break;
case CsValueType::Code: case CsValueType::Code:
set_code(cs_copy_code(v.get_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); csv_cleanup(p_type, p_stor);
p_type = CsValueType::String; p_type = CsValueType::String;
p_len = val.size(); p_len = val.size();
if (p_len == 0) { char *buf = new char[p_len + 1];
/* ostd zero length strings cannot be releaseed */ memcpy(buf, val.data(), p_len + 1);
char *buf = new char[1]; csv_get<char *>(p_stor) = buf;
buf[0] = '\0';
csv_get<char *>(p_stor) = buf;
return;
}
csv_get<char *>(p_stor) = val.data();
val.release();
} }
void CsValue::set_null() { void CsValue::set_null() {
@ -197,10 +189,10 @@ ostd::ConstCharRange CsValue::force_str() {
CsString rs; CsString rs;
switch (get_type()) { switch (get_type()) {
case CsValueType::Float: case CsValueType::Float:
rs = std::move(floatstr(csv_get<CsFloat>(p_stor))); rs = floatstr(csv_get<CsFloat>(p_stor));
break; break;
case CsValueType::Int: case CsValueType::Int:
rs = std::move(intstr(csv_get<CsInt>(p_stor))); rs = intstr(csv_get<CsInt>(p_stor));
break; break;
case CsValueType::Macro: case CsValueType::Macro:
case CsValueType::Cstring: case CsValueType::Cstring:
@ -270,7 +262,7 @@ CsString CsValue::get_str() const {
case CsValueType::String: case CsValueType::String:
case CsValueType::Macro: case CsValueType::Macro:
case CsValueType::Cstring: case CsValueType::Cstring:
return ostd::ConstCharRange(csv_get<char const *>(p_stor), p_len); return CsString{csv_get<char const *>(p_stor), p_len};
case CsValueType::Int: case CsValueType::Int:
return intstr(csv_get<CsInt>(p_stor)); return intstr(csv_get<CsInt>(p_stor));
case CsValueType::Float: case CsValueType::Float:
@ -299,7 +291,7 @@ void CsValue::get_val(CsValue &r) const {
case CsValueType::Macro: case CsValueType::Macro:
case CsValueType::Cstring: case CsValueType::Cstring:
r.set_str( r.set_str(
ostd::ConstCharRange(csv_get<char const *>(p_stor), p_len) CsString{csv_get<char const *>(p_stor), p_len}
); );
break; break;
case CsValueType::Int: case CsValueType::Int:

View File

@ -777,9 +777,9 @@ static uint32_t *runcode(CsState &cs, uint32_t *code, CsValue &result) {
case CsCodeVal | CsRetString: { case CsCodeVal | CsRetString: {
uint32_t len = op >> 8; uint32_t len = op >> 8;
args[numargs++].set_str(ostd::ConstCharRange( args[numargs++].set_str(CsString{
reinterpret_cast<char const *>(code), len reinterpret_cast<char const *>(code), len
)); });
code += len / sizeof(uint32_t) + 1; code += len / sizeof(uint32_t) + 1;
continue; continue;
} }
@ -829,7 +829,7 @@ static uint32_t *runcode(CsState &cs, uint32_t *code, CsValue &result) {
numargs++; numargs++;
continue; continue;
case CsCodeDup | CsRetString: case CsCodeDup | CsRetString:
args[numargs].set_str(std::move(args[numargs - 1].get_str())); args[numargs].set_str(args[numargs - 1].get_str());
numargs++; numargs++;
continue; continue;
@ -994,22 +994,24 @@ static uint32_t *runcode(CsState &cs, uint32_t *code, CsValue &result) {
CsValue &arg = args[numargs - 1]; CsValue &arg = args[numargs - 1];
switch (cs_get_lookupu_type(cs, arg, id, op)) { switch (cs_get_lookupu_type(cs, arg, id, op)) {
case CsIdAlias: case CsIdAlias:
arg.set_str(std::move( arg.set_str(
static_cast<CsAlias *>(id)->get_value().get_str() static_cast<CsAlias *>(id)->get_value().get_str()
)); );
continue; continue;
case CsIdSvar: case CsIdSvar:
arg.set_str(static_cast<CsSvar *>(id)->get_value()); arg.set_str(CsString{
static_cast<CsSvar *>(id)->get_value()
});
continue; continue;
case CsIdIvar: case CsIdIvar:
arg.set_str(std::move( arg.set_str(
intstr(static_cast<CsIvar *>(id)->get_value()) intstr(static_cast<CsIvar *>(id)->get_value())
)); );
continue; continue;
case CsIdFvar: case CsIdFvar:
arg.set_str(std::move( arg.set_str(
floatstr(static_cast<CsFvar *>(id)->get_value()) floatstr(static_cast<CsFvar *>(id)->get_value())
)); );
continue; continue;
case CsIdUnknown: case CsIdUnknown:
arg.set_str(""); arg.set_str("");
@ -1020,7 +1022,7 @@ static uint32_t *runcode(CsState &cs, uint32_t *code, CsValue &result) {
} }
case CsCodeLookup | CsRetString: case CsCodeLookup | CsRetString:
args[numargs++].set_str( 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; continue;
case CsCodeLookupArg | CsRetString: { case CsCodeLookupArg | CsRetString: {
@ -1028,9 +1030,7 @@ static uint32_t *runcode(CsState &cs, uint32_t *code, CsValue &result) {
if (!a) { if (!a) {
args[numargs++].set_str(""); args[numargs++].set_str("");
} else { } else {
args[numargs++].set_str( args[numargs++].set_str(a->get_value().get_str());
std::move(a->get_value().get_str())
);
} }
continue; continue;
} }
@ -1130,7 +1130,9 @@ static uint32_t *runcode(CsState &cs, uint32_t *code, CsValue &result) {
static_cast<CsAlias *>(id)->get_value().get_val(arg); static_cast<CsAlias *>(id)->get_value().get_val(arg);
continue; continue;
case CsIdSvar: case CsIdSvar:
arg.set_str(static_cast<CsSvar *>(id)->get_value()); arg.set_str(CsString{
static_cast<CsSvar *>(id)->get_value()
});
continue; continue;
case CsIdIvar: case CsIdIvar:
arg.set_int(static_cast<CsIvar *>(id)->get_value()); arg.set_int(static_cast<CsIvar *>(id)->get_value());
@ -1171,14 +1173,14 @@ static uint32_t *runcode(CsState &cs, uint32_t *code, CsValue &result) {
arg.set_cstr(static_cast<CsSvar *>(id)->get_value()); arg.set_cstr(static_cast<CsSvar *>(id)->get_value());
continue; continue;
case CsIdIvar: case CsIdIvar:
arg.set_str(std::move( arg.set_str(
intstr(static_cast<CsIvar *>(id)->get_value()) intstr(static_cast<CsIvar *>(id)->get_value())
)); );
continue; continue;
case CsIdFvar: case CsIdFvar:
arg.set_str(std::move( arg.set_str(
floatstr(static_cast<CsFvar *>(id)->get_value()) floatstr(static_cast<CsFvar *>(id)->get_value())
)); );
continue; continue;
case CsIdUnknown: case CsIdUnknown:
arg.set_cstr(""); arg.set_cstr("");
@ -1237,9 +1239,9 @@ static uint32_t *runcode(CsState &cs, uint32_t *code, CsValue &result) {
case CsCodeSvar | CsRetString: case CsCodeSvar | CsRetString:
case CsCodeSvar | CsRetNull: case CsCodeSvar | CsRetNull:
args[numargs++].set_str(static_cast<CsSvar *>( args[numargs++].set_str(CsString{static_cast<CsSvar *>(
cs.p_state->identmap[op >> 8] cs.p_state->identmap[op >> 8]
)->get_value()); )->get_value()});
continue; continue;
case CsCodeSvar | CsRetInt: case CsCodeSvar | CsRetInt:
args[numargs++].set_int(cs_parse_int(static_cast<CsSvar *>( args[numargs++].set_int(cs_parse_int(static_cast<CsSvar *>(
@ -1270,9 +1272,9 @@ static uint32_t *runcode(CsState &cs, uint32_t *code, CsValue &result) {
)->get_value()); )->get_value());
continue; continue;
case CsCodeIvar | CsRetString: case CsCodeIvar | CsRetString:
args[numargs++].set_str(std::move(intstr(static_cast<CsIvar *>( args[numargs++].set_str(intstr(static_cast<CsIvar *>(
cs.p_state->identmap[op >> 8] cs.p_state->identmap[op >> 8]
)->get_value()))); )->get_value()));
continue; continue;
case CsCodeIvar | CsRetFloat: case CsCodeIvar | CsRetFloat:
args[numargs++].set_float(CsFloat(static_cast<CsIvar *>( args[numargs++].set_float(CsFloat(static_cast<CsIvar *>(
@ -1309,11 +1311,11 @@ static uint32_t *runcode(CsState &cs, uint32_t *code, CsValue &result) {
)->get_value()); )->get_value());
continue; continue;
case CsCodeFvar | CsRetString: case CsCodeFvar | CsRetString:
args[numargs++].set_str(std::move(floatstr( args[numargs++].set_str(floatstr(
static_cast<CsFvar *>( static_cast<CsFvar *>(
cs.p_state->identmap[op >> 8] cs.p_state->identmap[op >> 8]
)->get_value() )->get_value()
))); ));
continue; continue;
case CsCodeFvar | CsRetInt: case CsCodeFvar | CsRetInt:
args[numargs++].set_int(int(static_cast<CsFvar *>( args[numargs++].set_int(int(static_cast<CsFvar *>(
@ -1833,7 +1835,7 @@ ostd::Maybe<CsString> CsState::run_file_str(ostd::ConstCharRange fname) {
if (!cs_run_file(*this, fname, ret)) { if (!cs_run_file(*this, fname, ret)) {
return ostd::nothing; return ostd::nothing;
} }
return std::move(ret.get_str()); return ret.get_str();
} }
ostd::Maybe<CsInt> CsState::run_file_int(ostd::ConstCharRange fname) { ostd::Maybe<CsInt> CsState::run_file_int(ostd::ConstCharRange fname) {

View File

@ -259,7 +259,7 @@ CsString CsFvar::to_printable() const {
} }
ostd::ConstCharRange CsSvar::get_value() const { ostd::ConstCharRange CsSvar::get_value() const {
return p_storage.iter(); return ostd::iter(p_storage);
} }
void CsSvar::set_value(CsString val) { void CsSvar::set_value(CsString val) {
p_storage = std::move(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) { void CsState::print_var(CsVar *v) {
writeln(v->to_printable()); ostd::writeln(v->to_printable());
} }
void CsAlias::get_cstr(CsValue &v) const { 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()); v.set_cstr(p_val.get_strr());
break; break;
case CsValueType::Int: case CsValueType::Int:
v.set_str(std::move(intstr(p_val.get_int()))); v.set_str(intstr(p_val.get_int()));
break; break;
case CsValueType::Float: case CsValueType::Float:
v.set_str(std::move(floatstr(p_val.get_float()))); v.set_str(floatstr(p_val.get_float()));
break; break;
default: default:
v.set_cstr(""); v.set_cstr("");
@ -758,7 +758,7 @@ void CsState::set_var_str(
*this, sv, sv->p_flags, *this, sv, sv->p_flags,
[&sv]() { sv->p_overrideval = sv->get_value(); } [&sv]() { sv->p_overrideval = sv->get_value(); }
); );
sv->set_value(v); sv->set_value(CsString{v});
if (dofunc) { if (dofunc) {
sv->changed(*this); 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)) { if ((a->get_index() < MaxArguments) && !cs_is_arg_used(*this, a)) {
return ostd::nothing; 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) { 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, *this, sv, sv->p_flags,
[&sv]() { sv->p_overrideval = sv->get_value(); } [&sv]() { sv->p_overrideval = sv->get_value(); }
); );
sv->set_value(v); sv->set_value(CsString{v});
sv->changed(*this); sv->changed(*this);
} }
@ -1051,7 +1051,7 @@ void cs_init_lib_base(CsState &gcs) {
try { try {
cs.run(args[0].get_code(), result); cs.run(args[0].get_code(), result);
} catch (CsErrorException const &e) { } catch (CsErrorException const &e) {
result.set_str(e.what()); result.set_str(CsString{e.what()});
if (e.get_stack().get()) { if (e.get_stack().get()) {
auto app = ostd::appender<CsString>(); auto app = ostd::appender<CsString>();
cscript::util::print_stack(app, e.get_stack()); cscript::util::print_stack(app, e.get_stack());

View File

@ -180,7 +180,7 @@ void cs_init_lib_list(CsState &gcs) {
if (offset > 0) { if (offset > 0) {
p.skip(); p.skip();
} }
res.set_str(p.get_input()); res.set_str(CsString{p.get_input()});
return; return;
} }
@ -191,7 +191,7 @@ void cs_init_lib_list(CsState &gcs) {
} }
ostd::ConstCharRange quote = p.get_raw_item(true); ostd::ConstCharRange quote = p.get_raw_item(true);
char const *qend = !quote.empty() ? &quote[quote.size()] : list; char const *qend = !quote.empty() ? &quote[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) { 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; int n = -1;
for (util::ListParser p(cs, args[1].get_strr()); p.parse();) { for (util::ListParser p(cs, args[1].get_strr()); p.parse();) {
++n; ++n;
idv.set_str(p.get_raw_item()); idv.set_str(CsString{p.get_raw_item()});
idv.push(); idv.push();
if (cs.run_bool(body)) { if (cs.run_bool(body)) {
res.set_int(CsInt(n)); res.set_int(CsInt(n));
@ -223,7 +223,7 @@ void cs_init_lib_list(CsState &gcs) {
int n = -1; int n = -1;
for (util::ListParser p(cs, args[1].get_strr()); p.parse();) { for (util::ListParser p(cs, args[1].get_strr()); p.parse();) {
++n; ++n;
idv.set_str(p.get_raw_item()); idv.set_str(CsString{p.get_raw_item()});
idv.push(); idv.push();
if (cs.run_bool(body)) { if (cs.run_bool(body)) {
if (p.parse()) { if (p.parse()) {
@ -389,7 +389,7 @@ end:
CsString r; CsString r;
int n = 0; int n = 0;
for (util::ListParser p(cs, args[1].get_strr()); p.parse(); ++n) { 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(); idv.push();
if (cs.run_bool(body)) { if (cs.run_bool(body)) {
if (r.size()) { if (r.size()) {
@ -409,7 +409,7 @@ end:
auto body = args[2].get_code(); auto body = args[2].get_code();
int n = 0, r = 0; int n = 0, r = 0;
for (util::ListParser p(cs, args[1].get_strr()); p.parse(); ++n) { 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(); idv.push();
if (cs.run_bool(body)) { if (cs.run_bool(body)) {
r++; r++;
@ -549,7 +549,7 @@ static void cs_list_sort(
} }
if (items.empty()) { if (items.empty()) {
res.set_str(list); res.set_str(CsString{list});
return; return;
} }

View File

@ -99,7 +99,7 @@ void cs_init_lib_string(CsState &cs) {
} }
CsString s; CsString s;
CsString fs = args[0].get_str(); CsString fs = args[0].get_str();
ostd::ConstCharRange f = fs.iter(); ostd::ConstCharRange f = ostd::iter(fs);
while (!f.empty()) { while (!f.empty()) {
char c = *f; char c = *f;
++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 start = args[1].get_int(), count = args[2].get_int();
CsInt numargs = args[3].get_int(); CsInt numargs = args[3].get_int();
CsInt len = CsInt(s.size()), offset = ostd::clamp(start, CsInt(0), len); CsInt len = CsInt(s.size()), offset = ostd::clamp(start, CsInt(0), len);
res.set_str(ostd::ConstCharRange( res.set_str(CsString{
&s[offset], &s[offset],
(numargs >= 3) (numargs >= 3)
? ostd::clamp(count, CsInt(0), len - offset) ? size_t(ostd::clamp(count, CsInt(0), len - offset))
: (len - offset) : size_t(len - offset)
)); });
}); });
cs.new_command("strcmp", "s1V", [](auto &, auto args, auto &res) { cs.new_command("strcmp", "s1V", [](auto &, auto args, auto &res) {
@ -175,7 +175,7 @@ void cs_init_lib_string(CsState &cs) {
} }
CsString buf; CsString buf;
if (!oldval.size()) { if (!oldval.size()) {
res.set_str(s); res.set_str(CsString{s});
return; return;
} }
for (size_t i = 0;; ++i) { for (size_t i = 0;; ++i) {

View File

@ -7,9 +7,9 @@
static void init_lineedit(CsState &, ostd::ConstCharRange) { static void init_lineedit(CsState &, ostd::ConstCharRange) {
} }
static ostd::Maybe<ostd::String> read_line(CsState &, CsSvar *pr) { static ostd::Maybe<std::string> read_line(CsState &, CsSvar *pr) {
ostd::write(pr->get_value()); ostd::write(pr->get_value());
ostd::String ret; std::string ret;
/* i really need to implement some sort of get_line for ostd streams */ /* 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()) { for (char c = ostd::in.getchar(); c && (c != '\n'); c = ostd::in.getchar()) {
ret += c; ret += c;

View File

@ -36,7 +36,7 @@ static char *ln_hint(char const *buf, int *color, int *bold) {
if (!cmd) { if (!cmd) {
return nullptr; return nullptr;
} }
ostd::String args = " ["; std::string args = " [";
fill_cmd_args(args, cmd->get_args()); fill_cmd_args(args, cmd->get_args());
args += ']'; args += ']';
*color = 35; *color = 35;
@ -59,7 +59,7 @@ static void init_lineedit(CsState &cs, ostd::ConstCharRange) {
linenoiseSetFreeHintsCallback(ln_hint_free); linenoiseSetFreeHintsCallback(ln_hint_free);
} }
static ostd::Maybe<ostd::String> read_line(CsState &, CsSvar *pr) { static ostd::Maybe<std::string> read_line(CsState &, CsSvar *pr) {
auto line = linenoise(pr->get_value().data()); auto line = linenoise(pr->get_value().data());
if (!line) { if (!line) {
/* linenoise traps ctrl-c, detect it and let the user exit */ /* linenoise traps ctrl-c, detect it and let the user exit */
@ -67,15 +67,15 @@ static ostd::Maybe<ostd::String> read_line(CsState &, CsSvar *pr) {
raise(SIGINT); raise(SIGINT);
return ostd::nothing; return ostd::nothing;
} }
return ostd::String(); return std::string{};
} }
ostd::String ret = line; std::string ret = line;
linenoiseFree(line); linenoiseFree(line);
return std::move(ret); return std::move(ret);
} }
static void add_history(CsState &, ostd::ConstCharRange line) { 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()); linenoiseHistoryAdd(line.data());
} }

View File

@ -51,8 +51,8 @@ void ln_hint() {
rl_redisplay(); rl_redisplay();
return; return;
} }
ostd::String old = rl_line_buffer; std::string old = rl_line_buffer;
ostd::String args = old; std::string args = old;
args += " ["; args += " [";
fill_cmd_args(args, cmd->get_args()); fill_cmd_args(args, cmd->get_args());
args += "] "; args += "] ";
@ -68,18 +68,18 @@ static void init_lineedit(CsState &cs, ostd::ConstCharRange) {
rl_redisplay_function = ln_hint; rl_redisplay_function = ln_hint;
} }
static ostd::Maybe<ostd::String> read_line(CsState &, CsSvar *pr) { static ostd::Maybe<std::string> read_line(CsState &, CsSvar *pr) {
auto line = readline(pr->get_value().data()); auto line = readline(pr->get_value().data());
if (!line) { if (!line) {
return ostd::String(); return std::string();
} }
ostd::String ret = line; std::string ret = line;
free(line); free(line);
return std::move(ret); return std::move(ret);
} }
static void add_history(CsState &, ostd::ConstCharRange line) { 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()); add_history(line.data());
} }

View File

@ -70,7 +70,7 @@ static inline ostd::ConstCharRange get_arg_type(char arg) {
return "illegal"; 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'; char variadic = '\0';
int nrep = 0; int nrep = 0;
if (!args.empty() && ((args.back() == 'V') || (args.back() == 'C'))) { if (!args.empty() && ((args.back() == 'V') || (args.back() == 'C'))) {
@ -368,7 +368,7 @@ endargs:
do_tty(gcs); do_tty(gcs);
return 0; return 0;
} else { } else {
ostd::String str; std::string str;
for (char c = '\0'; (c = ostd::in.getchar()) != EOF;) { for (char c = '\0'; (c = ostd::in.getchar()) != EOF;) {
str += c; str += c;
} }