From 1406d3a1349376eb128eccc6308b42a14ba83e97 Mon Sep 17 00:00:00 2001 From: William Rusnack Date: Sun, 5 May 2024 10:31:02 -0400 Subject: [PATCH] Inlined asserts --- src/Data/Text/IO.hs | 54 ++++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/src/Data/Text/IO.hs b/src/Data/Text/IO.hs index a4d39ff0..431fa2e1 100644 --- a/src/Data/Text/IO.hs +++ b/src/Data/Text/IO.hs @@ -1,6 +1,5 @@ {-# LANGUAGE BangPatterns, CPP, RecordWildCards, ScopedTypeVariables #-} {-# LANGUAGE Trustworthy #-} -{-# LANGUAGE NamedFieldPuns #-} -- | -- Module : Data.Text.IO -- Copyright : (c) 2009, 2010 Bryan O'Sullivan, @@ -56,8 +55,8 @@ import Data.Text.Internal.Fusion (stream) import Data.Text.Internal.Fusion.Types (Step(..), Stream(..)) import Data.Text.Internal.IO (hGetLineWith, readChunk) import GHC.IO.Buffer (Buffer(..), BufferState(..), CharBufElem, CharBuffer, - emptyBuffer, isEmptyBuffer, newCharBuffer) -import qualified GHC.IO.Buffer + RawCharBuffer, emptyBuffer, isEmptyBuffer, newCharBuffer, + writeCharBuf) import GHC.IO.Exception (IOException(ioe_type), IOErrorType(InappropriateType)) import GHC.IO.Handle.Internals (augmentIOError, hClose_help, wantReadableHandle, wantWritableHandle) @@ -210,9 +209,9 @@ hPutChars h (Stream next0 s0 _len) = loop s0 writeLines :: Handle -> Newline -> Buffer CharBufElem -> Stream Char -> IO () writeLines h nl buf0 (Stream next0 s0 _len) = outer s0 buf0 where - outer s1 buf@Buffer{bufSize=len} = inner s1 (0::Int) + outer s1 Buffer{bufRaw=raw, bufSize=len} = inner s1 (0::Int) where - inner !s !n = + inner !s !n = E.assert (n >= 0 && n < len) $ case next0 s of Done -> commit n False{-no flush-} True{-release-} >> return () Skip s' -> inner s' n @@ -220,47 +219,42 @@ writeLines h nl buf0 (Stream next0 s0 _len) = outer s0 buf0 | n + 1 >= len -> commit n True{-needs flush-} False >>= outer s | x == '\n' -> do n' <- if nl == CRLF - then do n1 <- writeCharBuf buf n '\r' - writeCharBuf buf n1 '\n' - else writeCharBuf buf n x + then do n1 <- writeCharBuf raw n '\r' + writeCharBuf raw n1 '\n' + else writeCharBuf raw n x commit n' True{-needs flush-} False >>= outer s' - | otherwise -> writeCharBuf buf n x >>= inner s' - commit = commitBuffer h buf + | otherwise -> writeCharBuf raw n x >>= inner s' + commit = commitBuffer h raw len writeBlocksCRLF :: Handle -> Buffer CharBufElem -> Stream Char -> IO () writeBlocksCRLF h buf0 (Stream next0 s0 _len) = outer s0 buf0 where - outer s1 buf@Buffer{bufSize=len} = inner s1 (0::Int) + outer s1 Buffer{bufRaw=raw, bufSize=len} = inner s1 (0::Int) where - inner !s !n = + inner !s !n = E.assert (n >= 0 && n < len) $ case next0 s of Done -> commit n False{-no flush-} True{-release-} >> return () Skip s' -> inner s' n Yield x s' | n + 1 >= len -> commit n True{-needs flush-} False >>= outer s - | x == '\n' -> do n1 <- writeCharBuf buf n '\r' - writeCharBuf buf n1 '\n' >>= inner s' - | otherwise -> writeCharBuf buf n x >>= inner s' - commit = commitBuffer h buf + | x == '\n' -> do n1 <- writeCharBuf raw n '\r' + writeCharBuf raw n1 '\n' >>= inner s' + | otherwise -> writeCharBuf raw n x >>= inner s' + commit = commitBuffer h raw len writeBlocksRaw :: Handle -> Buffer CharBufElem -> Stream Char -> IO () writeBlocksRaw h buf0 (Stream next0 s0 _len) = outer s0 buf0 where - outer s1 buf@Buffer{bufSize=len} = inner s1 (0::Int) + outer s1 Buffer{bufRaw=raw, bufSize=len} = inner s1 (0::Int) where - inner !s !n = + inner !s !n = E.assert (n >= 0 && n < len) $ case next0 s of Done -> commit n False{-no flush-} True{-release-} >> return () Skip s' -> inner s' n Yield x s' - | n >= len -> commit n True{-needs flush-} False >>= outer s - | otherwise -> writeCharBuf buf n x >>= inner s' - commit = commitBuffer h buf - --- | Only modifies the raw buffer and not the buffer attributes -writeCharBuf :: CharBuffer -> Int -> Char -> IO Int -writeCharBuf Buffer{bufRaw, bufSize} n c = E.assert (n >= 0 && n < bufSize) $ - GHC.IO.Buffer.writeCharBuf bufRaw n c + | n - 10 >= len -> commit n True{-needs flush-} False >>= outer s + | otherwise -> writeCharBuf raw n x >>= inner s' + commit = commitBuffer h raw len -- This function is completely lifted from GHC.IO.Handle.Text. getSpareBuffer :: Handle__ -> IO (BufferMode, CharBuffer) @@ -282,12 +276,12 @@ getSpareBuffer Handle__{haCharBuffer=ref, return (mode, new_buf) --- This function is modified from GHC.Internal.IO.Handle.Text. -commitBuffer :: Handle -> CharBuffer -> Int -> Bool -> Bool +-- This function is completely lifted from GHC.IO.Handle.Text. +commitBuffer :: Handle -> RawCharBuffer -> Int -> Int -> Bool -> Bool -> IO CharBuffer -commitBuffer hdl Buffer{bufRaw, bufSize} !count flush release = +commitBuffer hdl !raw !sz !count flush release = wantWritableHandle "commitAndReleaseBuffer" hdl $ - commitBuffer' bufRaw bufSize count flush release + commitBuffer' raw sz count flush release {-# INLINE commitBuffer #-} -- | Write a string to a handle, followed by a newline.