Require usage of startsWith() and endsWith() over grepl()/substr() versions
Source:R/string_boundary_linter.R
string_boundary_linter.Rd
startsWith()
is used to detect fixed initial substrings; it is more
readable and more efficient than equivalents using grepl()
or substr()
.
c.f. startsWith(x, "abc")
, grepl("^abc", x)
,
substr(x, 1L, 3L) == "abc"
.
Arguments
- allow_grepl
Logical, default
FALSE
. IfTRUE
, usages withgrepl()
are ignored. Some authors may prefer theNA
input toFALSE
output conciseness offered bygrepl()
, which doesn't have a direct equivalent withstartsWith()
orendsWith()
.
Details
Ditto for using endsWith()
to detect fixed terminal substrings.
Note that there is a difference in behavior between how grepl()
and startsWith()
(and endsWith()
) handle missing values. In particular, for grepl()
, NA
inputs
are considered FALSE
, while for startsWith()
, NA
inputs have NA
outputs.
That means the strict equivalent of grepl("^abc", x)
is
!is.na(x) & startsWith(x, "abc")
.
We lint grepl()
usages by default because the !is.na()
version is more explicit
with respect to NA
handling -- though documented, the way grepl()
handles
missing inputs may be surprising to some readers.
See also
linters for a complete list of linters available in lintr.