1
fast_replace = function(string, pattern, replacement, ...) {
2
  #' @title Fast escape replace
3
  #'
4
  #' @description Fast escape function for limited case where only one pattern
5
  #' provided actually matches anything
6
  #'
7
  #' @param string a character vector where replacements are sought
8
  #' @param pattern Character string to be matched in the given character vector
9
  #' @param replacement Character string equal in length to pattern or of length
10
  #' one which are a replacement for matched pattern.
11
  #' @param \dots arguments to pass to gsub
12

13 6
  for (i in seq_along(pattern)) {
14 6
    string = gsub(pattern[i], replacement[i], string, ...)
15
  }
16 6
  return(string)
17
}
18

19
get_matches = function(string, pattern, i, ...) {
20
  #' @title Get all matches
21
  #'
22
  #' @description Helper function to be used in a loop to check each pattern
23
  #' provided for matches
24
  #'
25
  #' @param string a character vector where replacements are sought
26
  #' @param pattern Character string to be matched in the given character vector
27
  #' @param i an iterator provided by a looping function
28
  #' @param \dots arguments to pass to gregexpr
29

30 6
  tmp = gregexpr(pattern[i], string, ...)
31 6
  start = tmp[[1]]
32 6
  length = attr(tmp[[1]], "match.length")
33 6
  return(matrix(cbind(i, start, length, start + length - 1), ncol = 4))
34
}
35

36
filter_overlap = function(x) {
37
  #' @title Filter overlaps from matches
38
  #'
39
  #' @description Helper function used to identify which results from gregexpr
40
  #' overlap other matches and filter out shorter, overlapped results
41
  #'
42
  #' @param x Matrix of gregexpr results, 4 columns, index of column matched,
43
  #' start of match, length of match, end of match. Produced exclusively from
44
  #' a worker function in mgsub
45 6
  for (i in nrow(x):2) {
46 6
    s = x[i, 2]
47 6
    ps = x[1:(i - 1), 2]
48 6
    e = x[i, 4]
49 6
    pe = x[1:(i - 1), 4]
50 6
    if (any(ps <= s & pe >= s)) {
51 6
      x = x[-i, ]
52 6
      next
53
    }
54 6
    if (any(ps <= e & pe >= e)) {
55 6
      x = x[-i, ]
56 6
      next
57
    }
58
  }
59 6
  return(matrix(x, ncol = 4))
60
}

Read our documentation on viewing source code .

Loading