diff --git a/NAMESPACE b/NAMESPACE index 0293053f2..f490121aa 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/helper-functions.R b/R/helper-functions.R index 26eea88db..defc60f8b 100644 --- a/R/helper-functions.R +++ b/R/helper-functions.R @@ -205,13 +205,28 @@ amp_split <- function(x) { res <- vector("character", 3) # Identify the names found in the string: returns them as matrix: strip the & nam <- gsub(pattern = "&", "", unlist(stri_match_all_regex(x, "&[LCR]"))) - # split the string and assign names to join - z <- unlist(stri_split_regex(x, "&[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 &[LCR] + if (grepl("&L", x)) { + has[1] <- 1 + res[1] <- stri_extract_first_regex(x, "&L.*?(?=&C|&R|$)") + } + if (grepl("&C", x)) { + has[2] <- 1 + res[2] <- stri_extract_first_regex(x, "&C.*?(?=&R|$)") + } + if (grepl("&R", x)) { + has[3] <- 1 + res[3] <- stri_extract_first_regex(x, "&R.*?$") + } + + if (sum(has) == 0) return(character(0)) + + res <- stri_replace_all_regex(res, "&[LCR]", "") # return the string vector unname(res) diff --git a/R/openxlsx2-package.R b/R/openxlsx2-package.R index 1188c3f94..e1651b9b4 100644 --- a/R/openxlsx2-package.R +++ b/R/openxlsx2-package.R @@ -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 diff --git a/tests/testthat/test-helper-functions.R b/tests/testthat/test-helper-functions.R index 9d831d052..ac968a746 100644 --- a/tests/testthat/test-helper-functions.R +++ b/tests/testthat/test-helper-functions.R @@ -119,6 +119,16 @@ test_that("amp_split & genHeaderFooterNode", { got <- genHeaderFooterNode(got) expect_equal(exp, got) + xml <- "&L^ &D +&C&R" + + exp <- list(oddFooter = c("^ &D +", "", "")) + got <- getHeaderFooterNode(xml) + expect_equal(exp, got) + + exp <- "&L^ &D +" + got <- genHeaderFooterNode(got) + expect_equal(exp, got) + }) test_that("add_sparklines", {