388 lines
12 KiB
C++
388 lines
12 KiB
C++
#include "engine/functions/helpers.hpp"
|
|
#include "engine/workbook.hpp"
|
|
|
|
#include <cctype>
|
|
#include <cmath>
|
|
#include <cstdlib>
|
|
|
|
namespace htab::fn {
|
|
|
|
namespace {
|
|
|
|
std::optional<double> try_parse_simple_number(std::string_view sv) {
|
|
sv = trim_ascii(sv);
|
|
if (sv.empty()) return std::nullopt;
|
|
bool pct = false;
|
|
if (sv.back() == '%') {
|
|
pct = true;
|
|
sv.remove_suffix(1);
|
|
sv = trim_ascii(sv);
|
|
if (sv.empty()) return std::nullopt;
|
|
}
|
|
size_t i = 0;
|
|
if (sv[i] == '+' || sv[i] == '-') ++i;
|
|
bool has_digits = false;
|
|
while (i < sv.size() && std::isdigit(static_cast<unsigned char>(sv[i]))) {
|
|
has_digits = true;
|
|
++i;
|
|
}
|
|
if (i < sv.size() && sv[i] == '.') {
|
|
++i;
|
|
while (i < sv.size() && std::isdigit(static_cast<unsigned char>(sv[i]))) {
|
|
has_digits = true;
|
|
++i;
|
|
}
|
|
}
|
|
if (!has_digits) return std::nullopt;
|
|
if (i < sv.size() && (sv[i] == 'e' || sv[i] == 'E')) {
|
|
++i;
|
|
if (i < sv.size() && (sv[i] == '+' || sv[i] == '-')) ++i;
|
|
bool exp_d = false;
|
|
while (i < sv.size() && std::isdigit(static_cast<unsigned char>(sv[i]))) {
|
|
exp_d = true;
|
|
++i;
|
|
}
|
|
if (!exp_d) return std::nullopt;
|
|
}
|
|
if (i != sv.size()) return std::nullopt;
|
|
std::string tmp(sv);
|
|
char* endptr = nullptr;
|
|
double val = std::strtod(tmp.c_str(), &endptr);
|
|
if (endptr != tmp.c_str() + tmp.size() || !std::isfinite(val)) return std::nullopt;
|
|
if (pct) val /= 100.0;
|
|
return val == 0.0 ? 0.0 : val;
|
|
}
|
|
|
|
std::vector<std::string_view> parse_wildcard_tokens(std::string_view pat) {
|
|
auto raw_cps = utf8_codepoints(pat);
|
|
std::vector<std::string_view> out;
|
|
out.reserve(raw_cps.size());
|
|
for (size_t i = 0; i < raw_cps.size(); ++i) {
|
|
out.push_back(raw_cps[i]);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
struct PatAtom {
|
|
enum Kind { WildMany, WildOne, Literal };
|
|
Kind kind = Literal;
|
|
std::string_view cp;
|
|
};
|
|
|
|
std::vector<PatAtom> compile_wildcard(std::string_view pattern) {
|
|
auto cps = parse_wildcard_tokens(pattern);
|
|
std::vector<PatAtom> atoms;
|
|
atoms.reserve(cps.size());
|
|
for (size_t i = 0; i < cps.size(); ++i) {
|
|
if (cps[i] == "~" && i + 1 < cps.size()) {
|
|
atoms.push_back({PatAtom::Literal, cps[i + 1]});
|
|
++i;
|
|
} else if (cps[i] == "*") {
|
|
if (atoms.empty() || atoms.back().kind != PatAtom::WildMany) {
|
|
atoms.push_back({PatAtom::WildMany, {}});
|
|
}
|
|
} else if (cps[i] == "?") {
|
|
atoms.push_back({PatAtom::WildOne, {}});
|
|
} else {
|
|
atoms.push_back({PatAtom::Literal, cps[i]});
|
|
}
|
|
}
|
|
return atoms;
|
|
}
|
|
|
|
bool cp_iequals(std::string_view a, std::string_view b) {
|
|
if (a.size() == 1 && b.size() == 1) {
|
|
return std::tolower(static_cast<unsigned char>(a[0])) ==
|
|
std::tolower(static_cast<unsigned char>(b[0]));
|
|
}
|
|
return a == b;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
std::optional<Value> eval_scalar_arg(const std::shared_ptr<Expr>& expr,
|
|
const EvalContext& ctx) {
|
|
Value v = deref_to_value(evaluate_ast(expr.get(), ctx), *ctx.wb);
|
|
return v;
|
|
}
|
|
|
|
std::optional<double> eval_number_arg(const std::shared_ptr<Expr>& expr,
|
|
const EvalContext& ctx,
|
|
ErrorCode& err_out) {
|
|
Value v = deref_to_value(evaluate_ast(expr.get(), ctx), *ctx.wb);
|
|
auto n = to_number(v);
|
|
if (std::holds_alternative<ErrorCode>(n)) {
|
|
err_out = std::get<ErrorCode>(n);
|
|
return std::nullopt;
|
|
}
|
|
return std::get<double>(n);
|
|
}
|
|
|
|
std::optional<bool> eval_bool_arg(const std::shared_ptr<Expr>& expr,
|
|
const EvalContext& ctx,
|
|
ErrorCode& err_out) {
|
|
Value v = deref_to_value(evaluate_ast(expr.get(), ctx), *ctx.wb);
|
|
auto b = to_bool(v);
|
|
if (std::holds_alternative<ErrorCode>(b)) {
|
|
err_out = std::get<ErrorCode>(b);
|
|
return std::nullopt;
|
|
}
|
|
return std::get<bool>(b);
|
|
}
|
|
|
|
std::optional<std::string> eval_string_arg(const std::shared_ptr<Expr>& expr,
|
|
const EvalContext& ctx,
|
|
ErrorCode& err_out) {
|
|
Value v = deref_to_value(evaluate_ast(expr.get(), ctx), *ctx.wb);
|
|
auto s = to_string(v);
|
|
if (std::holds_alternative<ErrorCode>(s)) {
|
|
err_out = std::get<ErrorCode>(s);
|
|
return std::nullopt;
|
|
}
|
|
return std::get<std::string>(s);
|
|
}
|
|
|
|
std::optional<ErrorCode> collect_numeric_args(
|
|
const std::vector<std::shared_ptr<Expr>>& args,
|
|
const EvalContext& ctx,
|
|
std::vector<double>& out_nums) {
|
|
for (const auto& arg : args) {
|
|
EvalResult res = evaluate_ast(arg.get(), ctx);
|
|
if (res.is_ref) {
|
|
for (const auto& rng : res.ranges) {
|
|
for (int r = rng.r1; r <= rng.r2; ++r) {
|
|
for (int c = rng.c1; c <= rng.c2; ++c) {
|
|
Value v = ctx.wb->get_scalar_value(rng.sheet, r, c);
|
|
if (v.is_error()) return v.as_error();
|
|
if (v.is_number()) out_nums.push_back(v.as_number());
|
|
}
|
|
}
|
|
}
|
|
} else if (res.value.is_array()) {
|
|
const auto& arr = res.value.as_array();
|
|
if (arr) {
|
|
for (const auto& v : arr->data) {
|
|
if (v.is_error()) return v.as_error();
|
|
if (v.is_number()) out_nums.push_back(v.as_number());
|
|
}
|
|
}
|
|
} else {
|
|
const Value& v = res.value;
|
|
if (v.is_error()) return v.as_error();
|
|
if (v.is_empty()) continue;
|
|
auto num = to_number(v);
|
|
if (std::holds_alternative<ErrorCode>(num)) {
|
|
return std::get<ErrorCode>(num);
|
|
}
|
|
out_nums.push_back(std::get<double>(num));
|
|
}
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
std::variant<Array2D, ErrorCode> result_to_array2d(const EvalResult& res,
|
|
const Workbook& wb) {
|
|
if (res.is_ref) {
|
|
if (res.ranges.size() != 1) return ErrorCode::Value;
|
|
const CellRange& rng = res.ranges[0];
|
|
int rc = rng.row_count();
|
|
int cc = rng.col_count();
|
|
Array2D arr(rc, cc);
|
|
for (int r = 0; r < rc; ++r) {
|
|
for (int c = 0; c < cc; ++c) {
|
|
arr.at(r, c) = wb.get_scalar_value(rng.sheet, rng.r1 + r, rng.c1 + c);
|
|
}
|
|
}
|
|
return arr;
|
|
}
|
|
if (res.value.is_error()) return res.value.as_error();
|
|
if (res.value.is_array()) {
|
|
const auto& p = res.value.as_array();
|
|
if (!p) return ErrorCode::Value;
|
|
return *p;
|
|
}
|
|
Array2D single(1, 1, res.value);
|
|
return single;
|
|
}
|
|
|
|
std::vector<std::string_view> utf8_codepoints(std::string_view s) {
|
|
std::vector<std::string_view> cps;
|
|
size_t i = 0;
|
|
while (i < s.size()) {
|
|
unsigned char b = static_cast<unsigned char>(s[i]);
|
|
size_t len = 1;
|
|
if ((b & 0x80) == 0) len = 1;
|
|
else if ((b & 0xE0) == 0xC0) len = 2;
|
|
else if ((b & 0xF0) == 0xE0) len = 3;
|
|
else if ((b & 0xF8) == 0xF0) len = 4;
|
|
if (i + len > s.size()) len = 1;
|
|
cps.push_back(s.substr(i, len));
|
|
i += len;
|
|
}
|
|
return cps;
|
|
}
|
|
|
|
std::string utf8_join(const std::vector<std::string_view>& cps, size_t start, size_t count) {
|
|
std::string out;
|
|
size_t end = std::min(cps.size(), start + count);
|
|
for (size_t i = start; i < end; ++i) {
|
|
out.append(cps[i]);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
bool wildcard_match_ci(std::string_view pattern, std::string_view text) {
|
|
auto atoms = compile_wildcard(pattern);
|
|
auto text_cps = utf8_codepoints(text);
|
|
size_t m = atoms.size();
|
|
size_t n = text_cps.size();
|
|
|
|
std::vector<bool> prev(n + 1, false), curr(n + 1, false);
|
|
prev[0] = true;
|
|
for (size_t i = 0; i < m; ++i) {
|
|
std::fill(curr.begin(), curr.end(), false);
|
|
if (atoms[i].kind == PatAtom::WildMany) {
|
|
curr[0] = prev[0];
|
|
for (size_t j = 1; j <= n; ++j) {
|
|
curr[j] = prev[j] || curr[j - 1];
|
|
}
|
|
} else if (atoms[i].kind == PatAtom::WildOne) {
|
|
for (size_t j = 1; j <= n; ++j) {
|
|
curr[j] = prev[j - 1];
|
|
}
|
|
} else {
|
|
for (size_t j = 1; j <= n; ++j) {
|
|
if (prev[j - 1] && cp_iequals(atoms[i].cp, text_cps[j - 1])) {
|
|
curr[j] = true;
|
|
}
|
|
}
|
|
}
|
|
prev.swap(curr);
|
|
}
|
|
return prev[n];
|
|
}
|
|
|
|
bool matches_criteria(const Value& cell_val, const Value& criteria_val) {
|
|
if (criteria_val.is_error()) {
|
|
return cell_val.is_error() && cell_val.as_error() == criteria_val.as_error();
|
|
}
|
|
if (criteria_val.is_empty()) {
|
|
return cell_val.is_empty() ||
|
|
(cell_val.is_string() && cell_val.as_string().empty());
|
|
}
|
|
if (criteria_val.is_number()) {
|
|
if (cell_val.is_number()) return cell_val.as_number() == criteria_val.as_number();
|
|
if (cell_val.is_string()) {
|
|
auto n = try_parse_simple_number(cell_val.as_string());
|
|
return n.has_value() && *n == criteria_val.as_number();
|
|
}
|
|
return false;
|
|
}
|
|
if (criteria_val.is_bool()) {
|
|
return cell_val.is_bool() && cell_val.as_bool() == criteria_val.as_bool();
|
|
}
|
|
if (!criteria_val.is_string()) {
|
|
return false;
|
|
}
|
|
|
|
std::string_view crit = criteria_val.as_string();
|
|
enum class Op { Eq, Neq, Lt, Gt, Le, Ge };
|
|
Op op = Op::Eq;
|
|
bool has_explicit_op = false;
|
|
if (crit.rfind("<=", 0) == 0) { op = Op::Le; crit.remove_prefix(2); has_explicit_op = true; }
|
|
else if (crit.rfind(">=", 0) == 0) { op = Op::Ge; crit.remove_prefix(2); has_explicit_op = true; }
|
|
else if (crit.rfind("<>", 0) == 0) { op = Op::Neq; crit.remove_prefix(2); has_explicit_op = true; }
|
|
else if (!crit.empty() && crit[0] == '<') { op = Op::Lt; crit.remove_prefix(1); has_explicit_op = true; }
|
|
else if (!crit.empty() && crit[0] == '>') { op = Op::Gt; crit.remove_prefix(1); has_explicit_op = true; }
|
|
else if (!crit.empty() && crit[0] == '=') { op = Op::Eq; crit.remove_prefix(1); has_explicit_op = true; }
|
|
|
|
// Special empty checks "=" and "<>"
|
|
if (has_explicit_op && crit.empty()) {
|
|
bool is_cell_blank = cell_val.is_empty() ||
|
|
(cell_val.is_string() && cell_val.as_string().empty());
|
|
if (op == Op::Eq) return is_cell_blank;
|
|
if (op == Op::Neq) return !is_cell_blank;
|
|
return false;
|
|
}
|
|
|
|
auto rhs_num = try_parse_simple_number(crit);
|
|
if (rhs_num.has_value()) {
|
|
if (cell_val.is_number()) {
|
|
double cv = cell_val.as_number();
|
|
double rv = *rhs_num;
|
|
switch (op) {
|
|
case Op::Eq: return cv == rv;
|
|
case Op::Neq: return cv != rv;
|
|
case Op::Lt: return cv < rv;
|
|
case Op::Gt: return cv > rv;
|
|
case Op::Le: return cv <= rv;
|
|
case Op::Ge: return cv >= rv;
|
|
}
|
|
}
|
|
if (op == Op::Neq) {
|
|
if (cell_val.is_string()) {
|
|
auto cn = try_parse_simple_number(cell_val.as_string());
|
|
if (cn.has_value()) return *cn != *rhs_num;
|
|
}
|
|
return true;
|
|
}
|
|
if (op == Op::Eq && cell_val.is_string()) {
|
|
auto cn = try_parse_simple_number(cell_val.as_string());
|
|
return cn.has_value() && *cn == *rhs_num;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// Boolean criteria literal
|
|
std::string_view crit_trim = trim_ascii(crit);
|
|
if (iequals_ascii(crit_trim, "TRUE") || iequals_ascii(crit_trim, "FALSE")) {
|
|
bool rb = iequals_ascii(crit_trim, "TRUE");
|
|
if (cell_val.is_bool()) {
|
|
bool cb = cell_val.as_bool();
|
|
switch (op) {
|
|
case Op::Eq: return cb == rb;
|
|
case Op::Neq: return cb != rb;
|
|
case Op::Lt: return !cb && rb;
|
|
case Op::Gt: return cb && !rb;
|
|
case Op::Le: return cb <= rb;
|
|
case Op::Ge: return cb >= rb;
|
|
}
|
|
}
|
|
return op == Op::Neq;
|
|
}
|
|
|
|
// Error criteria literal
|
|
if (auto rerr = parse_error_code(crit_trim)) {
|
|
bool same = cell_val.is_error() && cell_val.as_error() == *rerr;
|
|
if (op == Op::Eq) return same;
|
|
if (op == Op::Neq) return !same;
|
|
return false;
|
|
}
|
|
|
|
// String comparison / wildcard matching
|
|
if (op == Op::Eq) {
|
|
if (cell_val.is_empty()) return crit.empty();
|
|
if (!cell_val.is_string()) return false;
|
|
return wildcard_match_ci(crit, cell_val.as_string());
|
|
}
|
|
if (op == Op::Neq) {
|
|
if (cell_val.is_empty()) return !crit.empty();
|
|
if (!cell_val.is_string()) return true;
|
|
return !wildcard_match_ci(crit, cell_val.as_string());
|
|
}
|
|
// Relational string comparison (<, >, <=, >=)
|
|
if (!cell_val.is_string()) return false;
|
|
auto cmp = compare_values(cell_val, Value::make_string(std::string(crit)));
|
|
if (!std::holds_alternative<int>(cmp)) return false;
|
|
int c = std::get<int>(cmp);
|
|
switch (op) {
|
|
case Op::Lt: return c < 0;
|
|
case Op::Gt: return c > 0;
|
|
case Op::Le: return c <= 0;
|
|
case Op::Ge: return c >= 0;
|
|
default: return false;
|
|
}
|
|
}
|
|
|
|
} // namespace htab::fn
|