Skip to content

Commit

Permalink
string.[lr]strip: working for whitespace, i.e. one arg (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
pyroscope committed Jun 18, 2018
1 parent 99d99f9 commit 91e3298
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
35 changes: 35 additions & 0 deletions patches/command_pyroscope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,38 @@ torrent::Object cmd_string_join(rpc::target_type target, const torrent::Object::
}


torrent::Object cmd_string_strip(int where, const torrent::Object::list_type& args) {
std::string text = string_get_first_arg("[lr]strip", args);
torrent::Object::list_const_iterator first = args.begin() + 1, last = args.end();

if (args.size() == 1) {
// Strip whitespace
if (where <= 0) {
text.erase(text.begin(),
std::find_if(text.begin(), text.end(),
std::not1(std::ptr_fun<int, int>(std::isspace))));
}
if (where >= 0) {
text.erase(std::find_if(text.rbegin(), text.rend(),
std::not1(std::ptr_fun<int, int>(std::isspace))).base(),
text.end());
}
} else {
for (torrent::Object::list_const_iterator itr = first; itr != last; ++itr) {
bool changed;
do {
changed = false;
const std::string& strippable = itr->as_string();

// TODO
} while (changed);
}
}

return text;
}


torrent::Object cmd_string_split(rpc::target_type target, const torrent::Object::list_type& args) {
const std::string text = string_get_first_arg("split", args);
if (args.size() != 2 || !args.rbegin()->is_string()) {
Expand Down Expand Up @@ -1106,6 +1138,9 @@ void initialize_command_pyroscope() {
CMD2_ANY_LIST("string.equals", std::bind(&cmd_string_compare, 0, std::placeholders::_2));
CMD2_ANY_LIST("string.startswith", std::bind(&cmd_string_compare, 1, std::placeholders::_2));
CMD2_ANY_LIST("string.endswith", std::bind(&cmd_string_compare, 2, std::placeholders::_2));
CMD2_ANY_LIST("string.strip", std::bind(&cmd_string_strip, 0, std::placeholders::_2));
CMD2_ANY_LIST("string.lstrip", std::bind(&cmd_string_strip, -1, std::placeholders::_2));
CMD2_ANY_LIST("string.rstrip", std::bind(&cmd_string_strip, 1, std::placeholders::_2));

// array.* group
CMD2_ANY_LIST("array.at", &cmd_array_at);
Expand Down
13 changes: 13 additions & 0 deletions tests/commands/string.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ $ rtxmlrpc --repr string.equals '' +1 2nd
ERROR … string.equals needs a string argument.0
# END

# string.strip
$ rtxmlrpc --repr string.lstrip '' ' left only '
'left only '
$ rtxmlrpc --repr string.strip '' ' both ends '
'both ends'
$ rtxmlrpc --repr string.rstrip '' ' right only '
' right only'
$ rtxmlrpc --repr string.strip '' ' abc ' ' '
'abc'
$ rtxmlrpc --repr string.strip '' +42
ERROR … string.[lr]strip needs a string argument.0
# END

# string.join
$ rtxmlrpc --repr string.join '' ':'
''
Expand Down

0 comments on commit 91e3298

Please sign in to comment.