libcubescript/src/lib_str.cc

246 lines
8.6 KiB
C++
Raw Permalink Normal View History

2021-05-03 00:39:00 +02:00
#include <cstdlib>
2017-02-08 01:07:35 +01:00
#include <functional>
#include <iterator>
2016-08-17 19:20:19 +02:00
2017-06-20 21:21:39 +02:00
#include <cubescript/cubescript.hh>
2021-03-23 01:11:21 +01:00
#include "cs_std.hh"
2021-03-23 01:25:47 +01:00
#include "cs_strman.hh"
#include "cs_thread.hh"
2021-03-23 23:32:25 +01:00
namespace cubescript {
2016-08-17 19:20:19 +02:00
template<typename F>
2021-03-23 23:29:32 +01:00
static inline void str_cmp_by(
state &cs, span_type<any_value> args, any_value &res, F cfunc
2021-03-20 05:41:25 +01:00
) {
2016-08-17 19:20:19 +02:00
bool val;
if (args.size() >= 2) {
val = cfunc(args[0].get_string(cs), args[1].get_string(cs));
2017-01-25 02:09:50 +01:00
for (size_t i = 2; (i < args.size()) && val; ++i) {
val = cfunc(args[i - 1].get_string(cs), args[i].get_string(cs));
2016-08-17 19:20:19 +02:00
}
} else {
2016-08-17 19:21:24 +02:00
val = cfunc(
!args.empty() ? args[0].get_string(cs) : std::string_view(),
std::string_view()
2016-08-17 19:21:24 +02:00
);
2016-08-17 19:20:19 +02:00
}
res.set_integer(integer_type(val));
}
2016-08-17 19:20:19 +02:00
LIBCUBESCRIPT_EXPORT void std_init_string(state &cs) {
new_cmd_quiet(cs, "strstr", "ss", [](auto &ccs, auto args, auto &res) {
std::string_view a = args[0].get_string(ccs);
std::string_view b = args[1].get_string(ccs);
auto pos = a.find(b);
if (pos == a.npos) {
res.set_integer(-1);
} else {
res.set_integer(integer_type(pos));
}
});
new_cmd_quiet(cs, "strlen", "s", [](auto &ccs, auto args, auto &res) {
res.set_integer(integer_type(args[0].get_string(ccs).size()));
});
new_cmd_quiet(cs, "strcode", "si", [](auto &ccs, auto args, auto &res) {
std::string_view str = args[0].get_string(ccs);
integer_type i = args[1].get_integer();
2021-03-23 23:29:32 +01:00
if (i >= integer_type(str.size())) {
res.set_integer(0);
} else {
res.set_integer(static_cast<unsigned char>(str[i]));
}
});
new_cmd_quiet(cs, "codestr", "i", [](auto &ccs, auto args, auto &res) {
char const p[2] = { char(args[0].get_integer()), '\0' };
res.set_string(std::string_view{static_cast<char const *>(p)}, ccs);
});
new_cmd_quiet(cs, "strlower", "s", [](auto &ccs, auto args, auto &res) {
auto inps = args[0].get_string(ccs);
auto *ics = state_p{ccs}.ts().istate;
2021-03-19 01:31:34 +01:00
auto *buf = ics->strman->alloc_buf(inps.size());
for (std::size_t i = 0; i < inps.size(); ++i) {
buf[i] = char(tolower(inps.data()[i]));
2016-08-17 19:20:19 +02:00
}
res.set_string(ics->strman->steal(buf));
});
new_cmd_quiet(cs, "strupper", "s", [](auto &ccs, auto args, auto &res) {
auto inps = args[0].get_string(ccs);
auto *ics = state_p{ccs}.ts().istate;
2021-03-19 01:31:34 +01:00
auto *buf = ics->strman->alloc_buf(inps.size());
for (std::size_t i = 0; i < inps.size(); ++i) {
buf[i] = char(toupper(inps.data()[i]));
2016-08-17 19:20:19 +02:00
}
res.set_string(ics->strman->steal(buf));
});
new_cmd_quiet(cs, "escape", "s", [](auto &ccs, auto args, auto &res) {
2021-03-23 23:29:32 +01:00
charbuf s{ccs};
escape_string(std::back_inserter(s), args[0].get_string(ccs));
res.set_string(s.str(), ccs);
});
new_cmd_quiet(cs, "unescape", "s", [](auto &ccs, auto args, auto &res) {
2021-03-23 23:29:32 +01:00
charbuf s{ccs};
unescape_string(std::back_inserter(s), args[0].get_string(ccs));
res.set_string(s.str(), ccs);
});
new_cmd_quiet(cs, "concat", "...", [](auto &ccs, auto args, auto &res) {
res.set_string(concat_values(ccs, args, " "));
});
new_cmd_quiet(cs, "concatword", "...", [](auto &ccs, auto args, auto &res) {
res.set_string(concat_values(ccs, args));
});
new_cmd_quiet(cs, "format", "...", [](auto &ccs, auto args, auto &res) {
2016-08-17 19:20:19 +02:00
if (args.empty()) {
return;
2016-08-17 19:20:19 +02:00
}
2021-03-23 23:29:32 +01:00
charbuf s{ccs};
string_ref fs = args[0].get_string(ccs);
std::string_view f{fs};
for (auto it = f.begin(); it != f.end(); ++it) {
char c = *it;
++it;
if ((c == '%') && (it != f.end())) {
char ic = *it;
++it;
if ((ic >= '1') && (ic <= '9')) {
int i = ic - '0';
if (std::size_t(i) < args.size()) {
s.append(args[i].get_string(ccs));
2016-09-15 23:04:32 +02:00
}
2016-08-17 19:20:19 +02:00
} else {
2021-03-19 02:50:48 +01:00
s.push_back(ic);
2016-08-17 19:20:19 +02:00
}
} else {
2021-03-19 02:50:48 +01:00
s.push_back(c);
2016-08-17 19:20:19 +02:00
}
}
res.set_string(s.str(), ccs);
});
new_cmd_quiet(cs, "tohex", "ii", [](auto &ccs, auto args, auto &res) {
2021-03-20 06:52:10 +01:00
char buf[32];
/* use long long as the largest signed integer type */
auto val = static_cast<long long>(args[0].get_integer());
int prec = std::max(int(args[1].get_integer()), 1);
2021-03-20 06:52:10 +01:00
int n = snprintf(buf, sizeof(buf), "0x%.*llX", prec, val);
if (n >= int(sizeof(buf))) {
2021-03-23 23:29:32 +01:00
charbuf s{ccs};
2021-03-20 06:52:10 +01:00
s.reserve(n + 1);
s.data()[0] = '\0';
int nn = snprintf(s.data(), n + 1, "0x%.*llX", prec, val);
if ((nn > 0) && (nn <= n)) {
res.set_string(
std::string_view{s.data(), std::size_t(nn)}, ccs
);
2021-03-20 06:52:10 +01:00
return;
}
} else if (n > 0) {
res.set_string(static_cast<char const *>(buf), ccs);
2021-03-20 06:52:10 +01:00
return;
}
2021-05-03 00:39:00 +02:00
/* should be unreachable */
abort();
});
2021-04-29 04:15:16 +02:00
new_cmd_quiet(cs, "substr", "sii#", [](auto &ccs, auto args, auto &res) {
std::string_view s = args[0].get_string(ccs);
auto start = args[1].get_integer(), count = args[2].get_integer();
auto numargs = args[3].get_integer();
auto len = integer_type(s.size());
auto offset = std::clamp(start, integer_type(0), len);
res.set_string(std::string_view{
&s[offset],
((numargs >= 3)
2021-03-23 23:29:32 +01:00
? size_t(std::clamp(count, integer_type(0), len - offset))
: size_t(len - offset))
}, ccs);
});
new_cmd_quiet(cs, "strcmp", "s1...", [](auto &ccs, auto args, auto &res) {
str_cmp_by(ccs, args, res, std::equal_to<std::string_view>());
2016-08-17 19:20:19 +02:00
});
new_cmd_quiet(cs, "=s", "s1...", [](auto &ccs, auto args, auto &res) {
str_cmp_by(ccs, args, res, std::equal_to<std::string_view>());
2016-08-17 19:20:19 +02:00
});
new_cmd_quiet(cs, "!=s", "s1...", [](auto &ccs, auto args, auto &res) {
str_cmp_by(ccs, args, res, std::not_equal_to<std::string_view>());
2016-08-17 19:20:19 +02:00
});
new_cmd_quiet(cs, "<s", "s1...", [](auto &ccs, auto args, auto &res) {
str_cmp_by(ccs, args, res, std::less<std::string_view>());
2016-08-17 19:20:19 +02:00
});
new_cmd_quiet(cs, ">s", "s1...", [](auto &ccs, auto args, auto &res) {
str_cmp_by(ccs, args, res, std::greater<std::string_view>());
2016-08-17 19:20:19 +02:00
});
new_cmd_quiet(cs, "<=s", "s1...", [](auto &ccs, auto args, auto &res) {
str_cmp_by(ccs, args, res, std::less_equal<std::string_view>());
2016-08-17 19:20:19 +02:00
});
new_cmd_quiet(cs, ">=s", "s1...", [](auto &ccs, auto args, auto &res) {
str_cmp_by(ccs, args, res, std::greater_equal<std::string_view>());
2016-08-17 19:20:19 +02:00
});
new_cmd_quiet(cs, "strreplace", "ssss", [](
auto &ccs, auto args, auto &res
) {
std::string_view s = args[0].get_string(ccs);
std::string_view oldval = args[1].get_string(ccs),
newval = args[2].get_string(ccs),
newval2 = args[3].get_string(ccs);
if (newval2.empty()) {
newval2 = newval;
}
if (oldval.empty()) {
res.set_string(s, ccs);
return;
}
2021-03-23 23:29:32 +01:00
charbuf buf{ccs};
2017-01-25 02:09:50 +01:00
for (size_t i = 0;; ++i) {
auto p = s.find(oldval);
if (p == s.npos) {
2021-03-19 02:50:48 +01:00
buf.append(s);
res.set_string(s, ccs);
return;
}
buf.append(s.substr(0, p));
buf.append((i & 1) ? newval2 : newval);
buf.append(s.substr(
p + oldval.size(),
s.size() - p - oldval.size()
));
}
});
new_cmd_quiet(cs, "strsplice", "ssii", [](
auto &ccs, auto args, auto &res
) {
std::string_view s = args[0].get_string(ccs);
std::string_view vals = args[1].get_string(ccs);
integer_type skip = args[2].get_integer(),
count = args[3].get_integer();
2021-03-23 23:29:32 +01:00
integer_type offset = std::clamp(skip, integer_type(0), integer_type(s.size())),
len = std::clamp(count, integer_type(0), integer_type(s.size()) - offset);
charbuf p{ccs};
2016-09-15 23:04:32 +02:00
p.reserve(s.size() - len + vals.size());
2016-08-17 19:20:19 +02:00
if (offset) {
p.append(s.substr(0, offset));
2016-08-17 19:20:19 +02:00
}
2021-03-19 02:50:48 +01:00
p.append(vals);
2021-03-23 23:29:32 +01:00
if ((offset + len) < integer_type(s.size())) {
p.append(s.substr(offset + len, s.size() - offset - len));
2016-08-17 19:20:19 +02:00
}
res.set_string(p.str(), ccs);
});
}
2021-03-23 23:32:25 +01:00
} /* namespace cubescript */