Skip to content

Instantly share code, notes, and snippets.

@alanz
Created January 9, 2016 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanz/bb18c3aaafa7e7d3548b to your computer and use it in GitHub Desktop.
Save alanz/bb18c3aaafa7e7d3548b to your computer and use it in GitHub Desktop.
haskell code to clean up whitespace, replaces tabs with spaces and deletes trailing whitespace
-- |strip trailing whitespace, and turn tabs into spaces
-- Note: using Text as its append performance beats String
cleanupWhiteSpace :: FilePath -> IO T.Text
cleanupWhiteSpace file = do
contents <- T.readFile file
let fixed = map cleanupOneLine (T.lines contents)
return (T.unlines fixed)
tabWidth :: Int
tabWidth = 8
cleanupOneLine :: T.Text -> T.Text
cleanupOneLine str = str'
where
numSpacesForTab n = tabWidth - (n `mod` tabWidth)
-- loop over the line, keeping current pos. Where a tab is found, insert
-- spaces until the next tab stop. Discard any trailing whitespace.
go col res cur =
if T.null cur
then res
else
case T.head cur of
'\t' -> go (col + toAdd) (res <> T.replicate toAdd " ") (T.tail cur)
where
toAdd = numSpacesForTab col
c -> go (col + 1) (T.snoc res c) (T.tail cur)
str1 = go 0 T.empty str
str' = T.dropWhileEnd isSpace str1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment