module Text.Format (
format
) where
import Data.List (foldl')
format :: String -> [String] -> String
format pattern = snd . foldl' replace (0, pattern)
where
replace :: (Int,String) -> String -> (Int,String)
replace (num,result) str = (num + 1, replaceSubstring (getFrom num) str result)
where
getFrom :: Int -> String
getFrom n = "{" ++ (show n) ++ "}"
replaceSubstring :: String -> String -> String -> String
replaceSubstring rFrom rTo rWhere = rss $ breakSubstring rFrom rWhere
where
rss (before,after)
| null after = before
| otherwise = before ++ rTo ++
(rss $ breakSubstring rFrom $ drop (length rFrom) after)
breakSubstring :: String -> String -> (String,String)
breakSubstring i_from i_str = doBreak i_from i_str ("","")
where
doBreak [] st (a,b) = (a, b ++ st)
doBreak _ [] res = res
doBreak (fx:fxs) (sx:sxs) (a,b)
| fx == sx = doBreak fxs sxs (a, b ++ [sx])
| otherwise = doBreak i_from sxs (a ++ b ++ [sx], "")