ToString String and doctest.

This commit is contained in:
Kazu Yamamoto 2014-04-21 09:45:00 +09:00
parent c138f4bac8
commit e09b406ee8

View File

@ -53,10 +53,33 @@ class ToString a where
toLisp :: a -> String toLisp :: a -> String
toPlain :: a -> String toPlain :: a -> String
-- |
--
-- >>> toLisp "fo\"o"
-- "\"fo\\\"o\"\n"
-- >>> toPlain "foo"
-- "foo\n"
instance ToString String where
toLisp = addNewLine . quote
toPlain = addNewLine
-- |
--
-- >>> toLisp ["foo", "bar", "ba\"z"]
-- "(\"foo\" \"bar\" \"ba\\\"z\")\n"
-- >>> toPlain ["foo", "bar", "baz"]
-- "foo\nbar\nbaz\n"
instance ToString [String] where instance ToString [String] where
toLisp = addNewLine . toSexp True toLisp = addNewLine . toSexp True
toPlain = unlines toPlain = unlines
-- |
--
-- >>> let inp = [((1,2,3,4),"foo"),((5,6,7,8),"bar")] :: [((Int,Int,Int,Int),String)]
-- >>> toLisp inp
-- "((1 2 3 4 \"foo\") (5 6 7 8 \"bar\"))\n"
-- >>> toPlain inp
-- "1 2 3 4 \"foo\"\n5 6 7 8 \"bar\"\n"
instance ToString [((Int,Int,Int,Int),String)] where instance ToString [((Int,Int,Int,Int),String)] where
toLisp = addNewLine . toSexp False . map toS toLisp = addNewLine . toSexp False . map toS
where where
@ -72,7 +95,7 @@ tupToString ((a,b,c,d),s) = show a ++ " "
++ show b ++ " " ++ show b ++ " "
++ show c ++ " " ++ show c ++ " "
++ show d ++ " " ++ show d ++ " "
++ quote s ++ quote s -- fixme: quote is not necessary
quote :: String -> String quote :: String -> String
quote x = "\"" ++ escape x ++ "\"" quote x = "\"" ++ escape x ++ "\""