clean up string lib

master
Daniel Kolesa 2016-09-15 23:04:32 +02:00
parent 4bbe2ca6df
commit 1846fee1a0
2 changed files with 41 additions and 73 deletions

View File

@ -54,7 +54,7 @@ static void cs_error_line(
throw CsErrorException(gs.cs, rfmt, ostd::forward<A>(args)...); throw CsErrorException(gs.cs, rfmt, ostd::forward<A>(args)...);
} }
char *cs_dup_ostr(ostd::ConstCharRange s) { static char *cs_dup_ostr(ostd::ConstCharRange s) {
char *r = new char[s.size() + 1]; char *r = new char[s.size() + 1];
if (s.data()) { if (s.data()) {
memcpy(r, s.data(), s.size()); memcpy(r, s.data(), s.size());

View File

@ -50,68 +50,55 @@ void cs_init_lib_string(CsState &cs) {
}); });
cs.new_command("codestr", "i", [](auto &, auto args, auto &res) { cs.new_command("codestr", "i", [](auto &, auto args, auto &res) {
char *s = new char[2]; res.set_str(CsString(1, char(args[0].get_int())));
s[0] = char(args[0].get_int());
s[1] = '\0';
res.set_mstr(s);
}); });
cs.new_command("strlower", "s", [](auto &, auto args, auto &res) { cs.new_command("strlower", "s", [](auto &, auto args, auto &res) {
ostd::ConstCharRange s = args[0].get_strr(); CsString s = args[0].get_str();
char *buf = new char[s.size() + 1];
for (auto i: ostd::range(s.size())) { for (auto i: ostd::range(s.size())) {
buf[i] = tolower(s[i]); s[i] = tolower(s[i]);
} }
buf[s.size()] = '\0'; res.set_str(ostd::move(s));
res.set_mstr(ostd::CharRange(buf, s.size()));
}); });
cs.new_command("strupper", "s", [](auto &, auto args, auto &res) { cs.new_command("strupper", "s", [](auto &, auto args, auto &res) {
ostd::ConstCharRange s = args[0].get_strr(); CsString s = args[0].get_str();
char *buf = new char[s.size() + 1];
for (auto i: ostd::range(s.size())) { for (auto i: ostd::range(s.size())) {
buf[i] = toupper(s[i]); s[i] = toupper(s[i]);
} }
buf[s.size()] = '\0'; res.set_str(ostd::move(s));
res.set_mstr(ostd::CharRange(buf, s.size()));
}); });
cs.new_command("escape", "s", [](auto &, auto args, auto &res) { cs.new_command("escape", "s", [](auto &, auto args, auto &res) {
auto x = ostd::appender<CsString>(); auto s = ostd::appender<CsString>();
util::escape_string(x, args[0].get_strr()); util::escape_string(s, args[0].get_strr());
ostd::Size len = x.size(); res.set_str(ostd::move(s.get()));
res.set_mstr(ostd::CharRange(x.get().disown(), len));
}); });
cs.new_command("unescape", "s", [](auto &, auto args, auto &res) { cs.new_command("unescape", "s", [](auto &, auto args, auto &res) {
ostd::ConstCharRange s = args[0].get_strr(); auto s = ostd::appender<CsString>();
char *buf = new char[s.size() + 1]; util::unescape_string(s, args[0].get_strr());
auto writer = ostd::CharRange(buf, s.size() + 1); res.set_str(ostd::move(s.get()));
util::unescape_string(writer, s);
writer.put('\0');
res.set_mstr(ostd::CharRange(buf, s.size()));
}); });
cs.new_command("concat", "V", [](auto &, auto args, auto &res) { cs.new_command("concat", "V", [](auto &, auto args, auto &res) {
auto s = ostd::appender<CsString>(); auto s = ostd::appender<CsString>();
cscript::util::tvals_concat(s, args, " "); cscript::util::tvals_concat(s, args, " ");
res.set_mstr(s.get().iter()); res.set_str(ostd::move(s.get()));
s.get().disown();
}); });
cs.new_command("concatword", "V", [](auto &, auto args, auto &res) { cs.new_command("concatword", "V", [](auto &, auto args, auto &res) {
auto s = ostd::appender<CsString>(); auto s = ostd::appender<CsString>();
cscript::util::tvals_concat(s, args); cscript::util::tvals_concat(s, args);
res.set_mstr(s.get().iter()); res.set_str(ostd::move(s.get()));
s.get().disown();
}); });
cs.new_command("format", "V", [](auto &, auto args, auto &res) { cs.new_command("format", "V", [](auto &, auto args, auto &res) {
if (args.empty()) { if (args.empty()) {
return; return;
} }
CsVector<char> s; CsString s;
CsString fs = ostd::move(args[0].get_str()); CsString fs = args[0].get_str();
ostd::ConstCharRange f = fs.iter(); ostd::ConstCharRange f = fs.iter();
while (!f.empty()) { while (!f.empty()) {
char c = *f; char c = *f;
@ -121,33 +108,26 @@ void cs_init_lib_string(CsState &cs) {
++f; ++f;
if (ic >= '1' && ic <= '9') { if (ic >= '1' && ic <= '9') {
int i = ic - '0'; int i = ic - '0';
CsString sub = ostd::move( if (ostd::Size(i) < args.size()) {
(ostd::Size(i) < args.size()) s += args[i].get_str();
? args[i].get_str() }
: CsString("")
);
s.push_n(sub.data(), sub.size());
} else { } else {
s.push(ic); s += ic;
} }
} else { } else {
s.push(c); s += c;
} }
} }
s.push('\0'); res.set_str(ostd::move(s));
ostd::Size len = s.size() - 1;
res.set_mstr(ostd::CharRange(s.disown(), len));
}); });
cs.new_command("tohex", "ii", [](auto &, auto args, auto &res) { cs.new_command("tohex", "ii", [](auto &, auto args, auto &res) {
auto r = ostd::appender<CsVector<char>>(); auto r = ostd::appender<CsString>();
ostd::format( ostd::format(
r, "0x%.*X", ostd::max(args[1].get_int(), CsInt(1)), r, "0x%.*X", ostd::max(args[1].get_int(), CsInt(1)),
args[0].get_int() args[0].get_int()
); );
r.put('\0'); res.set_str(ostd::move(r.get()));
ostd::Size len = r.size() - 1;
res.set_mstr(ostd::CharRange(r.get().disown(), len));
}); });
cs.new_command("substr", "siiN", [](auto &, auto args, auto &res) { cs.new_command("substr", "siiN", [](auto &, auto args, auto &res) {
@ -193,7 +173,7 @@ void cs_init_lib_string(CsState &cs) {
if (newval2.empty()) { if (newval2.empty()) {
newval2 = newval; newval2 = newval;
} }
CsVector<char> buf; CsString buf;
if (!oldval.size()) { if (!oldval.size()) {
res.set_str(s); res.set_str(s);
return; return;
@ -208,22 +188,12 @@ void cs_init_lib_string(CsState &cs) {
} }
} }
if (!found.empty()) { if (!found.empty()) {
auto bef = ostd::slice_until(s, found); buf += ostd::slice_until(s, found);
for (; !bef.empty(); ++bef) { buf += (i & 1) ? newval2 : newval;
buf.push(*bef);
}
auto use = (i & 1) ? newval2 : newval;
for (; !use.empty(); ++use) {
buf.push(*use);
}
s = found + oldval.size(); s = found + oldval.size();
} else { } else {
for (; !s.empty(); ++s) { buf += s;
buf.push(*s); res.set_str(ostd::move(buf));
}
buf.push('\0');
ostd::Size len = buf.size() - 1;
res.set_mstr(ostd::CharRange(buf.disown(), len));
return; return;
} }
} }
@ -234,22 +204,20 @@ void cs_init_lib_string(CsState &cs) {
ostd::ConstCharRange vals = args[1].get_strr(); ostd::ConstCharRange vals = args[1].get_strr();
CsInt skip = args[2].get_int(), CsInt skip = args[2].get_int(),
count = args[3].get_int(); count = args[3].get_int();
CsInt slen = CsInt(s.size()), CsInt offset = ostd::clamp(skip, CsInt(0), CsInt(s.size())),
vlen = CsInt(vals.size()); len = ostd::clamp(count, CsInt(0), CsInt(s.size()) - offset);
CsInt offset = ostd::clamp(skip, CsInt(0), slen), CsString p;
len = ostd::clamp(count, CsInt(0), slen - offset); p.reserve(s.size() - len + vals.size());
char *p = new char[slen - len + vlen + 1];
if (offset) { if (offset) {
memcpy(p, s.data(), offset); p += s.slice(0, offset);
} }
if (vlen) { if (!vals.empty()) {
memcpy(&p[offset], vals.data(), vlen); p += vals;
} }
if (offset + len < slen) { if ((offset + len) < CsInt(s.size())) {
memcpy(&p[offset + vlen], &s[offset + len], slen - (offset + len)); p += s.slice(offset + len, s.size());
} }
p[slen - len + vlen] = '\0'; res.set_str(ostd::move(p));
res.set_mstr(ostd::CharRange(p, slen - len + vlen));
}); });
} }