My current implementation with Text.ParserCombinators.ReadP:
trim :: ReadP String
trim = do skipSpaces
content <- some get
skipSpaces
eof
return content
The problem is, ambiguous results frequently pop up when run:
ghci> readP_to_S trim " hello world "
[("hello world",""),("hello world ","")]
I believe I need to specify that the trimmed content will not end with space characters, but I have no idea how to express that apart from this extremely inefficient design:
trim :: ReadP String
trim = do skipSpaces
content <- many get
lastOne <- satisfy (not . isSpace)
skipSpaces
eof
return (content ++ [lastOne])
We can have better things, right?
Instead of trying to do this through parser combinatorics, why not just parse the content and then trim, like:
The parser can be shortened then to:
We thus first retrieve all characters, and then we return the trimmed string, although the
eofmight not be necessary here.