Skip to content

Commit 2a5714e

Browse files
committed
Simplify import of utf8 and add a comment about pointer comparison in hPutStr
1 parent d176653 commit 2a5714e

File tree

1 file changed

+5
-2
lines changed
  • src/Data/Text/Internal

1 file changed

+5
-2
lines changed

src/Data/Text/Internal/IO.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,11 @@ import GHC.IO.Buffer (Buffer(..), BufferState(..), CharBuffer, RawCharBuffer,
4040
bufferAdjustL, bufferElems, charSize, emptyBuffer,
4141
isEmptyBuffer, newCharBuffer, readCharBuf, withRawBuffer,
4242
writeCharBuf)
43-
import GHC.IO.Encoding (utf8)
4443
import GHC.IO.Handle.Internals (ioe_EOF, readTextDevice, wantReadableHandle_,
4544
wantWritableHandle)
4645
import GHC.IO.Handle.Text (commitBuffer')
4746
import GHC.IO.Handle.Types (BufferList(..), BufferMode(..), Handle__(..), Newline(..))
48-
import System.IO (Handle, hPutChar)
47+
import System.IO (Handle, hPutChar, utf8)
4948
import System.IO.Error (isEOFError)
5049
import qualified Data.Text as T
5150

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

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

0 commit comments

Comments
 (0)