Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add strings.contains{_i} commands #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion src/command_logic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,42 @@ apply_arith_other(const char* op, const torrent::Object::list_type& args) {
}
}

torrent::Object::value_type
apply_string_contains(bool ignore_case,
const torrent::Object::list_type& args) {
if (args.size() < 2) {
throw torrent::input_error(
"string.contains[_i] takes at least two arguments!");
}

torrent::Object::list_const_iterator itr = args.begin();
std::string text = itr->as_string();
if (ignore_case)
std::transform(text.begin(), text.end(), text.begin(), ::tolower);

for (++itr; itr != args.end(); ++itr) {
std::string substr = itr->as_string();
if (ignore_case)
std::transform(substr.begin(), substr.end(), substr.begin(), ::tolower);
if (substr.empty() || text.find(substr) != std::string::npos)
return 1;
}

return 0;
}

torrent::Object
cmd_string_contains(rpc::target_type, const torrent::Object::list_type& args) {
return apply_string_contains(false, args);
}

// XXX: Will NOT work correctly for non-ASCII strings!
torrent::Object
cmd_string_contains_i(rpc::target_type,
const torrent::Object::list_type& args) {
return apply_string_contains(true, args);
}

void
initialize_command_logic() {
CMD2_ANY("cat", &apply_cat);
Expand Down Expand Up @@ -761,4 +797,7 @@ initialize_command_logic() {
CMD2_ANY_LIST("elapsed.greater", [](const auto&, const auto& args) {
return apply_elapsed_greater(args);
kannibalox marked this conversation as resolved.
Show resolved Hide resolved
});
kannibalox marked this conversation as resolved.
Show resolved Hide resolved
}

CMD2_ANY_LIST("string.contains", &cmd_string_contains);
CMD2_ANY_LIST("string.contains_i", &cmd_string_contains_i);
}