2

我试图在使用时从行尾删除 \r BS.getLine。我尝试过使用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

还有什么我应该做的吗?

4

1 回答 1

2

查看 的来源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我建议提交错误报告

于 2014-03-14T23:26:31.360 回答