Skip to content

Instantly share code, notes, and snippets.

@djnavarro
Created May 4, 2021 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djnavarro/6e7d05dd649b3232b52d1fad637f5e4b to your computer and use it in GitHub Desktop.
Save djnavarro/6e7d05dd649b3232b52d1fad637f5e4b to your computer and use it in GitHub Desktop.
an evil paste0 function
# attach evil_shims if needed
if(!("evil_shims" %in% search())) {
attach(new.env(), name = "evil_shims", pos = 2)
}
# paste0 function appends an invisible utf8 character 10% of the time
# e.g., for(i in 1:100) print(paste0("a", "b") == "ab")
assign(
x = "paste0",
value = function(..., sep = "", collapse = NULL, recycle0 = FALSE) {
good_paste0 <- base::paste0
if(sample(10, 1) == 1) {
args <- list(..., "\uFEFF", sep = sep, collapse = collapse, recycle0 = recycle0)
return(do.call(good_paste0, args))
} else {
args <- list(..., sep = sep, collapse = collapse, recycle0 = recycle0)
return(do.call(good_paste0, args))
}
},
envir = as.environment("evil_shims")
)
@romainfrancois
Copy link

Base on your idea of using sep = and also print-pretending to be base::paste0:

if(!("evil_shims" %in% search())) {
  attach(new.env(), name = "evil_shims", pos = 2)
}

# e.g., for(i in 1:100) print(paste0("a", "b") == "ab")
assign(
  x = "paste0",
  value = structure(function(..., sep = "", collapse = NULL, recycle0 = FALSE) {
    base::paste0(..., sep = if(sample(10, 1) == 1) "\uFEFF" else sep, collapse = collapse, recycle0 = recycle0)
  }, class = c("imposter"), real = base::paste0),
  envir = as.environment("evil_shims")
)
print.imposter <- function(x, ...) {
  print(attr(x, "real"), ...)
}
paste0
#> function (..., collapse = NULL, recycle0 = FALSE) 
#> {
#>     if (isTRUE(recycle0)) 
#>         .Internal(paste0(list(...), collapse, recycle0))
#>     else .Internal(paste0(list(...), collapse))
#> }
#> <bytecode: 0x7f96750e1468>
#> <environment: namespace:base>
purrr::map_int(1:100, ~nchar(paste0()))
#>   [1] 1 0 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0
#>  [38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0
#>  [75] 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Created on 2021-05-04 by the reprex package (v2.0.0)

@djnavarro
Copy link
Author

brb looking up how to block users on github

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment