-
-
Notifications
You must be signed in to change notification settings - Fork 221
Closed
Description
I put together a sugar function trimws to mirror base::trimws (added in R 3.2.0). For those not familiar with this, it removes either leading (which = "left"), trailing (which = "right"), or leading and trailing (which = "both") whitespace (defined as any of ' ', '\t', '\r', or '\n') from strings, returning NA on NA input.
In my fork, usage looks like this:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
CharacterVector trim(CharacterVector x, const char* which = "both")
{
return trimws(x, which);
}
/*** R
trim(" x y z \t \n \r ")
# [1] "x y z"
trim(" x y z \t \n \r ", "left")
# [1] "x y z \t \n \r "
trim(" x y z \t \n \r ", "right")
# [1] " x y z"
*/Other features:
- Default
whichargument is"both", as inbase::trimws - Only the first letter of
whichis checked ('b','l', and'r'are valid first letters) to mimic the use ofmatch.argin R - It is also overloaded for the
CharacterMatrixandStringclasses - Much faster than the
baseversion, which uses regex to accomplish this
If this seems like a worthwhile addition, I can submit a PR shortly.
eddelbuettel and coatless
Metadata
Metadata
Assignees
Labels
No labels