packages feed

text-format-simple 1.0.0 → 1.1.0

raw patch · 2 files changed

+11/−30 lines, 2 filesdep +MissingH

Dependencies added: MissingH

Files

Text/Format.hs view
@@ -1,8 +1,9 @@+{-# OPTIONS_GHC -fno-warn-name-shadowing #-} module Text.Format (       format     ) where -import Data.List (foldl')+import Data.List.Utils (replace)  {- | Formats input string, using C\#-style.@@ -13,35 +14,15 @@  Example:  -> format "Some {0} believes that 1 + 1 = {1}." ["people",show 10]+> format "Some {0} think that 1 + 1 = {1}." ["people",show 10]  Result is: -> "Some people believes that 1 + 1 = 10."+> "Some people think that 1 + 1 = 10." -} 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) ++ "}"---- Replaces all occurences rFrom to rTo in rWhere.-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 ("","")+format a b = doFormat a (0::Int,b)     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], "")+    doFormat a (_,[]) = a+    doFormat a (n,(b:bs)) = replace (old n) b a `doFormat` (n+1,bs)+    old n = "{" ++ show n ++ "}"
text-format-simple.cabal view
@@ -1,5 +1,5 @@ Name:           text-format-simple-Version:        1.0.0+Version:        1.1.0 License:        BSD3 License-file:   LICENSE Category:       Text@@ -9,13 +9,13 @@ Description:    Tiny library dedicated for text formating in C# style. --              Sample of usage: --                  import Text.Format---                  format "Some {0} believes that 1 + 1 = {1}." ["people",show 10]+--                  format "Some {0} think that 1 + 1 = {1}." ["people",show 10] Build-Type:     Simple Cabal-Version:  >= 1.8  library     exposed-modules: Text.Format-    build-depends: base == 4.*+    build-depends: base == 4.*, MissingH     ghc-options: -Wall  source-repository head