Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace instances of stringr::str_sub() with base R equivalent #2195

Merged
merged 9 commits into from
Nov 19, 2022
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ Collate:
'template.R'
'utils-conversion.R'
'utils-rd2html.R'
'utils-string.R'
'utils-sweave.R'
'utils-upload.R'
'utils-vignettes.R'
Expand Down
13 changes: 4 additions & 9 deletions R/block.R
Original file line number Diff line number Diff line change
Expand Up @@ -551,22 +551,17 @@ inline_exec = function(
code = block$code; input = block$input
if ((n <- length(code)) == 0) return(input) # untouched if no code is found

loc = block$location
ans = character(n)
for (i in 1:n) {
res = hook_eval(code[i], envir)
if (inherits(res, c('knit_asis', 'knit_asis_url'))) res = sew(res, inline = TRUE)
tryCatch(as.character(res), error = function(e) {
stop2("The inline value cannot be coerced to character: ", code[i])
})
d = nchar(input)
# replace with evaluated results
stringr::str_sub(input, loc[i, 1], loc[i, 2]) = if (length(res)) {
paste(hook(res), collapse = '')
} else ''
if (i < n) loc[(i + 1):n, ] = loc[(i + 1):n, ] - (d - nchar(input))
# may need to move back and forth because replacement may be longer or shorter
if (length(res)) ans[i] = paste(hook(res), collapse = '')
}
input
# replace with evaluated results
str_replace(input, block$location, ans)
}

process_tangle = function(x) {
Expand Down
8 changes: 4 additions & 4 deletions R/header.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ insert_header_latex = function(doc, b) {
doc[j] = sub(p, '\n\\\\IfFileExists{upquote.sty}{\\\\usepackage{upquote}}{}\n\\2', doc[j], perl = TRUE)
}
i = i[1L]; l = stringr::str_locate(doc[i], b)
tmp = stringr::str_sub(doc[i], l[, 1], l[, 2])
stringr::str_sub(doc[i], l[,1], l[,2]) = paste0(tmp, make_header_latex(doc))
tmp = substr(doc[i], l[, 1], l[, 2])
doc[i] = str_replace(doc[i], l, paste0(tmp, make_header_latex(doc)))
} else if (parent_mode() && !child_mode()) {
# in parent mode, we fill doc to be a complete document
doc[1L] = one_string(c(
Expand Down Expand Up @@ -87,8 +87,8 @@ insert_header_html = function(doc, b) {
i = grep(b, doc)
if (length(i) == 1L) {
l = stringr::str_locate(doc[i], b)
tmp = stringr::str_sub(doc[i], l[, 1], l[, 2])
stringr::str_sub(doc[i], l[,1], l[,2]) = paste0(tmp, '\n', make_header_html())
tmp = substr(doc[i], l[, 1], l[, 2])
doc[i] = str_replace(doc[i], l, paste0(tmp, '\n', make_header_html()))
}
doc
}
Expand Down
10 changes: 10 additions & 0 deletions R/utils-string.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# replace parts of a string with new values; `pos` is a matrix of positions and
# each row is a pair of [start, end]
str_replace = function(x, pos, value) {
if (length(x) != 1) stop("Only a character scalar is supported.")
# extract parts of the string that are outside [start, end]
m = rbind(pos[, 1] - 1, pos[, 2] + 1)
m = matrix(c(1, m, nchar(x)), nrow = 2)
y = substring(x, m[1, ], m[2, ])
paste(rbind(y, c(value, '')), collapse = '')
}
4 changes: 2 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ set_preamble = function(input, patterns = knit_patterns$get()) {
if (is.na(idx1) || idx1 >= idx2) return()
txt = one_string(input[idx1:(idx2 - 1L)]) # rough preamble
idx = stringr::str_locate(txt, hb) # locate documentclass
options(tikzDocumentDeclaration = stringr::str_sub(txt, idx[, 1L], idx[, 2L]))
preamble = pure_preamble(split_lines(stringr::str_sub(txt, idx[, 2L] + 1L)), patterns)
options(tikzDocumentDeclaration = substr(txt, idx[, 1L], idx[, 2L]))
preamble = pure_preamble(split_lines(substr(txt, idx[, 2L] + 1L, nchar(txt))), patterns)
.knitEnv$tikzPackages = c(.header.sweave.cmd, preamble, '\n')
.knitEnv$bibliography = grep('^\\\\bibliography.+', input, value = TRUE)
}
Expand Down