Skip to content

Commit

Permalink
set perl = !fixed. add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ayogasekaram committed Jun 11, 2024
1 parent 11024e8 commit 3637e0c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
20 changes: 10 additions & 10 deletions R/standalone-stringr.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,41 @@ str_trim <- function(string, side = c("both", "left", "right")) {
}

str_squish <- function(string, fixed = FALSE) {
string <- gsub("\\s+", " ", string) # Replace multiple white spaces with a single white space
string <- gsub("^\\s+|\\s+$", "", string) # Trim leading and trailing white spaces
string <- gsub("\\s+", " ", string, perl = !fixed) # Replace multiple white spaces with a single white space
string <- gsub("^\\s+|\\s+$", "", string, perl= !fixed) # Trim leading and trailing white spaces
return(string)
}

str_remove <- function (string, pattern, fixed = FALSE) {
sub (x = string, pattern = pattern, replacement = "", fixed = fixed)
sub (x = string, pattern = pattern, replacement = "", fixed = fixed, perl = !fixed)
}

str_remove_all <- function(string, pattern, fixed = FALSE) {
gsub(x = string, pattern = pattern, replacement = "", fixed = fixed)
gsub(x = string, pattern = pattern, replacement = "", fixed = fixed, perl = !fixed)
}

str_extract <- function(string, pattern, fixed = FALSE) {
res <- rep(NA_character_, length.out = length(string))
res[str_detect(string, pattern, fixed = fixed)] <-
regmatches(x = string, m = regexpr(pattern = pattern, text = string, fixed = fixed))
regmatches(x = string, m = regexpr(pattern = pattern, text = string, fixed = fixed, perl = !fixed))

res
}

str_extract_all <- function(string, pattern, fixed = FALSE) {
regmatches(x = string, m = gregexpr(pattern = pattern, text = string, fixed = fixed))
regmatches(x = string, m = gregexpr(pattern = pattern, text = string, fixed = fixed, perl = !fixed))
}

str_detect <- function(string, pattern, fixed = FALSE) {
grepl(pattern = pattern, x = string, fixed = fixed)
grepl(pattern = pattern, x = string, fixed = fixed, perl = !fixed)
}

str_replace <- function(string, pattern, replacement, fixed = FALSE){
sub(x = string, pattern = pattern, replacement = replacement, fixed = fixed)
sub(x = string, pattern = pattern, replacement = replacement, fixed = fixed, perl = !fixed)
}

str_replace_all <- function (string, pattern, replacement, fixed = FALSE){
gsub(x = string, pattern = pattern, replacement = replacement, fixed = fixed)
gsub(x = string, pattern = pattern, replacement = replacement, fixed = fixed, perl = !fixed)
}

word <- function(string, start, end = start, sep = " ", fixed = TRUE) {
Expand All @@ -63,7 +63,7 @@ word <- function(string, start, end = start, sep = " ", fixed = TRUE) {
return(sapply(string, word, start, end, sep, fixed, USE.NAMES = FALSE))
}

words <- unlist(strsplit(string, split = sep, fixed = fixed))
words <- unlist(strsplit(string, split = sep, fixed = fixed, perl = !fixed))
words <- words[words != ""] # Remove empty strings

# Adjust negative indices
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-standalone-stringr.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ test_that("str_remove_all() works", {

s <- str_remove_all(c("one.1", "two..2", "three...3"), ".")
expect_identical(s, stringr::str_remove_all(c("one.1", "two..2", "three...3"), "."))

s <- str_remove_all("This is a test string\nwith multiple\nlines", pattern = "\\n(?!\\\\)")
expect_identical(s, stringr::str_remove_all("This is a test string\nwith multiple\nlines", pattern = "\\n(?!\\\\)"))
})

test_that("str_extract() works", {
Expand Down Expand Up @@ -157,6 +160,9 @@ test_that("str_replace() works", {

s <- str_replace(fruits, "([aeiou])", "\\1\\1")
expect_identical(s, stringr::str_replace(fruits, "([aeiou])", "\\1\\1"))

s <- str_replace("This is a test string\nwith multiple\nlines", pattern = "\\n(?!\\\\)", replacement = "")
expect_identical(s, stringr::str_replace("This is a test string\nwith multiple\nlines", pattern = "\\n(?!\\\\)", replacement = ""))
})

test_that("str_replace_all() works", {
Expand All @@ -172,6 +178,9 @@ test_that("str_replace_all() works", {

s <- str_replace_all(fruits, "[aeiou]", "-")
expect_identical(s, stringr::str_replace_all(fruits, "[aeiou]", "-"))

s <- str_replace_all("This is a test string\nwith multiple\nlines", pattern = "\\n(?!\\\\)", replacement = "")
expect_identical(s, stringr::str_replace_all("This is a test string\nwith multiple\nlines", pattern = "\\n(?!\\\\)", replacement = ""))
})

test_that("str_sub() works", {
Expand Down

0 comments on commit 3637e0c

Please sign in to comment.