Skip to content

Commit

Permalink
Simplify import of utf8 and add a comment about pointer comparison in…
Browse files Browse the repository at this point in the history
… hPutStr
  • Loading branch information
Lysxia committed Jun 3, 2024
1 parent d176653 commit 2a5714e
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Data/Text/Internal/IO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ import GHC.IO.Buffer (Buffer(..), BufferState(..), CharBuffer, RawCharBuffer,
bufferAdjustL, bufferElems, charSize, emptyBuffer,
isEmptyBuffer, newCharBuffer, readCharBuf, withRawBuffer,
writeCharBuf)
import GHC.IO.Encoding (utf8)
import GHC.IO.Handle.Internals (ioe_EOF, readTextDevice, wantReadableHandle_,
wantWritableHandle)
import GHC.IO.Handle.Text (commitBuffer')
import GHC.IO.Handle.Types (BufferList(..), BufferMode(..), Handle__(..), Newline(..))
import System.IO (Handle, hPutChar)
import System.IO (Handle, hPutChar, utf8)
import System.IO.Error (isEOFError)
import qualified Data.Text as T

Expand Down Expand Up @@ -204,6 +203,10 @@ hPutStreamOrUtf8 h str mPutUtf8 = do
(BlockBuffering _, buf) -> writeBlocks (nl == CRLF) h buf str

where
-- If the encoding is UTF-8, it's most likely pointer-equal to
-- 'System.IO.utf8', letting us avoid a String comparison.
-- If it is somehow UTF-8 but not pointer-equal to 'utf8',
-- we will just take a slower branch, but the result is still correct.
eqUTF8 = maybe False (\enc -> isTrue# (reallyUnsafePtrEquality# utf8 enc)) . haCodec
{-# INLINE hPutStreamOrUtf8 #-}

Expand Down

0 comments on commit 2a5714e

Please sign in to comment.