Finally add the split module

This commit is contained in:
2025-10-06 19:22:09 +11:00
parent 2ae3086481
commit 26cb591c49

View File

@@ -1,8 +1,45 @@
#include "split.h"
#include "../../defs/defs.h"
#include "../../error/error.h"
#include <vector>
#include <string>
Value modules::split(std::vector<Value> args) {
return Value("Work in progress!");
if (args.size() < 1) {
error("Not enough args for split module");
}
if (args[0].valtype != ValueType::String) {
error("First argument of split must be a string");
}
std::string delimiter = " ";
if (args.size() > 1) {
if (args[1].valtype == ValueType::String) {
delimiter = args[1].string_val;
} else {
error("Expected a string delimiter as second argument of split");
}
}
std::string buf;
std::vector<Value> list;
for (const char& chr : args[0].string_val) {
buf += chr;
if (buf.size() >= delimiter.size()) {
std::string checker = buf.substr(buf.size() - delimiter.size());
if (checker == delimiter) {
list.push_back(Value("\"" + buf.substr(0, buf.size() - delimiter.size()) + "\""));
buf.clear();
}
}
}
if (!buf.empty()) {
list.push_back(Value("\"" + buf + "\""));
}
return Value(list);
}