294 lines
11 KiB
C++
294 lines
11 KiB
C++
#include "engine/functions/helpers.hpp"
|
|
#include "engine/functions/registry.hpp"
|
|
#include "engine/workbook.hpp"
|
|
|
|
#include <cctype>
|
|
#include <cmath>
|
|
#include <variant>
|
|
|
|
namespace htab {
|
|
|
|
using namespace htab::fn;
|
|
|
|
namespace {
|
|
|
|
EvalResult fn_exact(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.size() != 2) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto a = eval_string_arg(args[0], ctx, err);
|
|
if (!a) return err_res(err);
|
|
auto b = eval_string_arg(args[1], ctx, err);
|
|
if (!b) return err_res(err);
|
|
return EvalResult::from_value(Value::make_bool(*a == *b));
|
|
}
|
|
|
|
EvalResult fn_len(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.size() != 1) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto s = eval_string_arg(args[0], ctx, err);
|
|
if (!s) return err_res(err);
|
|
return EvalResult::from_value(
|
|
Value::make_number(static_cast<double>(utf8_codepoints(*s).size())));
|
|
}
|
|
|
|
EvalResult fn_lower(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.size() != 1) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto s = eval_string_arg(args[0], ctx, err);
|
|
if (!s) return err_res(err);
|
|
return EvalResult::from_value(Value::make_string(to_lower_ascii(*s)));
|
|
}
|
|
|
|
EvalResult fn_upper(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.size() != 1) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto s = eval_string_arg(args[0], ctx, err);
|
|
if (!s) return err_res(err);
|
|
return EvalResult::from_value(Value::make_string(to_upper_ascii(*s)));
|
|
}
|
|
|
|
EvalResult fn_proper(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.size() != 1) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto s = eval_string_arg(args[0], ctx, err);
|
|
if (!s) return err_res(err);
|
|
std::string out = *s;
|
|
bool start_word = true;
|
|
for (char& ch : out) {
|
|
unsigned char uc = static_cast<unsigned char>(ch);
|
|
if (std::isalpha(uc)) {
|
|
ch = static_cast<char>(start_word ? std::toupper(uc) : std::tolower(uc));
|
|
start_word = false;
|
|
} else {
|
|
start_word = true;
|
|
}
|
|
}
|
|
return EvalResult::from_value(Value::make_string(std::move(out)));
|
|
}
|
|
|
|
EvalResult fn_trim(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.size() != 1) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto s = eval_string_arg(args[0], ctx, err);
|
|
if (!s) return err_res(err);
|
|
std::string out;
|
|
out.reserve(s->size());
|
|
bool in_space = true;
|
|
for (char ch : *s) {
|
|
if (ch == ' ') {
|
|
if (!in_space) {
|
|
out.push_back(' ');
|
|
in_space = true;
|
|
}
|
|
} else {
|
|
out.push_back(ch);
|
|
in_space = false;
|
|
}
|
|
}
|
|
if (!out.empty() && out.back() == ' ') out.pop_back();
|
|
return EvalResult::from_value(Value::make_string(std::move(out)));
|
|
}
|
|
|
|
EvalResult fn_left(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.empty() || args.size() > 2) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto s = eval_string_arg(args[0], ctx, err);
|
|
if (!s) return err_res(err);
|
|
int count = 1;
|
|
if (args.size() == 2) {
|
|
auto n = eval_number_arg(args[1], ctx, err);
|
|
if (!n) return err_res(err);
|
|
count = static_cast<int>(std::trunc(*n));
|
|
}
|
|
if (count < 0) return err_res(ErrorCode::Value);
|
|
auto cps = utf8_codepoints(*s);
|
|
return EvalResult::from_value(Value::make_string(utf8_join(cps, 0, static_cast<size_t>(count))));
|
|
}
|
|
|
|
EvalResult fn_right(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.empty() || args.size() > 2) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto s = eval_string_arg(args[0], ctx, err);
|
|
if (!s) return err_res(err);
|
|
int count = 1;
|
|
if (args.size() == 2) {
|
|
auto n = eval_number_arg(args[1], ctx, err);
|
|
if (!n) return err_res(err);
|
|
count = static_cast<int>(std::trunc(*n));
|
|
}
|
|
if (count < 0) return err_res(ErrorCode::Value);
|
|
auto cps = utf8_codepoints(*s);
|
|
size_t c = static_cast<size_t>(count);
|
|
size_t start = (c >= cps.size()) ? 0 : (cps.size() - c);
|
|
return EvalResult::from_value(Value::make_string(utf8_join(cps, start, c)));
|
|
}
|
|
|
|
EvalResult fn_mid(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.size() != 3) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto s = eval_string_arg(args[0], ctx, err);
|
|
if (!s) return err_res(err);
|
|
auto st = eval_number_arg(args[1], ctx, err);
|
|
if (!st) return err_res(err);
|
|
auto nc = eval_number_arg(args[2], ctx, err);
|
|
if (!nc) return err_res(err);
|
|
int start1 = static_cast<int>(std::trunc(*st));
|
|
int count = static_cast<int>(std::trunc(*nc));
|
|
if (start1 < 1 || count < 0) return err_res(ErrorCode::Value);
|
|
auto cps = utf8_codepoints(*s);
|
|
size_t start0 = static_cast<size_t>(start1 - 1);
|
|
if (start0 >= cps.size()) return EvalResult::from_value(Value::make_string(""));
|
|
return EvalResult::from_value(
|
|
Value::make_string(utf8_join(cps, start0, static_cast<size_t>(count))));
|
|
}
|
|
|
|
EvalResult fn_find(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.size() < 2 || args.size() > 3) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto needle = eval_string_arg(args[0], ctx, err);
|
|
if (!needle) return err_res(err);
|
|
auto hay = eval_string_arg(args[1], ctx, err);
|
|
if (!hay) return err_res(err);
|
|
int start1 = 1;
|
|
if (args.size() == 3) {
|
|
auto sn = eval_number_arg(args[2], ctx, err);
|
|
if (!sn) return err_res(err);
|
|
start1 = static_cast<int>(std::trunc(*sn));
|
|
}
|
|
auto hay_cps = utf8_codepoints(*hay);
|
|
auto ned_cps = utf8_codepoints(*needle);
|
|
if (start1 < 1 || static_cast<size_t>(start1) > hay_cps.size() + 1) {
|
|
return err_res(ErrorCode::Value);
|
|
}
|
|
if (ned_cps.empty()) {
|
|
return EvalResult::from_value(Value::make_number(static_cast<double>(start1)));
|
|
}
|
|
if (ned_cps.size() > hay_cps.size()) return err_res(ErrorCode::Value);
|
|
for (size_t i = static_cast<size_t>(start1 - 1); i + ned_cps.size() <= hay_cps.size(); ++i) {
|
|
bool match = true;
|
|
for (size_t j = 0; j < ned_cps.size(); ++j) {
|
|
if (hay_cps[i + j] != ned_cps[j]) {
|
|
match = false;
|
|
break;
|
|
}
|
|
}
|
|
if (match) {
|
|
return EvalResult::from_value(Value::make_number(static_cast<double>(i + 1)));
|
|
}
|
|
}
|
|
return err_res(ErrorCode::Value);
|
|
}
|
|
|
|
EvalResult fn_replace(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.size() != 4) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto old_t = eval_string_arg(args[0], ctx, err);
|
|
if (!old_t) return err_res(err);
|
|
auto st = eval_number_arg(args[1], ctx, err);
|
|
if (!st) return err_res(err);
|
|
auto nc = eval_number_arg(args[2], ctx, err);
|
|
if (!nc) return err_res(err);
|
|
auto new_t = eval_string_arg(args[3], ctx, err);
|
|
if (!new_t) return err_res(err);
|
|
|
|
int start1 = static_cast<int>(std::trunc(*st));
|
|
int count = static_cast<int>(std::trunc(*nc));
|
|
if (start1 < 1 || count < 0) return err_res(ErrorCode::Value);
|
|
|
|
auto cps = utf8_codepoints(*old_t);
|
|
size_t s0 = std::min(static_cast<size_t>(start1 - 1), cps.size());
|
|
size_t e0 = std::min(s0 + static_cast<size_t>(count), cps.size());
|
|
|
|
std::string out = utf8_join(cps, 0, s0);
|
|
out += *new_t;
|
|
out += utf8_join(cps, e0, cps.size() - e0);
|
|
return EvalResult::from_value(Value::make_string(std::move(out)));
|
|
}
|
|
|
|
EvalResult fn_rept(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.size() != 2) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto t = eval_string_arg(args[0], ctx, err);
|
|
if (!t) return err_res(err);
|
|
auto n = eval_number_arg(args[1], ctx, err);
|
|
if (!n) return err_res(err);
|
|
int times = static_cast<int>(std::trunc(*n));
|
|
if (times < 0) return err_res(ErrorCode::Value);
|
|
if (times == 0 || t->empty()) return EvalResult::from_value(Value::make_string(""));
|
|
if (t->size() * static_cast<size_t>(times) > 32767) return err_res(ErrorCode::Value);
|
|
|
|
std::string out;
|
|
out.reserve(t->size() * static_cast<size_t>(times));
|
|
for (int i = 0; i < times; ++i) out += *t;
|
|
return EvalResult::from_value(Value::make_string(std::move(out)));
|
|
}
|
|
|
|
EvalResult fn_substitute(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.size() < 3 || args.size() > 4) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto text = eval_string_arg(args[0], ctx, err);
|
|
if (!text) return err_res(err);
|
|
auto old_t = eval_string_arg(args[1], ctx, err);
|
|
if (!old_t) return err_res(err);
|
|
auto new_t = eval_string_arg(args[2], ctx, err);
|
|
if (!new_t) return err_res(err);
|
|
|
|
int instance_num = 0;
|
|
if (args.size() == 4) {
|
|
auto inst = eval_number_arg(args[3], ctx, err);
|
|
if (!inst) return err_res(err);
|
|
instance_num = static_cast<int>(std::trunc(*inst));
|
|
if (instance_num < 1) return err_res(ErrorCode::Value);
|
|
}
|
|
if (old_t->empty()) return EvalResult::from_value(Value::make_string(*text));
|
|
|
|
std::string out;
|
|
size_t pos = 0;
|
|
int seen = 0;
|
|
while (pos < text->size()) {
|
|
size_t found = text->find(*old_t, pos);
|
|
if (found == std::string::npos) {
|
|
out.append(*text, pos, text->size() - pos);
|
|
break;
|
|
}
|
|
++seen;
|
|
out.append(*text, pos, found - pos);
|
|
if (instance_num == 0 || seen == instance_num) {
|
|
out.append(*new_t);
|
|
} else {
|
|
out.append(*old_t);
|
|
}
|
|
pos = found + old_t->size();
|
|
}
|
|
return EvalResult::from_value(Value::make_string(std::move(out)));
|
|
}
|
|
|
|
EvalResult fn_t(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.size() != 1) return err_res(ErrorCode::Value);
|
|
Value v = deref_to_value(evaluate_ast(args[0].get(), ctx), *ctx.wb);
|
|
if (v.is_error()) return EvalResult::from_value(v);
|
|
if (v.is_string()) return EvalResult::from_value(v);
|
|
return EvalResult::from_value(Value::make_string(""));
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void register_text_functions(FunctionRegistry& reg) {
|
|
reg.register_fn("EXACT", fn_exact);
|
|
reg.register_fn("FIND", fn_find);
|
|
reg.register_fn("LEFT", fn_left);
|
|
reg.register_fn("LEN", fn_len);
|
|
reg.register_fn("LOWER", fn_lower);
|
|
reg.register_fn("MID", fn_mid);
|
|
reg.register_fn("PROPER", fn_proper);
|
|
reg.register_fn("REPLACE", fn_replace);
|
|
reg.register_fn("REPT", fn_rept);
|
|
reg.register_fn("RIGHT", fn_right);
|
|
reg.register_fn("SUBSTITUTE", fn_substitute);
|
|
reg.register_fn("T", fn_t);
|
|
reg.register_fn("TRIM", fn_trim);
|
|
reg.register_fn("UPPER", fn_upper);
|
|
}
|
|
|
|
} // namespace htab
|