-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstring_replacements.R
104 lines (99 loc) · 3.39 KB
/
string_replacements.R
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
# DIZtools - Utilities for 'DIZ' R Package Development
# Copyright (c) 2020-2025 Universitätsklinikum Erlangen, Germany
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#' @title Clean string with a given set of replacements.
#' @description This function provides the functionality to clean a string
#' with a given set of replacements. This is e.g. useful to create
#' filenames or paths that are not allowed to contain spaces.
#' @param input (string) The character string to be processed.
#' @param replace_mapping (Optional, list, default = "default") The mapping
#' containing what should be replaced with what:
#' `replace_mapping <- list("replace_this" = "with_this")`
#' @param tolower (boolean, default = FALSE) Should the result be lowercase?
#' @param toupper (boolean, default = FALSE) Should the result be uppercase?
#'
#' @return (String) All elements (names) of the input `replace_mapping` or
#' the default mapping are replaced by its values of the mapping.
#'
#' @examples
#' string_replacements(input = "Ab 20. April 2020 (((___((N = 1.234)")
#' # Result: "Ab_20_April_2020_N_1234"
#'
#' @export
#'
string_replacements <-
function(input,
replace_mapping = "default",
tolower = FALSE,
toupper = FALSE) {
res <- input
replaceme <- list(
# "replace_this" = "with_this",
"(" = "",
")" = "",
"{" = "",
"}" = "",
"." = "",
" " = "_",
"=" = "",
"__" = "_"
)
if (is.list(replace_mapping)) {
## Use the mapping handed over to the function:
replaceme <- replace_mapping
} else if (replace_mapping == "default") {
replaceme <- replaceme
} else {
DIZtools::feedback(
print_this = paste0(
"There is something handed over as `replace_mapping`",
" which is not a list. Using default replacement instead."
),
type = "Warning",
findme = "7c9d35cd88"
)
}
iteration <- 0
finished_changing <- FALSE
while (iteration < 1000 && !finished_changing) {
finished_changing <- TRUE
for (replace_this in names(replaceme)) {
res <-
gsub(
pattern = replace_this,
replacement = replaceme[[replace_this]],
x = res,
fixed = TRUE
)
}
for (replace_this in names(replaceme)) {
## Check if there is still something left to change:
if (grepl(pattern = replace_this,
x = res,
fixed = TRUE)) {
## There is a character left which needs to be changed,
## so this loop will be computed again.
finished_changing <- FALSE
}
}
iteration <- iteration + 1
}
if (tolower) {
res <- tolower(res)
} else if (toupper) {
res <- toupper(res)
}
return(res)
}