Skip to content

Commit c8ebff0

Browse files
committed
Documentation improvements
1 parent 10099d9 commit c8ebff0

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

Data/ByteString.hs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,13 @@ isValidUtf8 (BS ptr len) = accursedUnutterablePerformIO $ unsafeWithForeignPtr p
15581558

15591559
-- | Break a string on a substring, returning a pair of the part of the
15601560
-- string prior to the match, and the rest of the string.
1561+
-- If the substring is not found, return the entire string in the first component
1562+
-- and an empty string in the second component.
1563+
--
1564+
-- >>> breakSubstring "needle" "hayneedlestraw"
1565+
-- ("hay","needlestraw")
1566+
-- >>> breakSubstring "needle" "hay"
1567+
-- ("hay","")
15611568
--
15621569
-- The following relationships hold:
15631570
--
@@ -1576,7 +1583,7 @@ isValidUtf8 (BS ptr len) = accursedUnutterablePerformIO $ unsafeWithForeignPtr p
15761583
--
15771584
-- > fst (breakSubstring x y)
15781585
--
1579-
-- Note that calling `breakSubstring x` does some preprocessing work, so
1586+
-- Note that calling 'breakSubstring' @x@ does some preprocessing work, so
15801587
-- you should avoid unnecessarily duplicating breakSubstring calls with the same
15811588
-- pattern.
15821589
--
@@ -2017,8 +2024,7 @@ hGetContentsSizeHint hnd =
20172024
-- we grow the buffer sizes, but not too huge
20182025
-- we concatenate in the end anyway
20192026

2020-
-- | getContents. Read stdin strictly. Equivalent to hGetContents stdin
2021-
-- The 'Handle' is closed after the contents have been read.
2027+
-- | Equivalent to 'hGetContents' 'stdin', reading it /strictly/.
20222028
--
20232029
getContents :: IO ByteString
20242030
getContents = hGetContents stdin

Data/ByteString/Lazy.hs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,11 +1597,21 @@ illegalBufferSize handle fn sz =
15971597
msg = fn ++ ": illegal ByteString size " ++ showsPrec 9 sz []
15981598

15991599
-- | Read entire handle contents /lazily/ into a 'ByteString'. Chunks
1600-
-- are read on demand, using the default chunk size.
1600+
-- are read on demand, using the default chunk size ('defaultChunkSize').
16011601
--
16021602
-- File handles are closed on EOF if all the file is read, or through
16031603
-- garbage collection otherwise.
16041604
--
1605+
-- Do not use this function inside a 'Control.Exception.bracket' \/
1606+
-- 'System.IO.withFile' \/ 'System.IO.withBinaryFile':
1607+
-- lazy I/O will escape it causing 'Control.Exception.bracket' to close a handle prematurely:
1608+
--
1609+
-- >>> withBinaryFile "foo.txt" ReadMode BL.hGetContents >>= print
1610+
-- "*** Exception: foo.txt: hGetBufSome: illegal operation (handle is closed)
1611+
--
1612+
-- Consider using 'readFile' instead; if you must use 'hGetContents' then open a handle,
1613+
-- pass it to 'hGetContents' and leave it to close the handle when it sees fit.
1614+
--
16051615
hGetContents :: Handle -> IO ByteString
16061616
hGetContents = hGetContentsN defaultChunkSize
16071617

@@ -1644,7 +1654,7 @@ writeFile = modifyFile WriteMode
16441654
appendFile :: FilePath -> ByteString -> IO ()
16451655
appendFile = modifyFile AppendMode
16461656

1647-
-- | getContents. Equivalent to hGetContents stdin. Will read /lazily/
1657+
-- | Equivalent to 'hGetContents' 'stdin', reading it /lazily/.
16481658
--
16491659
getContents :: IO ByteString
16501660
getContents = hGetContents stdin

Data/ByteString/Lazy/Internal.hs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,14 @@ import Control.Exception (assert)
7878
-- 8-bit characters.
7979
--
8080
#ifndef HS_BYTESTRING_ASSERTIONS
81-
data ByteString = Empty | Chunk {-# UNPACK #-} !S.StrictByteString ByteString
82-
-- INVARIANT: The S.StrictByteString field of any Chunk is not empty.
83-
-- (See also the 'invariant' and 'checkInvariant' functions.)
81+
data ByteString
82+
= Empty
83+
| Chunk
84+
{-# UNPACK #-} !S.StrictByteString
85+
-- ^ Must be nonempty. Consider using
86+
-- a smart constructor 'chunk' to ensure this invariant.
87+
-- See also the 'invariant' and 'checkInvariant' functions.
88+
LazyByteString
8489

8590
-- To make testing of this invariant convenient, we add an
8691
-- assertion to that effect when the HS_BYTESTRING_ASSERTIONS

Data/ByteString/Short/Internal.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,13 @@ isSuffixOf sbs1 = \sbs2 -> do
12961296

12971297
-- | Break a string on a substring, returning a pair of the part of the
12981298
-- string prior to the match, and the rest of the string.
1299+
-- If the substring is not found, return the entire string in the first component
1300+
-- and an empty string in the second component.
1301+
--
1302+
-- >>> breakSubstring "needle" "hayneedlestraw"
1303+
-- ("hay","needlestraw")
1304+
-- >>> breakSubstring "needle" "hay"
1305+
-- ("hay","")
12991306
--
13001307
-- The following relationships hold:
13011308
--

0 commit comments

Comments
 (0)