Finding matched parentheses

David Epstein dfepstein at comcast.net
Sun Jul 28 20:42:31 EDT 2013


Thanks for all the interesting replies to this query.  I used  
"offset" rather than (what most replies suggested) "repeat" with (or  
for) each character thinking it would be faster, but that seems to be  
true only if the string is extremely long and the sought for  
character very far from the front.  And I tried to match the first  
"(" rather than building a list of all matches.  There is no  
practical need for yet another approach, but in case others will find  
it of interest here is a speedy one that does not step through each  
character and barely uses offset.

David Epstein

function offsetPair a,b,str -- NOTE: a and b are single characters,  
str cannot be more than one line
-- in string str, locate the first a, and the nth b, where n = the  
number of a's that precede that b
-- e.g., if a = "("  and b = ")" the character positions of the first  
matched pair will be returned
-- return 0 if a is not found and empty if no match is found
   put offset(a,str) into ca
   if ca = 0 then return 0
   put numToChar(7) into char 1 to ca of str -- some char that's not  
a or b
   replace a with return in str
   put 1 into na -- number (ordinal) of a's, also the line count in str
   put 0 into nb
   repeat for each line k in str
     if k is not empty then -- does this line have (na-nb) instances  
of b?
       get nthHit(na-nb,b,k,z,numToChar(7))
       if it is not empty then return ca && it+ca+length(line 1 to  
na-1 of str)
       add z to nb
     end if
     add 1 to na
   end repeat
   return empty
end offsetPair

function nthHit n,a,str, at z
   -- returns character number of the nth instance of a (a single  
character) in str
   -- z reports the total number of hits
   put numToChar(7)  & str & numToChar(7)  into str   -- so str does  
not begin or end with a
   set itemDelimiter to a
   put -1 + the number of items in str into z
   if n > z then return empty
   return length(item 1 to n of str)
end nthHit




More information about the use-livecode mailing list