Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# stringr (development version)

* Update `str_starts()` and `str_ends()` functions so they honor regex operator precedence. (@carlganz)

* `word()` now returns all the sentence when using a negative `start` parameter
that is greater or equal than the number of words. (@pdelboca, #245)

Expand Down
4 changes: 2 additions & 2 deletions R/detect.r
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ str_starts <- function(string, pattern, negate = FALSE) {
fixed = stri_startswith_fixed(string, pattern, negate = negate, opts_fixed = opts(pattern)),
coll = stri_startswith_coll(string, pattern, negate = negate, opts_collator = opts(pattern)),
regex = {
pattern2 <- paste0("^", pattern)
pattern2 <- paste0("^(", pattern, ")")
attributes(pattern2) <- attributes(pattern)
str_detect(string, pattern2, negate)
}
Expand All @@ -98,7 +98,7 @@ str_ends <- function(string, pattern, negate = FALSE) {
fixed = stri_endswith_fixed(string, pattern, negate = negate, opts_fixed = opts(pattern)),
coll = stri_endswith_coll(string, pattern, negate = negate, opts_collator = opts(pattern)),
regex = {
pattern2 <- paste0(pattern, "$")
pattern2 <- paste0("(", pattern, ")$")
attributes(pattern2) <- attributes(pattern)
str_detect(string, pattern2, negate)
}
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-detect.r
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ test_that("str_starts works", {
# Special typing of patterns.
expect_true(str_starts("ab", fixed("A", ignore_case = TRUE)))
expect_true(str_starts("ab", regex("A", ignore_case = TRUE)))

# Or operators are respected
expect_true(str_starts("ab", "b|a"))
expect_false(str_starts("ab", "c|b"))
})

test_that("str_ends works", {
Expand All @@ -48,6 +52,10 @@ test_that("str_ends works", {

# Special typing of patterns.
expect_true(str_ends("ab", fixed("B", ignore_case = TRUE)))

# Or operators are respected
expect_true(str_ends("ab", "b|a"))
expect_false(str_ends("ab", "c|a"))
})


Expand Down