BS.getLine和CRLF结尾

时间:2014-03-14 22:50:57

标签: haskell newline bytestring

我正在尝试在使用BS.getLine时从行尾删除\ r \ n。我已尝试使用hSetNewlineMode,但它适用于getLine,但不适用于BS.getLine

import qualified Data.ByteString.Char8 as BS
import Data.ByteString (ByteString)
import System.IO (hSetNewlineMode, universalNewlineMode, stdin)

main = do
  hSetNewlineMode stdin universalNewlineMode
  -- s <- BS.pack `fmap` getLine   -- \r removed
  s <- BS.getLine                  -- \r not removed
  putStrLn $ show s

-- to test: perl -e 'print "this\r\n"' | runhaskell program.hs

我还应该做些什么吗?

1 个答案:

答案 0 :(得分:1)

查看BS.hGetLine的来源,我发现'\n'是硬编码的:

[...]
-- find the end-of-line character, if there is one
findEOL r w raw
    | r == w = return w
    | otherwise =  do
        (c,r') <- readCharFromBuffer raw r
        if c == '\n'
            then return r -- NB. not r': don't include the '\n'
            else findEOL r' w raw
[...]

如果我们希望将新行模式考虑在内,则必须将此帮助程序更改为使用提供的haInputNL中的Handle而不是硬编码值。我建议filing a bug report