Skip to content

CRLF with BlockBuffering not always correct #648

@neilmayhew

Description

@neilmayhew

The following test case produces different results with System.IO and Data.Text.IO:

import Control.Monad (when)
import Data.Text qualified as T
import Data.Text.IO qualified as T
import Data.ByteString.Char8 qualified as BS
import System.Directory (removeFile)
import System.IO

main :: IO ()
main = do
  let
    test putFunc value = do
      let tempFile = "test-data.txt"
      withFile tempFile WriteMode $ \h -> do
        hSetEncoding h utf8
        hSetNewlineMode h (NewlineMode CRLF CRLF)
        hSetBuffering h (BlockBuffering $ Just 8192)
        putFunc h value
      BS.readFile tempFile

    testStr = replicate 2047 '_'

  resultStr <- test hPutStrLn testStr
  resultTxt <- test T.hPutStrLn (T.pack testStr)

  when (resultStr /= resultTxt) $ do
    putStrLn "String:"
    print resultStr
    putStrLn "Text:"
    print resultTxt

The results are:

String:
"_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________\r\n"
Text:
"_______________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________\n"

Text is using "\n" when it should be using "\r\n"

This doesn't happen with a different text size (eg 2046, 2048) and it doesn't happen with text-2.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions