Skip to content

Commit

Permalink
[load] attempt to improve the amp_split() function. (#1186)
Browse files Browse the repository at this point in the history
The split into left, center and right previously could fail. fixes #1185
  • Loading branch information
JanMarvin authored Nov 20, 2024
1 parent e255996 commit cb0d24f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ importFrom(magrittr,"%>%")
importFrom(stringi,stri_c)
importFrom(stringi,stri_encode)
importFrom(stringi,stri_extract_all_regex)
importFrom(stringi,stri_extract_first_regex)
importFrom(stringi,stri_isempty)
importFrom(stringi,stri_join)
importFrom(stringi,stri_match)
Expand Down
29 changes: 20 additions & 9 deletions R/helper-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,28 @@ pxml <- function(x) {
#' @noRd
amp_split <- function(x) {
if (length(x) == 0) return(NULL)
# create output string of width 3
res <- vector("character", 3)
# Identify the names found in the string: returns them as matrix: strip the &amp;
nam <- gsub(pattern = "&amp;", "", unlist(stri_match_all_regex(x, "&amp;[LCR]")))
# split the string and assign names to join
z <- unlist(stri_split_regex(x, "&amp;[LCR]", omit_empty = TRUE))

if (length(z) == 0) return(character(0))
# Initialize an empty vector of three elements
res <- c(L = "", C = "", R = "")

names(z) <- as.character(nam)
res[c("L", "C", "R") %in% names(z)] <- z
has <- c(0, 0, 0)
# Extract each component if present and remove &amp;[LCR]
if (grepl("&amp;L", x)) {
has[1] <- 1
res[1] <- stri_extract_first_regex(x, "&amp;L.*?(?=&amp;C|&amp;R|$)")
}
if (grepl("&amp;C", x)) {
has[2] <- 1
res[2] <- stri_extract_first_regex(x, "&amp;C.*?(?=&amp;R|$)")
}
if (grepl("&amp;R", x)) {
has[3] <- 1
res[3] <- stri_extract_first_regex(x, "&amp;R.*?$")
}

if (sum(has) == 0) return(character(0))

res <- stri_replace_all_regex(res, "&amp;[LCR]", "")

# return the string vector
unname(res)
Expand Down
2 changes: 1 addition & 1 deletion R/openxlsx2-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' @import R6
#' @importFrom grDevices bmp col2rgb colors dev.copy dev.list dev.off jpeg palette png rgb tiff
#' @importFrom magrittr %>%
#' @importFrom stringi stri_c stri_encode stri_extract_all_regex
#' @importFrom stringi stri_c stri_encode stri_extract_all_regex stri_extract_first_regex
#' stri_isempty stri_join stri_match stri_match_all_regex stri_match_first_regex
#' stri_order stri_opts_collator stri_pad_left stri_rand_strings stri_read_lines
#' stri_replace_all_fixed stri_replace_all_regex stri_split_fixed
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-helper-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ test_that("amp_split & genHeaderFooterNode", {
got <- genHeaderFooterNode(got)
expect_equal(exp, got)

xml <- "<headerFooter alignWithMargins=\"0\"><oddFooter>&amp;L^ &amp;D +&amp;C&amp;R</oddFooter></headerFooter>"

exp <- list(oddFooter = c("^ &amp;D +", "", ""))
got <- getHeaderFooterNode(xml)
expect_equal(exp, got)

exp <- "<headerFooter differentOddEven=\"0\" differentFirst=\"0\" scaleWithDoc=\"0\" alignWithMargins=\"0\"><oddFooter>&amp;L^ &amp;D +</oddFooter></headerFooter>"
got <- genHeaderFooterNode(got)
expect_equal(exp, got)

})

test_that("add_sparklines", {
Expand Down

0 comments on commit cb0d24f

Please sign in to comment.