r-lib/backports

trimws implementation differs from that of R.

Closed this issue · 1 comments

e.g. in R 3.4.0 base::trimws is defined as

function (x, which = c("both", "left", "right"))
{
    which <- match.arg(which)
    mysub <- function(re, x) sub(re, "", x, perl = TRUE)
    if (which == "left")
        return(mysub("^[ \t\r\n]+", x))
    if (which == "right")
        return(mysub("[ \t\r\n]+$", x))
    mysub("[ \t\r\n]+$", mysub("^[ \t\r\n]+", x))
}

backports:::trimws

function (x, which = c("both", "left", "right"))
{
    which = match.arg(which)
    if (which %in% c("left", "both"))
        x = gsub("^[[:space:]]", "", x)
    if (which %in% c("right", "both"))
        x = gsub("[[:space:]]$", "", x)
    return(x)
}

While we could debate which implementation is better (I actually prefer the backports one), I don't think it is appropriate for this package to change the implementations unnecessarily.

A package using backports will change behavior if it is installed on a newer R that does define trimws.

base::trimws("a\v")
#> [1] "a\v"

backports:::trimws("a\v")
#> [1] "a"
mllg commented

Right, changed it to match the current implementation in R core.