Finding matched parentheses
Geoff Canyon
gcanyon at gmail.com
Wed Jul 31 04:19:05 EDT 2013
On Tue, Jul 30, 2013 at 8:52 AM, Peter M. Brigham <pmbrig at gmail.com> wrote:
> function firstOffsetPair a,b,str
> -- from dfepstein
> -- str cannot be more than one line
> -- returns first instance of char a && "matching" instance of char b in
> str, or 0 if no a or empty if no match
> put offset(a,str) into ca
> if ca = 0 then return 0
> put numToChar(7) into char 1 to ca of str
> set lineDelimiter to a
> set itemDelimiter to b
> repeat with i = 1 to the number of items in str
> if i = the number of lines in item 1 to i of str then return
> ca+length(item 1 to i of str)
> end repeat
> return empty
> end firstOffsetPair
>
(you asked for feedback)
I haven't looked at the rest of the code you posted, but his function could
scale better by avoiding things like "the number of lines in item 1 to i of
str"
For comparison:
function offsetPair b,e,str
set the itemdelimiter to b
set the linedelimiter to e
put offset(b,str) into bi
if bi = 0 then return 0
put 0 into C
put 0 into LC
repeat for each line L in str
add 1 to LC
add the number of items of (L & b) - 2 to C
if C = 0 then return bi,(1+length(line 1 to LC of str))
end repeat
return empty
end offsetPair
That scales roughly linearly on the size of the input: a 20,000 character
string will take roughly twice as long as a similar 10,000 character
string. That means for strings around 30,000 characters long
firstOffsetPair can take 50x as long as offsetPair. Whoever first posted
code that used items and lines, thanks, that's quite clever.
regards,
geoff
More information about the use-livecode
mailing list