locbalpar2 {lazy.tools} | R Documentation |
Return the locations of the matching parentheses
locbalpar2(expr, loc)
loc |
The starting or ending locations of the parentheses
in |
epr |
Input string vector. |
This is a expanded version of lazy.tools:locbalpar
which can handle a string vector input.
When expr
is a scalar string, use lazy.tools:locbalpar
.
loc
must be positive and less than sum(nchar(exp))
.
substring(exp,loc,loc)
must be opening or closing parens.
Otherwise, this function returns line=0, locbp=0
.
The following characters are treated as parentheses:
"(", "{", "[", "<", ")", "}", "]", ">"
A list of
line
the line number.
locbp
the locations of matching paren in the line.
# When counting characters, count the length 0 character as 1.
# For example, consider the example below where
# expr1 has length 0 characters at lines 2 and 4 and
# expr2 has length 1 blank characters at lines 2 and 4.
# The locations of parems must be counted from expr2, not expr1.
expr1 <- c( "1(", "", "4)(", "", "89)" )
expr2 <- c( "1(", " ", "4)(", " ", "89)" )
cat(" 1234567890123456789012345678901234567890\n")
num <- paste(1:length(expr1),": ", sep="")
cat(" "); cat(paste(num, expr1, ": ", nchar(expr1), "\n", sep=""))
cat(" 1234567890123456789012345678901234567890\n")
num <- paste(1:length(expr2),": ", sep="")
cat(" "); cat(paste(num, expr2, ": ", nchar(expr2), "\n", sep=""))
exprc1 <- unlist(strsplit(expr1,""))
exprc2 <- unlist(strsplit(expr2,""))
Print(1:length(exprc2),exprc1, exprc2)
locbalpar2( expr1, c(2,5,6,10) )
locbalpar2( expr2, c(2,5,6,10) )
exprc1 <- unlist(strsplit(expr1,""))
exprc2 <- unlist(strsplit(expr2,""))
Print(1:length(exprc2),exprc1, exprc2)
locbalpar2( expr1, c(2,5,6,10) )
locbalpar2( expr2, c(2,5,6,10) )
expr <- c(
"locbalpar2 <- function( expr, loc ){",
" # find the location of the balanced paren",
" # use standard version when expr is a string",
"",
" if( !is.vector(expr) ){",
" return( locbalpar( expr, loc ) )",
" }",
"",
"} # locbalpar2"
)
cat(" 1234567890123456789012345678901234567890\n")
cat(" "); cat(paste(paste(1:length(expr),": ", sep=""), expr,"\n", sep=""))
locbalpar2( expr, c(23,36, 184) )
#
# e1 <- unlist(strsplit(expr,""))
# expr2 <- sub("^$"," ",expr)
# e2 <- unlist(strsplit(expr2,""))
# Print(1:199, e1, e2)