Skip to content

Commit 2b9416d

Browse files
authored
Tweak documentation (#523)
1 parent 4e62154 commit 2b9416d

3 files changed

Lines changed: 39 additions & 23 deletions

File tree

Data/ByteString.hs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,8 @@ snoc (BS x l) c = unsafeCreate (l+1) $ \p -> unsafeWithForeignPtr x $ \f -> do
389389

390390
-- | /O(1)/ Extract the first element of a ByteString, which must be non-empty.
391391
-- An exception will be thrown in the case of an empty ByteString.
392+
--
393+
-- This is a partial function, consider using 'uncons' instead.
392394
head :: HasCallStack => ByteString -> Word8
393395
head (BS x l)
394396
| l <= 0 = errorEmptyList "head"
@@ -397,13 +399,15 @@ head (BS x l)
397399

398400
-- | /O(1)/ Extract the elements after the head of a ByteString, which must be non-empty.
399401
-- An exception will be thrown in the case of an empty ByteString.
402+
--
403+
-- This is a partial function, consider using 'uncons' instead.
400404
tail :: HasCallStack => ByteString -> ByteString
401405
tail (BS p l)
402406
| l <= 0 = errorEmptyList "tail"
403407
| otherwise = BS (plusForeignPtr p 1) (l-1)
404408
{-# INLINE tail #-}
405409

406-
-- | /O(1)/ Extract the head and tail of a ByteString, returning Nothing
410+
-- | /O(1)/ Extract the 'head' and 'tail' of a ByteString, returning 'Nothing'
407411
-- if it is empty.
408412
uncons :: ByteString -> Maybe (Word8, ByteString)
409413
uncons (BS x l)
@@ -415,6 +419,8 @@ uncons (BS x l)
415419

416420
-- | /O(1)/ Extract the last element of a ByteString, which must be finite and non-empty.
417421
-- An exception will be thrown in the case of an empty ByteString.
422+
--
423+
-- This is a partial function, consider using 'unsnoc' instead.
418424
last :: HasCallStack => ByteString -> Word8
419425
last ps@(BS x l)
420426
| null ps = errorEmptyList "last"
@@ -424,13 +430,15 @@ last ps@(BS x l)
424430

425431
-- | /O(1)/ Return all the elements of a 'ByteString' except the last one.
426432
-- An exception will be thrown in the case of an empty ByteString.
433+
--
434+
-- This is a partial function, consider using 'unsnoc' instead.
427435
init :: HasCallStack => ByteString -> ByteString
428436
init ps@(BS p l)
429437
| null ps = errorEmptyList "init"
430438
| otherwise = BS p (l-1)
431439
{-# INLINE init #-}
432440

433-
-- | /O(1)/ Extract the 'init' and 'last' of a ByteString, returning Nothing
441+
-- | /O(1)/ Extract the 'init' and 'last' of a ByteString, returning 'Nothing'
434442
-- if it is empty.
435443
unsnoc :: ByteString -> Maybe (ByteString, Word8)
436444
unsnoc (BS x l)
@@ -1227,6 +1235,8 @@ intercalate (BS fSepPtr sepLen) (BS fhPtr hLen : t) =
12271235
-- Indexing ByteStrings
12281236

12291237
-- | /O(1)/ 'ByteString' index (subscript) operator, starting from 0.
1238+
--
1239+
-- This is a partial function, consider using 'indexMaybe' instead.
12301240
index :: HasCallStack => ByteString -> Int -> Word8
12311241
index ps n
12321242
| n < 0 = moduleError "index" ("negative index: " ++ show n)

Data/ByteString/Lazy.hs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,14 @@ snoc cs w = foldrChunks Chunk (singleton w) cs
342342
{-# INLINE snoc #-}
343343

344344
-- | /O(1)/ Extract the first element of a ByteString, which must be non-empty.
345+
--
346+
-- This is a partial function, consider using 'uncons' instead.
345347
head :: HasCallStack => ByteString -> Word8
346348
head Empty = errorEmptyList "head"
347349
head (Chunk c _) = S.unsafeHead c
348350
{-# INLINE head #-}
349351

350-
-- | /O(1)/ Extract the head and tail of a ByteString, returning Nothing
352+
-- | /O(1)/ Extract the 'head' and 'tail' of a ByteString, returning 'Nothing'
351353
-- if it is empty.
352354
uncons :: ByteString -> Maybe (Word8, ByteString)
353355
uncons Empty = Nothing
@@ -358,6 +360,8 @@ uncons (Chunk c cs)
358360

359361
-- | /O(1)/ Extract the elements after the head of a ByteString, which must be
360362
-- non-empty.
363+
--
364+
-- This is a partial function, consider using 'uncons' instead.
361365
tail :: HasCallStack => ByteString -> ByteString
362366
tail Empty = errorEmptyList "tail"
363367
tail (Chunk c cs)
@@ -367,6 +371,8 @@ tail (Chunk c cs)
367371

368372
-- | /O(n\/c)/ Extract the last element of a ByteString, which must be finite
369373
-- and non-empty.
374+
--
375+
-- This is a partial function, consider using 'unsnoc' instead.
370376
last :: HasCallStack => ByteString -> Word8
371377
last Empty = errorEmptyList "last"
372378
last (Chunk c0 cs0) = go c0 cs0
@@ -375,14 +381,16 @@ last (Chunk c0 cs0) = go c0 cs0
375381
-- XXX Don't inline this. Something breaks with 6.8.2 (haven't investigated yet)
376382

377383
-- | /O(n\/c)/ Return all the elements of a 'ByteString' except the last one.
384+
--
385+
-- This is a partial function, consider using 'unsnoc' instead.
378386
init :: HasCallStack => ByteString -> ByteString
379387
init Empty = errorEmptyList "init"
380388
init (Chunk c0 cs0) = go c0 cs0
381389
where go c Empty | S.length c == 1 = Empty
382390
| otherwise = Chunk (S.unsafeInit c) Empty
383391
go c (Chunk c' cs) = Chunk c (go c' cs)
384392

385-
-- | /O(n\/c)/ Extract the 'init' and 'last' of a ByteString, returning Nothing
393+
-- | /O(n\/c)/ Extract the 'init' and 'last' of a ByteString, returning 'Nothing'
386394
-- if it is empty.
387395
--
388396
-- * It is no faster than using 'init' and 'last'
@@ -1123,6 +1131,8 @@ intercalate s = concat . List.intersperse s
11231131
-- Indexing ByteStrings
11241132

11251133
-- | /O(c)/ 'ByteString' index (subscript) operator, starting from 0.
1134+
--
1135+
-- This is a partial function, consider using 'indexMaybe' instead.
11261136
index :: HasCallStack => ByteString -> Int64 -> Word8
11271137
index _ i | i < 0 = moduleError "index" ("negative index: " ++ show i)
11281138
index cs0 i = index' cs0 i

Data/ByteString/Short/Internal.hs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ module Data.ByteString.Short.Internal (
8585
any,
8686
concat,
8787

88-
-- ** Generating and unfolding ByteStrings
88+
-- ** Generating and unfolding ShortByteStrings
8989
replicate,
9090
unfoldr,
9191
unfoldrN,
@@ -280,12 +280,6 @@ import qualified Language.Haskell.TH.Syntax as TH
280280
-- 'ByteString' (at the cost of copying the string data). It supports very few
281281
-- other operations.
282282
--
283-
-- It is suitable for use as an internal representation for code that needs
284-
-- to keep many short strings in memory, but it /should not/ be used as an
285-
-- interchange type. That is, it should not generally be used in public APIs.
286-
-- The 'ByteString' type is usually more suitable for use in interfaces; it is
287-
-- more flexible and it supports a wide range of operations.
288-
--
289283
data ShortByteString = SBS ByteArray#
290284
deriving Typeable
291285

@@ -373,6 +367,8 @@ null :: ShortByteString -> Bool
373367
null sbs = length sbs == 0
374368

375369
-- | /O(1)/ 'ShortByteString' index (subscript) operator, starting from 0.
370+
--
371+
-- This is a partial function, consider using 'indexMaybe' instead.
376372
index :: HasCallStack => ShortByteString -> Int -> Word8
377373
index sbs i
378374
| i >= 0 && i < length sbs = unsafeIndex sbs i
@@ -734,7 +730,7 @@ tail = \sbs ->
734730
True -> errorEmptySBS "tail"
735731
False -> create nl $ \mba -> copyByteArray (asBA sbs) 1 mba 0 nl
736732

737-
-- | /O(n)/ Extract the head and tail of a ByteString, returning Nothing
733+
-- | /O(n)/ Extract the 'head' and 'tail' of a ShortByteString, returning 'Nothing'
738734
-- if it is empty.
739735
--
740736
-- @since 0.11.3.0
@@ -774,7 +770,7 @@ init = \sbs ->
774770
True -> errorEmptySBS "init"
775771
False -> create nl $ \mba -> copyByteArray (asBA sbs) 0 mba 0 nl
776772

777-
-- | /O(n)/ Extract the 'init' and 'last' of a ByteString, returning Nothing
773+
-- | /O(n)/ Extract the 'init' and 'last' of a ShortByteString, returning 'Nothing'
778774
-- if it is empty.
779775
--
780776
-- @since 0.11.3.0
@@ -892,7 +888,7 @@ intercalate sep = \case
892888

893889

894890
-- ---------------------------------------------------------------------
895-
-- Reducing 'ByteString's
891+
-- Reducing 'ShortByteString's
896892

897893
-- | 'foldl', applied to a binary operator, a starting value (typically
898894
-- the left-identity of the operator), and a ShortByteString, reduces the
@@ -971,8 +967,8 @@ all k = \sbs ->
971967
in go 0
972968

973969

974-
-- | /O(n)/ Applied to a predicate and a ByteString, 'any' determines if
975-
-- any element of the 'ByteString' satisfies the predicate.
970+
-- | /O(n)/ Applied to a predicate and a 'ShortByteString', 'any' determines if
971+
-- any element of the 'ShortByteString' satisfies the predicate.
976972
--
977973
-- @since 0.11.3.0
978974
any :: (Word8 -> Bool) -> ShortByteString -> Bool
@@ -1235,7 +1231,7 @@ stripPrefix sbs1 = \sbs2 -> do
12351231
-- Unfolds and replicates
12361232

12371233

1238-
-- | /O(n)/ 'replicate' @n x@ is a ByteString of length @n@ with @x@
1234+
-- | /O(n)/ 'replicate' @n x@ is a ShortByteString of length @n@ with @x@
12391235
-- the value of every element. The following holds:
12401236
--
12411237
-- > replicate w c = unfoldr w (\u -> Just (u,u)) c
@@ -1430,8 +1426,8 @@ breakSubstring pat =
14301426
elem :: Word8 -> ShortByteString -> Bool
14311427
elem c = \sbs -> case elemIndex c sbs of Nothing -> False ; _ -> True
14321428

1433-
-- | /O(n)/ 'filter', applied to a predicate and a ByteString,
1434-
-- returns a ByteString containing those characters that satisfy the
1429+
-- | /O(n)/ 'filter', applied to a predicate and a ShortByteString,
1430+
-- returns a ShortByteString containing those characters that satisfy the
14351431
-- predicate.
14361432
--
14371433
-- @since 0.11.3.0
@@ -1460,7 +1456,7 @@ filter k = \sbs -> let l = length sbs
14601456
else
14611457
go' (br+1) bw
14621458

1463-
-- | /O(n)/ The 'find' function takes a predicate and a ByteString,
1459+
-- | /O(n)/ The 'find' function takes a predicate and a ShortByteString,
14641460
-- and returns the first element in matching the predicate, or 'Nothing'
14651461
-- if there is no such element.
14661462
--
@@ -1472,8 +1468,8 @@ find f = \sbs -> case findIndex f sbs of
14721468
Just n -> Just (sbs `index` n)
14731469
_ -> Nothing
14741470

1475-
-- | /O(n)/ The 'partition' function takes a predicate a ByteString and returns
1476-
-- the pair of ByteStrings with elements which do and do not satisfy the
1471+
-- | /O(n)/ The 'partition' function takes a predicate a ShortByteString and returns
1472+
-- the pair of ShortByteStrings with elements which do and do not satisfy the
14771473
-- predicate, respectively; i.e.,
14781474
--
14791475
-- > partition p bs == (filter p sbs, filter (not . p) sbs)
@@ -1539,7 +1535,7 @@ count w = \sbs@(SBS ba#) -> accursedUnutterablePerformIO $
15391535
fromIntegral <$> c_count ba# (fromIntegral $ length sbs) w
15401536

15411537
-- | /O(n)/ The 'findIndex' function takes a predicate and a 'ShortByteString' and
1542-
-- returns the index of the first element in the ByteString
1538+
-- returns the index of the first element in the ShortByteString
15431539
-- satisfying the predicate.
15441540
--
15451541
-- @since 0.11.3.0

0 commit comments

Comments
 (0)