module Data.Text.Extended
( module Data.Text
, tabFilter
, joinLines
, lines'
) where
import Data.Text
import qualified Data.Text as T
tabFilter :: Int -> Text -> Text
tabFilter tabstop = T.concat . pad . T.split (== '\t')
where pad [] = []
pad [t] = [t]
pad (t:ts) = T.justifyLeft n ' ' t : pad ts
where tl = T.length t
n = tl + tabstop (tl `mod` tabstop)
joinLines :: [Text] -> Text
joinLines = T.intercalate "\n"
lines' :: Text -> [Text]
lines' ps | T.null ps = []
| otherwise = h : case T.uncons t of
Nothing -> []
Just (c,t')
| c == '\n' -> lines' t'
| c == '\r' -> case T.uncons t' of
Just ('\n',t'') -> lines' t''
_ -> lines' t'
_ -> error "lines': IMPOSSIBLE HAPPENED"
where (h,t) = T.span notEOL ps
notEOL c = c /= '\n' && c /= '\r'