I experienced a following problem while dealing with some text processing.

I have a text and want to get the same text with parts enclosed into {} or [] stripped away. Substituting them with a ' ' would also work.

Here is the code I wrote (T is Data.Text):

stripBrackets :: T.Text -> T.Text
stripBrackets text = snd $ T.mapAccumL f 0 text where
   f depth c = let
       depth' = depth + d' c
       c' | depth > 0 || depth' > 0 = ' '
          | otherwise = c
       in  
       (depth', c')

   d' '{' = 1
   d' '[' = 1
   d' '}' = -1
   d' ']' = -1
   d' _   = 0

The only problem is that it takes about a minute to complete on 3GHz+ processor when text is a 30k chars long.

Any ideas how to improve this code?

--
Regards, Petr