1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
#' String interpolation
#'
#' @description
#' `r lifecycle::badge("superseded")`
#'
#' `str_interp()` is superseded in favour of [str_glue()].
#'
#' String interpolation is a useful way of specifying a character string which
#' depends on values in a certain environment. It allows for string creation
#' which is easier to read and write when compared to using e.g.
#' [paste()] or [sprintf()]. The (template) string can
#' include expression placeholders of the form `${expression}` or
#' `$[format]{expression}`, where expressions are valid R expressions that
#' can be evaluated in the given environment, and `format` is a format
#' specification valid for use with [sprintf()].
#'
#' @param string A template character string. This function is not vectorised:
#' a character vector will be collapsed into a single string.
#' @param env The environment in which to evaluate the expressions.
#' @seealso [str_glue()] and [str_glue_data()] for alternative approaches to
#' the same problem.
#' @keywords internal
#' @return An interpolated character string.
#' @author Stefan Milton Bache
#' @export
#' @examples
#'
#' # Using values from the environment, and some formats
#' user_name <- "smbache"
#' amount <- 6.656
#' account <- 1337
#' str_interp("User ${user_name} (account $[08d]{account}) has $$[.2f]{amount}.")
#'
#' # Nested brace pairs work inside expressions too, and any braces can be
#' # placed outside the expressions.
#' str_interp("Works with } nested { braces too: $[.2f]{{{2 + 2}*{amount}}}")
#'
#' # Values can also come from a list
#' str_interp(
#' "One value, ${value1}, and then another, ${value2*2}.",
#' list(value1 = 10, value2 = 20)
#' )
#'
#' # Or a data frame
#' str_interp(
#' "Values are $[.2f]{max(Sepal.Width)} and $[.2f]{min(Sepal.Width)}.",
#' iris
#' )
#'
#' # Use a vector when the string is long:
#' max_char <- 80
#' str_interp(c(
#' "This particular line is so long that it is hard to write ",
#' "without breaking the ${max_char}-char barrier!"
#' ))
str_interp <- function(string, env = parent.frame()) {
check_character(string)
string <- str_c(string, collapse = "")
# Find expression placeholders
matches <- interp_placeholders(string)
# Determine if any placeholders were found.
if (matches$indices[1] <= 0) {
string
} else {
# Evaluate them to get the replacement strings.
replacements <- eval_interp_matches(matches$matches, env)
# Replace the expressions by their values and return.
`regmatches<-`(string, list(matches$indices), FALSE, list(replacements))
}
}
#' Match String Interpolation Placeholders
#'
#' Given a character string a set of expression placeholders are matched. They
#' are of the form \code{${...}} or optionally \code{$[f]{...}} where `f`
#' is a valid format for [sprintf()].
#'
#' @param string character: The string to be interpolated.
#'
#' @return list containing `indices` (regex match data) and `matches`,
#' the string representations of matched expressions.
#'
#' @noRd
#' @author Stefan Milton Bache
interp_placeholders <- function(string, error_call = caller_env()) {
# Find starting position of ${} or $[]{} placeholders.
starts <- gregexpr("\\$(\\[.*?\\])?\\{", string)[[1]]
# Return immediately if no matches are found.
if (starts[1] <= 0) {
return(list(indices = starts))
}
# Break up the string in parts
parts <- substr(
rep(string, length(starts)),
start = starts,
stop = c(starts[-1L] - 1L, nchar(string))
)
# If there are nested placeholders, each part will not contain a full
# placeholder in which case we report invalid string interpolation template.
if (any(!grepl("\\$(\\[.*?\\])?\\{.+\\}", parts))) {
cli::cli_abort(
tr_("Invalid template string for interpolation."),
call = error_call
)
}
# For each part, find the opening and closing braces.
opens <- lapply(strsplit(parts, ""), function(v) which(v == "{"))
closes <- lapply(strsplit(parts, ""), function(v) which(v == "}"))
# Identify the positions within the parts of the matching closing braces.
# These are the lengths of the placeholder matches.
lengths <- mapply(match_brace, opens, closes)
# Update the `starts` match data with the
attr(starts, "match.length") <- lengths
# Return both the indices (regex match data) and the actual placeholder
# matches (as strings.)
list(
indices = starts,
matches = mapply(substr, starts, starts + lengths - 1, x = string)
)
}
#' Evaluate String Interpolation Matches
#'
#' The expression part of string interpolation matches are evaluated in a
#' specified environment and formatted for replacement in the original string.
#' Used internally by [str_interp()].
#'
#' @param matches Match data
#'
#' @param env The environment in which to evaluate the expressions.
#'
#' @return A character vector of replacement strings.
#'
#' @noRd
#' @author Stefan Milton Bache
eval_interp_matches <- function(matches, env, error_call = caller_env()) {
# Extract expressions from the matches
expressions <- extract_expressions(matches, error_call = error_call)
# Evaluate them in the given environment
values <- lapply(
expressions,
eval,
envir = env,
enclos = if (is.environment(env)) env else environment(env)
)
# Find the formats to be used
formats <- extract_formats(matches)
# Format the values and return.
mapply(sprintf, formats, values, SIMPLIFY = FALSE)
}
#' Extract Expression Objects from String Interpolation Matches
#'
#' An interpolation match object will contain both its wrapping \code{${ }} part
#' and possibly a format. This extracts the expression parts and parses them to
#' prepare them for evaluation.
#'
#' @param matches Match data
#'
#' @return list of R expressions
#'
#' @noRd
#' @author Stefan Milton Bache
extract_expressions <- function(matches, error_call = caller_env()) {
# Parse function for text argument as first argument.
parse_text <- function(text) {
withCallingHandlers(
parse(text = text),
error = function(e) {
cli::cli_abort(
tr_("Failed to parse input {.str {text}}"),
parent = e,
call = error_call
)
}
)
}
# string representation of the expressions (without the possible formats).
strings <- gsub("\\$(\\[.+?\\])?\\{", "", matches)
# Remove the trailing closing brace and parse.
lapply(substr(strings, 1L, nchar(strings) - 1), parse_text)
}
#' Extract String Interpolation Formats from Matched Placeholders
#'
#' An expression placeholder for string interpolation may optionally contain a
#' format valid for [sprintf()]. This function will extract such or
#' default to "s" the format for strings.
#'
#' @param matches Match data
#'
#' @return A character vector of format specifiers.
#'
#' @noRd
#' @author Stefan Milton Bache
extract_formats <- function(matches) {
# Extract the optional format parts.
formats <- gsub("\\$(\\[(.+?)\\])?.*", "\\2", matches)
# Use string options "s" as default when not specified.
paste0("%", ifelse(formats == "", "s", formats))
}
#' Utility Function for Matching a Closing Brace
#'
#' Given positions of opening and closing braces `match_brace` identifies
#' the closing brace matching the first opening brace.
#'
#' @param opening integer: Vector with positions of opening braces.
#'
#' @param closing integer: Vector with positions of closing braces.
#'
#' @return Integer with the posision of the matching brace.
#'
#' @noRd
#' @author Stefan Milton Bache
match_brace <- function(opening, closing) {
# maximum index for the matching closing brace
max_close <- max(closing)
# "path" for mapping opening and closing breaces
path <- numeric(max_close)
# Set openings to 1, and closings to -1
path[opening[opening < max_close]] <- 1
path[closing] <- -1
# Cumulate the path ...
cumpath <- cumsum(path)
# ... and the first 0 after the first opening identifies the match.
min(which(1:max_close > min(which(cumpath == 1)) & cumpath == 0))
}
|