On 05/26/2015 04:27 PM, Jeffery, Clint ([email protected]) wrote:
 > Regarding balancing multi-character strings, I hope someone finds a 
 > procedure you can use. You might also try the 
Icon mailing list.  Ralph Griswold was fond of writing replacements for 
built-ins in Icon, so a version of bal() written 
in Icon may very well be floating around.

I have the following in the UniLib package I wrote a while back,
but it relies on other parts of the UniLib package (specifically
the FindFirst class), so it'll take some adaptation to use without
UniLib.  Also not the last sentence of the intro comment...

-------------------------------------------------------
#<p>
# Match strings the same way bal() matches characters.
#   The input table is a tagged set of strings to match.  The key is
#   the <I>start</I> string while the value is the <I>end</I> string.
#   This makes sure the start and stop strings are balanced w.r.t
#   each other.
#   <[param keyStrings table of start->stop pairs]>
#   <[generates matching substrings]>
#</p>
#<p>
# For example, given the table:
#<pre>
#
#     t := table()
#     t["begin"] := "end"
#
#</pre>
#  then the code:
#<pre>
#
#     if match("begin") then
#        clause := sbal(t)
#
#</pre>
# assigns to <tt>clause</tt> the substring from <tt>begin</tt> through
# the matching <tt>end</tt>.  (Assuming, of course, that there are no
# conflicts along the way...)
#</p>
#<p>
# <b>This is an unoptimized preliminary version that may contain bugs.</b>
#</p>
procedure sbal(keyStrings)
     local startStrings, stopStrings, allStrings, w, ff, bCount

     every insert(startStrings := set(), key(keyStrings))
     every insert(stopStrings  := set(), !keyStrings)
     every put(allStrings := [], !(startStrings|stopStrings))

     ff := FindFirst(allStrings)
     inside := 0

     while tab(ff.locate()) do {
         p1 := &pos
         w := ff.moveMatch()
         if member(startStrings, w) then {
             if (inside +:= 1) = 1 then sPos := p1
             }
         else if member(stopStrings, w) then {
             if (inside -:= 1) = 0 then {
                 suspend .&subject[sPos:&pos]
                 }
             }
         if inside < 0 then fail
         }

     end
-------------------------------------------------------
-- 
Steve Wampler -- [email protected]
The gods that smiled on your birth are now laughing out loud.

------------------------------------------------------------------------------
_______________________________________________
Unicon-group mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/unicon-group

Reply via email to