text-format-simple (empty) → 1.0.0
raw patch · 4 files changed
+102/−0 lines, 4 filesdep +basesetup-changed
Dependencies added: base
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- Text/Format.hs +47/−0
- text-format-simple.cabal +23/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c)2011, Dmitry + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of Dmitry nor the names of other + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ Text/Format.hs view
@@ -0,0 +1,47 @@+module Text.Format (+ format+ ) where++import Data.List (foldl')++{- |+Formats input string, using C\#-style.++First param is the input string in the form: \"Please, replace here {0} and here {1}\".++Second param is list of strings to put into {0}, {1} .. {N} positions.++Example: ++> format "Some {0} believes that 1 + 1 = {1}." ["people",show 10]++Result is:++> "Some people believes 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 ("","")+ 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], "")
+ text-format-simple.cabal view
@@ -0,0 +1,23 @@+Name: text-format-simple+Version: 1.0.0+License: BSD3+License-file: LICENSE+Category: Text+Author: Dmitry Bespalov+Maintainer: Dmitry Bespalov <bespalovdn@gmail.com>+Synopsis: Simple text formatting library.+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]+Build-Type: Simple+Cabal-Version: >= 1.8++library+ exposed-modules: Text.Format+ build-depends: base == 4.*+ ghc-options: -Wall++source-repository head+ type: darcs+ location: http://darcsden.com/dmitryb/text-format-simple