From 2a5714e29c75df963ab8e2434b54f09e66e906b9 Mon Sep 17 00:00:00 2001 From: Li-yao Xia Date: Mon, 3 Jun 2024 17:22:29 +0200 Subject: [PATCH] Simplify import of utf8 and add a comment about pointer comparison in hPutStr --- src/Data/Text/Internal/IO.hs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Data/Text/Internal/IO.hs b/src/Data/Text/Internal/IO.hs index 1262be39..9badb3a6 100644 --- a/src/Data/Text/Internal/IO.hs +++ b/src/Data/Text/Internal/IO.hs @@ -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 @@ -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 #-}