string lib cleanup
parent
caf30eaa03
commit
e0739608f1
152
lib_str.cc
152
lib_str.cc
|
@ -1,9 +1,25 @@
|
|||
#include <ostd/functional.hh>
|
||||
|
||||
#include "cubescript.hh"
|
||||
|
||||
namespace cscript {
|
||||
|
||||
template<typename F>
|
||||
static inline void cs_strgcmp(TvalRange args, TaggedValue &res, F cfunc) {
|
||||
bool val;
|
||||
if (args.size() >= 2) {
|
||||
val = cfunc(args[0].get_strr(), args[1].get_strr());
|
||||
for (ostd::Size i = 2; (i < args.size()) && val; ++i) {
|
||||
val = cfunc(args[i - 1].get_strr(), args[i].get_strr());
|
||||
}
|
||||
} else {
|
||||
val = cfunc(!args.empty() ? args[0].get_strr() : ostd::ConstCharRange(), ostd::ConstCharRange());
|
||||
}
|
||||
res.set_int(CsInt(val));
|
||||
};
|
||||
|
||||
void cs_init_lib_string(CsState &cs) {
|
||||
cs.add_command("strstr", "ss", [&cs](TvalRange args, TaggedValue &res) {
|
||||
cs.add_command("strstr", "ss", [](TvalRange args, TaggedValue &res) {
|
||||
ostd::ConstCharRange a = args[0].get_strr(), b = args[1].get_strr();
|
||||
ostd::ConstCharRange s = a;
|
||||
for (CsInt i = 0; b.size() <= s.size(); ++i) {
|
||||
|
@ -11,16 +27,16 @@ void cs_init_lib_string(CsState &cs) {
|
|||
res.set_int(i);
|
||||
return;
|
||||
}
|
||||
s.pop_front();
|
||||
++s;
|
||||
}
|
||||
res.set_int(-1);
|
||||
});
|
||||
|
||||
cs.add_command("strlen", "s", [&cs](TvalRange args, TaggedValue &res) {
|
||||
cs.add_command("strlen", "s", [](TvalRange args, TaggedValue &res) {
|
||||
res.set_int(CsInt(args[0].get_strr().size()));
|
||||
});
|
||||
|
||||
cs.add_command("strcode", "si", [&cs](TvalRange args, TaggedValue &res) {
|
||||
cs.add_command("strcode", "si", [](TvalRange args, TaggedValue &res) {
|
||||
ostd::ConstCharRange str = args[0].get_strr();
|
||||
CsInt i = args[1].get_int();
|
||||
if (i >= CsInt(str.size())) {
|
||||
|
@ -30,39 +46,41 @@ void cs_init_lib_string(CsState &cs) {
|
|||
}
|
||||
});
|
||||
|
||||
cs.add_command("codestr", "i", [&cs](TvalRange args, TaggedValue &res) {
|
||||
cs.add_command("codestr", "i", [](TvalRange args, TaggedValue &res) {
|
||||
char *s = new char[2];
|
||||
s[0] = char(args[0].get_int());
|
||||
s[1] = '\0';
|
||||
res.set_mstr(s);
|
||||
});
|
||||
|
||||
cs.add_command("strlower", "s", [&cs](TvalRange args, TaggedValue &res) {
|
||||
cs.add_command("strlower", "s", [](TvalRange args, TaggedValue &res) {
|
||||
ostd::ConstCharRange s = args[0].get_strr();
|
||||
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]);
|
||||
}
|
||||
buf[s.size()] = '\0';
|
||||
res.set_mstr(ostd::CharRange(buf, s.size()));
|
||||
});
|
||||
|
||||
cs.add_command("strupper", "s", [&cs](TvalRange args, TaggedValue &res) {
|
||||
cs.add_command("strupper", "s", [](TvalRange args, TaggedValue &res) {
|
||||
ostd::ConstCharRange s = args[0].get_strr();
|
||||
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]);
|
||||
}
|
||||
buf[s.size()] = '\0';
|
||||
res.set_mstr(ostd::CharRange(buf, s.size()));
|
||||
});
|
||||
|
||||
cs.add_command("escape", "s", [&cs](TvalRange args, TaggedValue &res) {
|
||||
cs.add_command("escape", "s", [](TvalRange args, TaggedValue &res) {
|
||||
auto x = ostd::appender<ostd::String>();
|
||||
util::escape_string(x, args[0].get_strr());
|
||||
ostd::Size len = x.size();
|
||||
res.set_mstr(ostd::CharRange(x.get().disown(), len));
|
||||
});
|
||||
|
||||
cs.add_command("unescape", "s", [&cs](TvalRange args, TaggedValue &res) {
|
||||
cs.add_command("unescape", "s", [](TvalRange args, TaggedValue &res) {
|
||||
ostd::ConstCharRange s = args[0].get_strr();
|
||||
char *buf = new char[s.size() + 1];
|
||||
auto writer = ostd::CharRange(buf, s.size() + 1);
|
||||
|
@ -71,88 +89,97 @@ void cs_init_lib_string(CsState &cs) {
|
|||
res.set_mstr(ostd::CharRange(buf, s.size()));
|
||||
});
|
||||
|
||||
cs.add_command("concat", "V", [&cs](TvalRange args, TaggedValue &res) {
|
||||
cs.add_command("concat", "V", [](TvalRange args, TaggedValue &res) {
|
||||
auto s = ostd::appender<ostd::String>();
|
||||
cscript::util::tvals_concat(s, args, " ");
|
||||
res.set_mstr(s.get().iter());
|
||||
s.get().disown();
|
||||
});
|
||||
|
||||
cs.add_command("concatword", "V", [&cs](TvalRange args, TaggedValue &res) {
|
||||
cs.add_command("concatword", "V", [](TvalRange args, TaggedValue &res) {
|
||||
auto s = ostd::appender<ostd::String>();
|
||||
cscript::util::tvals_concat(s, args);
|
||||
res.set_mstr(s.get().iter());
|
||||
s.get().disown();
|
||||
});
|
||||
|
||||
cs.add_command("format", "V", [&cs](TvalRange args, TaggedValue &res) {
|
||||
if (args.empty())
|
||||
cs.add_command("format", "V", [](TvalRange args, TaggedValue &res) {
|
||||
if (args.empty()) {
|
||||
return;
|
||||
}
|
||||
ostd::Vector<char> s;
|
||||
ostd::String fs = ostd::move(args[0].get_str());
|
||||
ostd::ConstCharRange f = fs.iter();
|
||||
while (!f.empty()) {
|
||||
char c = f.front();
|
||||
f.pop_front();
|
||||
char c = *f;
|
||||
++f;
|
||||
if ((c == '%') && !f.empty()) {
|
||||
char ic = f.front();
|
||||
f.pop_front();
|
||||
char ic = *f;
|
||||
++f;
|
||||
if (ic >= '1' && ic <= '9') {
|
||||
int i = ic - '0';
|
||||
ostd::String sub = ostd::move((ostd::Size(i) < args.size())
|
||||
? args[i].get_str() : ostd::String(""));
|
||||
ostd::String sub = ostd::move(
|
||||
(ostd::Size(i) < args.size())
|
||||
? args[i].get_str()
|
||||
: ostd::String("")
|
||||
);
|
||||
s.push_n(sub.data(), sub.size());
|
||||
} else s.push(ic);
|
||||
} else s.push(c);
|
||||
} else {
|
||||
s.push(ic);
|
||||
}
|
||||
} else {
|
||||
s.push(c);
|
||||
}
|
||||
}
|
||||
s.push('\0');
|
||||
ostd::Size len = s.size() - 1;
|
||||
res.set_mstr(ostd::CharRange(s.disown(), len));
|
||||
});
|
||||
|
||||
cs.add_command("tohex", "ii", [&cs](TvalRange args, TaggedValue &res) {
|
||||
cs.add_command("tohex", "ii", [](TvalRange args, TaggedValue &res) {
|
||||
auto r = ostd::appender<ostd::Vector<char>>();
|
||||
ostd::format(r, "0x%.*X", ostd::max(args[1].get_int(), 1), args[0].get_int());
|
||||
ostd::format(
|
||||
r, "0x%.*X", ostd::max(args[1].get_int(), 1), args[0].get_int()
|
||||
);
|
||||
r.put('\0');
|
||||
ostd::Size len = r.size() - 1;
|
||||
res.set_mstr(ostd::CharRange(r.get().disown(), len));
|
||||
});
|
||||
|
||||
cs.add_command("substr", "siiN", [&cs](TvalRange args, TaggedValue &res) {
|
||||
cs.add_command("substr", "siiN", [](TvalRange args, TaggedValue &res) {
|
||||
ostd::ConstCharRange s = args[0].get_strr();
|
||||
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, 0, len);
|
||||
res.set_str(ostd::ConstCharRange(
|
||||
&s[offset],
|
||||
(numargs >= 3) ? ostd::clamp(count, 0, len - offset)
|
||||
: (len - offset)
|
||||
(numargs >= 3) ? ostd::clamp(count, 0, len - offset) : (len - offset)
|
||||
));
|
||||
});
|
||||
|
||||
#define CS_CMD_CMPS(name, op) \
|
||||
cs.add_command(#name, "s1V", [&cs](TvalRange args, TaggedValue &res) { \
|
||||
bool val; \
|
||||
if (args.size() >= 2) { \
|
||||
val = strcmp(args[0].s, args[1].s) op 0; \
|
||||
for (ostd::Size i = 2; i < args.size() && val; ++i) \
|
||||
val = strcmp(args[i-1].s, args[i].s) op 0; \
|
||||
} else \
|
||||
val = (!args.empty() ? args[0].s[0] : 0) op 0; \
|
||||
res.set_int(CsInt(val)); \
|
||||
})
|
||||
cs.add_command("strcmp", "s1V", [](TvalRange args, TaggedValue &res) {
|
||||
cs_strgcmp(args, res, ostd::Equal<ostd::ConstCharRange>());
|
||||
});
|
||||
cs.add_command("=s", "s1V", [](TvalRange args, TaggedValue &res) {
|
||||
cs_strgcmp(args, res, ostd::Equal<ostd::ConstCharRange>());
|
||||
});
|
||||
cs.add_command("!=s", "s1V", [](TvalRange args, TaggedValue &res) {
|
||||
cs_strgcmp(args, res, ostd::NotEqual<ostd::ConstCharRange>());
|
||||
});
|
||||
cs.add_command("<s", "s1V", [](TvalRange args, TaggedValue &res) {
|
||||
cs_strgcmp(args, res, ostd::Less<ostd::ConstCharRange>());
|
||||
});
|
||||
cs.add_command(">s", "s1V", [](TvalRange args, TaggedValue &res) {
|
||||
cs_strgcmp(args, res, ostd::Greater<ostd::ConstCharRange>());
|
||||
});
|
||||
cs.add_command("<=s", "s1V", [](TvalRange args, TaggedValue &res) {
|
||||
cs_strgcmp(args, res, ostd::LessEqual<ostd::ConstCharRange>());
|
||||
});
|
||||
cs.add_command(">=s", "s1V", [](TvalRange args, TaggedValue &res) {
|
||||
cs_strgcmp(args, res, ostd::GreaterEqual<ostd::ConstCharRange>());
|
||||
});
|
||||
|
||||
CS_CMD_CMPS(strcmp, ==);
|
||||
CS_CMD_CMPS(=s, ==);
|
||||
CS_CMD_CMPS(!=s, !=);
|
||||
CS_CMD_CMPS(<s, <);
|
||||
CS_CMD_CMPS(>s, >);
|
||||
CS_CMD_CMPS(<=s, <=);
|
||||
CS_CMD_CMPS(>=s, >=);
|
||||
|
||||
#undef CS_CMD_CMPS
|
||||
|
||||
cs.add_command("strreplace", "ssss", [&cs](TvalRange args, TaggedValue &res) {
|
||||
cs.add_command("strreplace", "ssss", [](TvalRange args, TaggedValue &res) {
|
||||
ostd::ConstCharRange s = args[0].get_strr();
|
||||
ostd::ConstCharRange oldval = args[1].get_strr(),
|
||||
newval = args[2].get_strr(),
|
||||
|
@ -168,7 +195,7 @@ void cs_init_lib_string(CsState &cs) {
|
|||
for (ostd::Size i = 0;; ++i) {
|
||||
ostd::ConstCharRange found;
|
||||
ostd::ConstCharRange trys = s;
|
||||
for (; oldval.size() <= trys.size(); trys.pop_front()) {
|
||||
for (; oldval.size() <= trys.size(); ++trys) {
|
||||
if (trys.slice(0, oldval.size()) == oldval) {
|
||||
found = trys;
|
||||
break;
|
||||
|
@ -176,17 +203,17 @@ void cs_init_lib_string(CsState &cs) {
|
|||
}
|
||||
if (!found.empty()) {
|
||||
auto bef = ostd::slice_until(s, found);
|
||||
for (; !bef.empty(); bef.pop_front()) {
|
||||
buf.push(bef.front());
|
||||
for (; !bef.empty(); ++bef) {
|
||||
buf.push(*bef);
|
||||
}
|
||||
auto use = (i & 1) ? newval2 : newval;
|
||||
for (; !use.empty(); use.pop_front()) {
|
||||
buf.push(use.front());
|
||||
for (; !use.empty(); ++use) {
|
||||
buf.push(*use);
|
||||
}
|
||||
s = found + oldval.size();
|
||||
} else {
|
||||
for (; !s.empty(); s.pop_front()) {
|
||||
buf.push(s.front());
|
||||
for (; !s.empty(); ++s) {
|
||||
buf.push(*s);
|
||||
}
|
||||
buf.push('\0');
|
||||
ostd::Size len = buf.size() - 1;
|
||||
|
@ -196,7 +223,7 @@ void cs_init_lib_string(CsState &cs) {
|
|||
}
|
||||
});
|
||||
|
||||
cs.add_command("strsplice", "ssii", [&cs](TvalRange args, TaggedValue &res) {
|
||||
cs.add_command("strsplice", "ssii", [](TvalRange args, TaggedValue &res) {
|
||||
ostd::ConstCharRange s = args[0].get_strr();
|
||||
ostd::ConstCharRange vals = args[1].get_strr();
|
||||
CsInt skip = args[2].get_int(),
|
||||
|
@ -206,12 +233,15 @@ void cs_init_lib_string(CsState &cs) {
|
|||
CsInt offset = ostd::clamp(skip, 0, slen),
|
||||
len = ostd::clamp(count, 0, slen - offset);
|
||||
char *p = new char[slen - len + vlen + 1];
|
||||
if (offset)
|
||||
if (offset) {
|
||||
memcpy(p, s.data(), offset);
|
||||
if (vlen)
|
||||
}
|
||||
if (vlen) {
|
||||
memcpy(&p[offset], vals.data(), vlen);
|
||||
if (offset + len < slen)
|
||||
}
|
||||
if (offset + len < slen) {
|
||||
memcpy(&p[offset + vlen], &s[offset + len], slen - (offset + len));
|
||||
}
|
||||
p[slen - len + vlen] = '\0';
|
||||
res.set_mstr(ostd::CharRange(p, slen - len + vlen));
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue