HenrikBengtsson / R.rsp
1
###########################################################################/**
2
# @RdocDefault indexOfNonQuoted
3
#
4
# @title "Gets the first index of a string that is not inside a double quoted string"
5
#
6
# \description{
7
#  @get "title".
8
# }
9
#
10
# @synopsis
11
#
12
# \arguments{
13
#   \item{str}{The @character string to be scanned.}
14
#   \item{pattern}{The @character string to be searched for.}
15
#   \item{...}{Not used.}
16
# }
17
#
18
# \value{
19
#   Returns an @integer giving the position of (the first character of)
20
#   the search string in the main string.  If not found, -1 is returned.
21
# }
22
#
23
# @author
24
#
25
# \seealso{
26
#   @see "base::grep".
27
# }
28
#
29
# @keyword programming
30
# @keyword utilities
31
# @keyword internal
32
#*/###########################################################################
33
setMethodS3("indexOfNonQuoted", "default", function(str, pattern, ...) {
34
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
35
  # Validate arguments
36
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
37
  # Argument 'str':
38 1
  str <- as.character(str)
39

40
  # Argument 'pattern':
41 1
  pattern <- as.character(pattern)
42

43

44 1
  totalPos <- 0L;     # The position from the start of the string
45 1
  len <- 0L;          # The default match length
46 1
  qm <- NULL;         # The current quotation mark of a string, if exists.
47 1
  ready <- FALSE
48 1
  while(!ready) {
49
    # Get the first occurance of pattern in buffer
50 1
    pos <- regexpr(pattern, str)
51 0
    if (pos == -1L) return(-1L)
52

53 1
    totalPos <- totalPos + pos
54

55 1
    tmp <- substring(str, first=1L, last=pos-1L)
56

57
    # a. Remove all espaced (doubled) backslashes, i.e. '\\'
58
    #    (A backslash has to be escaped in C gsub(), i.e. '\\'. Each of
59
    #    these two backslashes has in turn to be escaped in R doubleing
60
    #    the number of backslashes again!)
61 1
    tmp <- gsub("\\\\\\\\", "", tmp)
62

63
    # b. Remove all espaced quotes, i.e. '\"'.
64 1
    tmp <- gsub("\\\\[\"\']", "", tmp)
65

66
    # c. Remove all non quotes
67 1
    tmp <- gsub("[^'\"]", "", tmp)
68

69
    # d. Exclude all single or double quoted strings.
70 1
    while (nchar(tmp) > 0L) {
71 1
      if (is.null(qm)) {
72
        # d. Get first quotation mark
73 1
        qm <- substring(tmp, first=1L, last=1L)
74 1
        tmp <- substring(tmp, first=2L)
75
      }
76

77
      # e. Exclude first (single or double) quoted string.
78 1
      if (qm == "'") {
79 1
        pattern <- "^[^']*'"
80
      } else {
81 1
        pattern <- "^[^\"]*\""
82
      }
83

84 1
      if (regexpr(pattern, str) != -1L) {
85 1
        str <- gsub(pattern, "", str)
86 1
        qm <- NULL
87
      }
88 1
    } # while (...)
89

90 1
    len <- attr(pos, "match.length")
91 1
    str <- substring(str, first=pos+len)
92

93 1
    ready <- is.null(qm)
94 1
  } # while (!ready)
95

96
  # The found position
97 1
  pos <- as.integer(totalPos)
98 1
  attr(pos, "match.length") <- len
99

100 1
  pos
101
}, protected=TRUE) # indexOfNonQuoted()

Read our documentation on viewing source code .

Loading