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

Sugar function trimws #679

nathan-russell opened this Issue Apr 22, 2017 · 0 comments


None yet
1 participant

nathan-russell commented Apr 22, 2017

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 which argument is "both", as in base::trimws
  • Only the first letter of which is checked ('b', 'l', and 'r' are valid first letters) to mimic the use of match.arg in R
  • It is also overloaded for the CharacterMatrix and String classes
  • Much faster than the base version, which uses regex to accomplish this

If this seems like a worthwhile addition, I can submit a PR shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment