% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/locbalpar2.R
\name{locbalpar2}
\alias{locbalpar2}
\title{Return the locations of the matching parentheses}
\usage{
locbalpar2(expr, loc, line = 1, escape = 1, debug = 0)
}
\arguments{
\item{expr}{Input string vector.}

\item{loc}{The starting or ending locations of the parentheses
in \code{expr}. (See the details and examples below.)}

\item{line}{line number}

\item{escape}{= 0 not to escape parens with double backslashes.}

\item{debug}{= 1 to print intermediate results}
}
\value{
A list of \cr
\code{line}   the line number. \cr
\code{locbp}  the locations of matching parens in the line.
}
\description{
Return the locations of the matching parentheses
}
\details{
This is an expanded version of \code{lazy.tools::locbalpar}
which can handle a string vector input. \cr

When \code{expr} is a scalar string, better use \code{lazy.tools::locbalpar}.
\code{loc} must be positive and less than \code{sum(nchar(exp))}.

When \code{expr} is a string vector,
assuming that there are no length 0 elements of expr,
the location of the parens can be specified as follows:\cr
Method 1 (with \code{line} and \code{loc})\cr
Suppose that the location of the paren of interest is in location
(chracter) LOC at line LINE. Then the following code will find
the location of the matching paren. \cr
locbalpar2( expr, LOC, LINE ) \cr
Note that \code{loc} and \code{line} can be vectors of the same length.\cr

Method 2 (with \code{loc})\cr
In this case, \code{loc} must be specified so that \cr
\code{unlist(strsplit(expr,""))[loc]} \cr
contains opening or closing parens.

This function returns \code{line=0, locbp=0}.

The following characters are treated as parentheses: \cr
\code{"(", "{", "[", "<", ")", "}", "]", ">"}
}
\examples{

# When counting characters, count the length 0 character, "", as 1.
# For example, consider the example below where
# expr1 has length 0 character, "", at lines 2 and 4 and
# expr2 has length 1 blank character, " ",  at lines 2 and 4.
# The locations of parens for expr1 must be counted from expr2 as: \cr
#  \code{unlist(strsplit(expr2,""))}\cr
# The two results below should be the same.
expr1 <- c( "1(", "", "4)(", "", "89)" )
expr2 <- c( "1(", " ", "4)(", " ", "89)" )
printm( expr1, expr2 )
exprc1 <- unlist(strsplit(expr1,""))
exprc2 <- unlist(strsplit(expr2,""))
printm( exprc1, exprc2 )
locbalpar2( expr2, c(2,3,2), c(1,3,3) )
locbalpar2( expr1, c(2,6,5) )
locbalpar2( expr2, c(2,6,5) )

# escape or not
locbalpar2( "a(b\\\\(d)",2 )
locbalpar2( "a(b\\\\(d)",2, escape=0 )


# Find the matching closing brace.
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 ) )",
 " }",
 "",
 "} # end of locbalpar2"
)
locbalpar2( expr, regexpr("{", expr, fixed=1 )[1] )

# find all "(" and their matching ")".
locs <- gregexpr("(", expr,  fixed=TRUE )
LL <- NULL
for( i in 1:length(locs) ){
 if( all( locs[[i]] < 0 ) ) next
 LL <- rbind(LL,cbind(i,locs[[i]]))
}
locbalpar2(expr,LL[,2],LL[,1])



}
