Skip to content

Sugar function trimws #679

@nathan-russell

Description

@nathan-russell

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions