bmewing / mgsub
1
fastReplace = 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 15
  for(i in seq_along(pattern)){
14 15
    string = gsub(pattern[i],replacement[i],string,...)
15
  }
16 15
  return(string)
17
}
18

19
getMatches = 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 15
  tmp = gregexpr(pattern[i],string,...)
31 15
  start = tmp[[1]]
32 15
  length = attr(tmp[[1]],"match.length")
33 15
  return(matrix(cbind(i,start,length,start+length-1),ncol=4))
34
}
35

36
filterOverlap = 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 15
  for(i in nrow(x):2){
46 15
    s = x[i,2]
47 15
    ps = x[1:(i-1),2]
48 15
    e = x[i,4]
49 15
    pe = x[1:(i-1),4]
50 15
    if(any(ps <= s & pe >= s)){
51 15
      x = x[-i,]
52 15
      next
53
    }
54 15
    if(any(ps <= e & pe >= e)){
55 15
      x = x[-i,]
56 15
      next
57
    }
58
  }
59 15
  return(matrix(x,ncol=4))
60
}

Read our documentation on viewing source code .

Loading