Skip to content

Commit 23c20b5

Browse files
add exclude arg to half_slashes
1 parent b40e0b8 commit 23c20b5

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

r_regex_tester/helper_functions.R

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
library(data.table)
22

3-
half_slashes = function(str) {
3+
4+
half_slashes = function(str, exclude = character(0)) {
5+
#' Given a string, remove half of the escaping backslashes
6+
#'
7+
#' @param str A string to remove backslashes from
8+
#' @param exclude A character vector of items to exclude from processing
9+
#'
10+
#' @return A version of the input string with half of the escaping chars
11+
#'
12+
#' @details An exception will be thrown if removing slashes leads to a parsing
13+
#' error. For example, "\\" will be converted to "\" which is not
14+
#' a valid string.
15+
#'
16+
#' @examples
17+
#' half_slashes("\\\\")
18+
#' # [1] "\\"
419
deparsed = deparse(str)
520

621
half_df = data.table::as.data.table(
@@ -13,9 +28,14 @@ half_slashes = function(str) {
1328
half_df[, out := paste0(half_slash, char_cap)]
1429
half_df = unique(half_df[order(nchar(out)), ])
1530

16-
# Removing slashes before double quoutes breaks eval
31+
# Removing slashes before double quotes breaks eval
1732
half_df = half_df[char_cap != '"']
1833

34+
# Dirty fix for new lines...
35+
for (x in exclude) {
36+
half_df[match == x, out := x]
37+
}
38+
1939
halfed_deparse = mgsub(half_df$match,
2040
half_df$out,
2141
deparsed,

0 commit comments

Comments
 (0)