tcp-server/client a bit improved

This commit is contained in:
2021-11-05 15:27:21 +01:00
parent 159845bb9b
commit 865e198a5b
6 changed files with 85 additions and 47 deletions

34
ml.cpp
View File

@@ -83,6 +83,12 @@ MlValue::MlValue(bool b) {
MlValue::MlValue(const std::vector<MlValue> &list) : type(LIST), list(list) {}
MlValue::MlValue(const std::vector<std::string> &slist) : type(LIST) {
list.reserve(slist.size());
for (size_t i = 0; i < slist.size(); i++)
list.push_back(MlValue::string(slist[i]));
}
MlValue MlValue::quote(const MlValue &quoted) {
MlValue result;
result.type = QUOTE;
@@ -1346,8 +1352,17 @@ MlValue tcp_client(std::vector<MlValue> args, MlEnvironment &env) {
throw MlError(MlValue("tcp-client", tcp_client), env, args.size() > 3 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
TcpNet tcpclient;
std::string response = tcpclient.client(args[0].as_string(), args[1].as_int(), args[2].as_string());
return MlValue::string(response);
if (args[2].is_list()) {
auto request_list = args[2].as_list();
std::vector<std::string> requests;// PERF reserve
std::transform(request_list.begin(), request_list.end(), back_inserter(requests), std::mem_fn(&MlValue::as_string));
std::vector<std::string> response = tcpclient.client(args[0].as_string(), args[1].as_int(), requests);
return MlValue(response);
} else {
std::string response = tcpclient.client(args[0].as_string(), args[1].as_int(), args[2].as_string());
return MlValue::string(response);
}
}
// Read a file and execute its code
@@ -1640,7 +1655,7 @@ MlValue tail(std::vector<MlValue> args, MlEnvironment &env) {
throw MlError(MlValue("tail", tail), env, args.size() > 1 ? TOO_MANY_ARGS : TOO_FEW_ARGS);
std::vector<MlValue> result, list = args[0].as_list();
// PERF reserve result
for (size_t i = 1; i < list.size(); i++)
result.push_back(list[i]);
@@ -1721,11 +1736,7 @@ MlValue string_regex_list(std::vector<MlValue> args, MlEnvironment &env) {
auto found_matches = regexp_search2(args[0].as_string(), args[1].as_string(), match_mode, ignore_case);
std::vector<MlValue> list;
for(auto &l : found_matches) {
std::vector<MlValue> sublist;
for(auto &v : l) {
sublist.push_back(MlValue::string(v));
}
list.push_back(sublist);
list.push_back(l);
}
return MlValue(list);
}
@@ -1739,12 +1750,7 @@ MlValue string_split(std::vector<MlValue> args, MlEnvironment &env) {
// TODO do it more efficient
std::vector<std::string> elements = regexp_strsplit(args[0].as_string(), args[1].as_string());
std::vector<MlValue> result{};
for (size_t i = 0; i < elements.size(); i++)
result.push_back(MlValue::string(elements[i]));
return MlValue(result);
return MlValue(elements);
}
// converts string to upper or lower case