Skip to content

Commit

Permalink
fix: more fixes and more tests for str_sub() and substr()
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher committed Dec 26, 2024
1 parent 04dfc46 commit 6bc3a37
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
17 changes: 15 additions & 2 deletions R/funs-string.R
Original file line number Diff line number Diff line change
Expand Up @@ -258,20 +258,33 @@ pl_str_sub_stringr <- function(string, start, end = NULL) {
len_string + end + 1
)$otherwise(2000)

foo6 <- pl$when(start_is_zero & end < 0 & end$abs() <= len_string)$then(
len_string + end + 1
)$otherwise(2000)

pl$

when(string$is_null() | start$is_null() | (end$is_null() & !end_is_null))$
then(pl$lit(NA_character_))$

when(start_is_zero & (end_is_zero | end$abs() > len_string))$
when(start > 0 & start > len_string & end > 0 & end > len_string)$
then(pl$lit(""))$

when(start_is_zero & end_is_zero)$
then(pl$lit(""))$

when(start_is_zero & (end_is_null | end$abs() > len_string))$
then(string)$

when(!start_is_zero & start >= 0 & end < 0 & end$abs() > len_string)$
then(pl$lit(""))$

when(start_is_zero & (end_is_null | end$abs() <= len_string))$
when(start_is_zero & end > 0 & end <= len_string)$
then(string$str$slice(0, foo2))$

when(start_is_zero & end < 0 & end$abs() <= len_string)$
then(string$str$slice(0, foo6))$

when(start >= 0 & end_is_null)$
then(string$str$slice(start, foo3))$

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-funs-string-lazy.R
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ test_that("str_dup() works", {

test_that("str_sub() works", {
for_all(
tests = 40,
tests = 100,
string = character_(any_na = TRUE),
start = numeric_(any_na = TRUE),
end = numeric_(any_na = TRUE),
Expand All @@ -180,7 +180,7 @@ test_that("substr() works", {
length <- sample(1:10, 1)

for_all(
tests = 40,
tests = 100,
string = character_(any_na = TRUE, len = length),
start = numeric_(any_na = TRUE, len = length),
end = numeric_(any_na = TRUE, len = length),
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-funs-string.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ test_that("str_dup() works", {

test_that("str_sub() works", {
for_all(
tests = 40,
tests = 100,
string = character_(any_na = TRUE),
start = numeric_(any_na = TRUE),
end = numeric_(any_na = TRUE),
Expand All @@ -176,7 +176,7 @@ test_that("substr() works", {
length <- sample(1:10, 1)

for_all(
tests = 40,
tests = 100,
string = character_(any_na = TRUE, len = length),
start = numeric_(any_na = TRUE, len = length),
end = numeric_(any_na = TRUE, len = length),
Expand Down

0 comments on commit 6bc3a37

Please sign in to comment.