Question

Attoparsec provides the function skipSpace.

This function consumes all whitespace available.

How can I implement a function skipSpaceNoNewline that skips any whitespace except \n and \r\n?

Note: This question intentionally shows no research effort as it was answered Q&A-Style.

Was it helpful?

Solution

You can combine skipWhile and isEndOfLine (which matches both \n and \r\n).

Using a lambda function, you can combine them to a skipWhile predicate that skips any whitespace except newlines.

skipSpaceNoNewline = skipWhile (\x -> isSpace_w8 x && not (isEndOfLine x))

OTHER TIPS

why not just use skipSpaceNtoNewLine = filter (not.isSpace)?

Edit: I was wrong. isSpace is useless. I thought isSpace '\n' is False but its True. Just check for space directly:

[ghci] putStrLn $ filter (/=' ') $ ";df g; lkdfg\n l sd;lfk sdf"
;dfg;lkdfg
lsd;lfksdf

Or to make sure you filter the right things:

[ghci] putStrLn $ filter (\m-> m `notElem` "\t\f\v") $ ";df g; lk\vdfg\n l sd;lfk sdf"
;df g; lkdfg
 l sd;lfk sdf

So your function becomes:

skipSpaceNtoNewLine = filter  (\m-> m `notElem` "\t\f\v") 

Sorry about the confusion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top