File: indexOfNonQuoted.R

package info (click to toggle)
r-cran-r.rsp 0.45.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,568 kB
  • sloc: javascript: 612; tcl: 304; sh: 18; makefile: 16
file content (101 lines) | stat: -rw-r--r-- 2,796 bytes parent folder | download | duplicates (2)
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
###########################################################################/**
# @RdocDefault indexOfNonQuoted
#
# @title "Gets the first index of a string that is not inside a double quoted string"
#
# \description{
#  @get "title".
# }
#
# @synopsis
#
# \arguments{
#   \item{str}{The @character string to be scanned.}
#   \item{pattern}{The @character string to be searched for.}
#   \item{...}{Not used.}
# }
#
# \value{
#   Returns an @integer giving the position of (the first character of)
#   the search string in the main string.  If not found, -1 is returned.
# }
#
# @author
#
# \seealso{
#   @see "base::grep".
# }
#
# @keyword programming
# @keyword utilities
# @keyword internal
#*/###########################################################################
setMethodS3("indexOfNonQuoted", "default", function(str, pattern, ...) {
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # Validate arguments
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # Argument 'str':
  str <- as.character(str)

  # Argument 'pattern':
  pattern <- as.character(pattern)


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

    totalPos <- totalPos + pos

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

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

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

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

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

      # e. Exclude first (single or double) quoted string.
      if (qm == "'") {
        pattern <- "^[^']*'"
      } else {
        pattern <- "^[^\"]*\""
      }

      if (regexpr(pattern, str) != -1L) {
        str <- gsub(pattern, "", str)
        qm <- NULL
      }
    } # while (...)

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

    ready <- is.null(qm)
  } # while (!ready)

  # The found position
  pos <- as.integer(totalPos)
  attr(pos, "match.length") <- len

  pos
}, protected=TRUE) # indexOfNonQuoted()