Initial commit: HTab spreadsheet engine and GemUI X11 frontend
This commit is contained in:
commit
395f458fee
55 files changed
+12682
No files matched your search
@@ -0,0 +1,275 @@
|
||||
#include "engine/value.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
|
||||
namespace htab {
|
||||
|
||||
namespace {
|
||||
|
||||
bool is_ascii_space(char ch) {
|
||||
return ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n';
|
||||
}
|
||||
|
||||
std::optional<double> parse_numeric_text(std::string_view sv) {
|
||||
sv = trim_ascii(sv);
|
||||
if (sv.empty()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
bool is_percent = false;
|
||||
if (sv.back() == '%') {
|
||||
is_percent = 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_digits = false;
|
||||
while (i < sv.size() && std::isdigit(static_cast<unsigned char>(sv[i]))) {
|
||||
exp_digits = true;
|
||||
++i;
|
||||
}
|
||||
if (!exp_digits) {
|
||||
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 (is_percent) {
|
||||
val /= 100.0;
|
||||
}
|
||||
if (val == 0.0) {
|
||||
val = 0.0;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
Value unwrap_1x1(const Value& v) {
|
||||
if (!v.is_array()) {
|
||||
return v;
|
||||
}
|
||||
const auto& arr = v.as_array();
|
||||
if (arr && arr->rows == 1 && arr->cols == 1 && !arr->data.empty()) {
|
||||
return arr->at(0, 0);
|
||||
}
|
||||
return Value::make_error(ErrorCode::Value);
|
||||
}
|
||||
|
||||
int compare_strings_ci(std::string_view a, std::string_view b) {
|
||||
size_t n = std::min(a.size(), b.size());
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
unsigned char ca = static_cast<unsigned char>(std::tolower(static_cast<unsigned char>(a[i])));
|
||||
unsigned char cb = static_cast<unsigned char>(std::tolower(static_cast<unsigned char>(b[i])));
|
||||
if (ca < cb) return -1;
|
||||
if (ca > cb) return 1;
|
||||
}
|
||||
if (a.size() < b.size()) return -1;
|
||||
if (a.size() > b.size()) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int type_rank(const Value& v) {
|
||||
if (v.is_number()) return 0;
|
||||
if (v.is_string()) return 1;
|
||||
if (v.is_bool()) return 2;
|
||||
return 3;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::string_view error_to_string(ErrorCode err) {
|
||||
switch (err) {
|
||||
case ErrorCode::Div0: return "#DIV/0!";
|
||||
case ErrorCode::Value: return "#VALUE!";
|
||||
case ErrorCode::Name: return "#NAME?";
|
||||
case ErrorCode::NA: return "#N/A";
|
||||
case ErrorCode::Ref: return "#REF!";
|
||||
case ErrorCode::Num: return "#NUM!";
|
||||
case ErrorCode::Cycle: return "#CYCLE!";
|
||||
case ErrorCode::Spill: return "#SPILL!";
|
||||
}
|
||||
return "#VALUE!";
|
||||
}
|
||||
|
||||
std::optional<ErrorCode> parse_error_code(std::string_view text) {
|
||||
text = trim_ascii(text);
|
||||
if (iequals_ascii(text, "#DIV/0!")) return ErrorCode::Div0;
|
||||
if (iequals_ascii(text, "#VALUE!")) return ErrorCode::Value;
|
||||
if (iequals_ascii(text, "#NAME?")) return ErrorCode::Name;
|
||||
if (iequals_ascii(text, "#N/A")) return ErrorCode::NA;
|
||||
if (iequals_ascii(text, "#REF!")) return ErrorCode::Ref;
|
||||
if (iequals_ascii(text, "#NUM!")) return ErrorCode::Num;
|
||||
if (iequals_ascii(text, "#CYCLE!")) return ErrorCode::Cycle;
|
||||
if (iequals_ascii(text, "#SPILL!")) return ErrorCode::Spill;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
Value Value::make_number(double d) {
|
||||
return Value(d == 0.0 ? 0.0 : d);
|
||||
}
|
||||
|
||||
Value Value::make_checked_number(double d) {
|
||||
if (!std::isfinite(d)) {
|
||||
return Value::make_error(ErrorCode::Num);
|
||||
}
|
||||
return Value(d == 0.0 ? 0.0 : d);
|
||||
}
|
||||
|
||||
std::variant<double, ErrorCode> to_number(const Value& v) {
|
||||
Value u = unwrap_1x1(v);
|
||||
if (u.is_error()) return u.as_error();
|
||||
if (u.is_empty()) return 0.0;
|
||||
if (u.is_number()) return u.as_number();
|
||||
if (u.is_bool()) return u.as_bool() ? 1.0 : 0.0;
|
||||
if (u.is_string()) {
|
||||
auto parsed = parse_numeric_text(u.as_string());
|
||||
if (parsed) return *parsed;
|
||||
return ErrorCode::Value;
|
||||
}
|
||||
return ErrorCode::Value;
|
||||
}
|
||||
|
||||
std::variant<bool, ErrorCode> to_bool(const Value& v) {
|
||||
Value u = unwrap_1x1(v);
|
||||
if (u.is_error()) return u.as_error();
|
||||
if (u.is_empty()) return false;
|
||||
if (u.is_bool()) return u.as_bool();
|
||||
if (u.is_number()) return u.as_number() != 0.0;
|
||||
if (u.is_string()) {
|
||||
auto t = trim_ascii(u.as_string());
|
||||
if (iequals_ascii(t, "TRUE")) return true;
|
||||
if (iequals_ascii(t, "FALSE")) return false;
|
||||
return ErrorCode::Value;
|
||||
}
|
||||
return ErrorCode::Value;
|
||||
}
|
||||
|
||||
std::variant<std::string, ErrorCode> to_string(const Value& v) {
|
||||
Value u = unwrap_1x1(v);
|
||||
if (u.is_error()) return u.as_error();
|
||||
if (u.is_empty()) return std::string{};
|
||||
if (u.is_string()) return u.as_string();
|
||||
if (u.is_bool()) return u.as_bool() ? std::string("TRUE") : std::string("FALSE");
|
||||
if (u.is_number()) {
|
||||
double d = u.as_number();
|
||||
if (d == 0.0) d = 0.0;
|
||||
char buf[64];
|
||||
std::snprintf(buf, sizeof(buf), "%.15g", d);
|
||||
return std::string(buf);
|
||||
}
|
||||
return ErrorCode::Value;
|
||||
}
|
||||
|
||||
std::variant<int, ErrorCode> compare_values(const Value& a_in, const Value& b_in) {
|
||||
Value a = unwrap_1x1(a_in);
|
||||
if (a.is_error()) return a.as_error();
|
||||
Value b = unwrap_1x1(b_in);
|
||||
if (b.is_error()) return b.as_error();
|
||||
|
||||
if (a.is_empty() && b.is_empty()) {
|
||||
return 0;
|
||||
}
|
||||
if (a.is_empty()) {
|
||||
if (b.is_number()) a = Value::make_number(0.0);
|
||||
else if (b.is_string()) a = Value::make_string("");
|
||||
else if (b.is_bool()) a = Value::make_bool(false);
|
||||
} else if (b.is_empty()) {
|
||||
if (a.is_number()) b = Value::make_number(0.0);
|
||||
else if (a.is_string()) b = Value::make_string("");
|
||||
else if (a.is_bool()) b = Value::make_bool(false);
|
||||
}
|
||||
|
||||
int ra = type_rank(a);
|
||||
int rb = type_rank(b);
|
||||
if (ra != rb) {
|
||||
return ra < rb ? -1 : 1;
|
||||
}
|
||||
if (a.is_number()) {
|
||||
double da = a.as_number();
|
||||
double db = b.as_number();
|
||||
if (da < db) return -1;
|
||||
if (da > db) return 1;
|
||||
return 0;
|
||||
}
|
||||
if (a.is_string()) {
|
||||
return compare_strings_ci(a.as_string(), b.as_string());
|
||||
}
|
||||
if (a.is_bool()) {
|
||||
bool ba = a.as_bool();
|
||||
bool bb = b.as_bool();
|
||||
if (ba == bb) return 0;
|
||||
return ba ? 1 : -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool iequals_ascii(std::string_view a, std::string_view b) {
|
||||
if (a.size() != b.size()) return false;
|
||||
for (size_t i = 0; i < a.size(); ++i) {
|
||||
if (std::tolower(static_cast<unsigned char>(a[i])) !=
|
||||
std::tolower(static_cast<unsigned char>(b[i]))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string to_upper_ascii(std::string_view s) {
|
||||
std::string out(s);
|
||||
for (char& ch : out) {
|
||||
ch = static_cast<char>(std::toupper(static_cast<unsigned char>(ch)));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string to_lower_ascii(std::string_view s) {
|
||||
std::string out(s);
|
||||
for (char& ch : out) {
|
||||
ch = static_cast<char>(std::tolower(static_cast<unsigned char>(ch)));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string_view trim_ascii(std::string_view s) {
|
||||
while (!s.empty() && is_ascii_space(s.front())) s.remove_prefix(1);
|
||||
while (!s.empty() && is_ascii_space(s.back())) s.remove_suffix(1);
|
||||
return s;
|
||||
}
|
||||
|
||||
} // namespace htab
|
||||
Reference in new issue
Block a user