module Agda.Utils.String
( quote
, showIndex
, addFinalNewLine
, indent
, Str(..)
) where
import Data.List
import Numeric
quote :: String -> String
quote s = "\"" ++ concatMap escape s ++ "\""
where
escape c | c == '\n' = "\\n"
| c `elem` escapeChars = ['\\', c]
| otherwise = [c]
escapeChars = "\"\\"
showIndex :: (Show i, Integral i) => i -> String
showIndex n =
showIntAtBase 10 (\i -> toEnum (i + fromEnum '\x2080')) n ""
addFinalNewLine :: String -> String
addFinalNewLine "" = "\n"
addFinalNewLine s | last s == '\n' = s
| otherwise = s ++ "\n"
indent :: Integral i => i -> String -> String
indent i = unlines . map (genericReplicate i ' ' ++) . lines
newtype Str = Str { getStr :: String }
instance Show Str where
show = getStr