SBS :: ByteArray# -> ShortByteString
Prior to bytestring-0.12 SBS was a genuine constructor of ShortByteString,
but now it is a bundled pattern synonym, provided as a compatibility shim.
A compact representation suitable for storing short byte strings in memory.
In typical use cases it can be imported alongside Data.ByteString, e.g.
import qualified Data.ByteString as B
import qualified Data.ByteString.Short as B
(ShortByteString, toShort, fromShort)Other ShortByteString operations clash with Data.ByteString or Prelude
functions however, so they should be imported qualified with a different
alias e.g.
import qualified Data.ByteString.Short as B.ShortShortByteString typeSBS :: ByteArray# -> ShortByteStringPrior to bytestring-0.12 SBS was a genuine constructor of ShortByteString,
but now it is a bundled pattern synonym, provided as a compatibility shim.
newtype ShortByteStringA compact representation of a Word8 vector.
It has a lower memory overhead than a ByteString and does not
contribute to heap fragmentation. It can be converted to or from a
ByteString (at the cost of copying the string data). It supports very few
other operations.
ShortByteString :: ByteArray -> ShortByteStringunShortByteString :: ShortByteString -> ByteArrayByteString unshared: 8 words; 32 or 64 bytes.
* tByteString shared substring: 4 words; 16 or 32 bytes.
* ShortByteString: 4 words; 16 or 32 bytes.
For the string data itself, both ShortByteString and tByteString use
one byte per element, rounded up to the nearest word. For example,
including the overheads, a length 10 ShortByteString would take
16 + 12 = 28 bytes on a 32bit platform and 32 + 16 = 48 bytes on a
64bit platform.
These overheads can all be reduced by 1 word (4 or 8 bytes) when the
ShortByteString or tByteString is unpacked into another constructor.
For example:
> data ThingId = ThingId {-# UNPACK #-} !Int
> {-# UNPACK #-} !ShortByteString
This will take 1 + 1 + 3 words (the ThingId constructor +
unpacked Int + unpacked ShortByteString), plus the words for the
string data.ByteString representation uses pinned memory,
meaning it cannot be moved by the GC. This is usually the right thing to
do for larger strings, but for small strings using pinned memory can
lead to heap fragmentation which wastes space. The ShortByteString
type (and the Text type from the text package) use unpinned memory
so they do not contribute to heap fragmentation. In addition, with GHC,
small unpinned strings are allocated in the same way as normal heap
allocations, rather than in a separate pinned area.ShortByteStringsempty :: ShortByteStringO(1). The empty ShortByteString.
singleton :: Word8 -> ShortByteStringO(1) Convert a Word8 into a ShortByteString
pack :: [Word8] -> ShortByteStringO(n). Convert a list into a ShortByteString
unpack :: ShortByteString -> [Word8]O(n). Convert a ShortByteString into a list.
fromShort :: ShortByteString -> ByteStringO(n). Convert a ShortByteString into a ByteString.
toShort :: ByteString -> ShortByteStringO(n). Convert a ByteString into a ShortByteString.
This makes a copy, so does not retain the input string.
snoc :: ShortByteString -> Word8 -> ShortByteStringO(n) Append a byte to the end of a ShortByteString
Note: copies the entire byte array
cons :: Word8 -> ShortByteString -> ShortByteStringO(n) cons is analogous to (:) for lists.
Note: copies the entire byte array
append :: ShortByteString -> ShortByteString -> ShortByteStringlast :: HasCallStack => ShortByteString -> Word8O(1) Extract the last element of a ShortByteString, which must be finite and non-empty. An exception will be thrown in the case of an empty ShortByteString.
This is a partial function, consider using unsnoc instead.
tail :: HasCallStack => ShortByteString -> ShortByteStringO(n) Extract the elements after the head of a ShortByteString, which must be non-empty. An exception will be thrown in the case of an empty ShortByteString.
This is a partial function, consider using uncons instead.
Note: copies the entire byte array
uncons :: ShortByteString -> Maybe (Word8, ShortByteString)O(n) Extract the head and tail of a ShortByteString, returning Nothing
if it is empty.
head :: HasCallStack => ShortByteString -> Word8O(1) Extract the first element of a ShortByteString, which must be non-empty. An exception will be thrown in the case of an empty ShortByteString.
This is a partial function, consider using uncons instead.
init :: HasCallStack => ShortByteString -> ShortByteStringO(n) Return all the elements of a ShortByteString except the last one.
An exception will be thrown in the case of an empty ShortByteString.
This is a partial function, consider using unsnoc instead.
Note: copies the entire byte array
unsnoc :: ShortByteString -> Maybe (ShortByteString, Word8)O(n) Extract the init and last of a ShortByteString, returning Nothing
if it is empty.
null :: ShortByteString -> BoolO(1) Test whether a ShortByteString is empty.
length :: ShortByteString -> IntO(1) The length of a ShortByteString.
isValidUtf8 :: ShortByteString -> BoolO(n) Check whether a ShortByteString represents valid UTF-8.
map :: (Word8 -> Word8) -> ShortByteString -> ShortByteStringO(n) map f xs is the ShortByteString obtained by applying f to each
element of xs.
reverse :: ShortByteString -> ShortByteStringO(n) reverse xs efficiently returns the elements of xs in reverse order.
intercalate :: ShortByteString -> [ShortByteString] -> ShortByteStringO(n) The intercalate function takes a ShortByteString and a list of
ShortByteStrings and concatenates the list after interspersing the first
argument between each element of the list.
ShortByteStrings (folds)foldl :: (a -> Word8 -> a) -> a -> ShortByteString -> afoldl, applied to a binary operator, a starting value (typically
the left-identity of the operator), and a ShortByteString, reduces the
ShortByteString using the binary operator, from left to right.
foldl' :: (a -> Word8 -> a) -> a -> ShortByteString -> afoldl' is like foldl, but strict in the accumulator.
foldl1 :: HasCallStack => (Word8 -> Word8 -> Word8) -> ShortByteString -> Word8foldl1 is a variant of foldl that has no starting value
argument, and thus must be applied to non-empty ShortByteStrings.
An exception will be thrown in the case of an empty ShortByteString.
foldl1' :: HasCallStack => (Word8 -> Word8 -> Word8) -> ShortByteString -> Word8foldl1' is like foldl1, but strict in the accumulator.
An exception will be thrown in the case of an empty ShortByteString.
foldr :: (Word8 -> a -> a) -> a -> ShortByteString -> afoldr, applied to a binary operator, a starting value
(typically the right-identity of the operator), and a ShortByteString,
reduces the ShortByteString using the binary operator, from right to left.
foldr' :: (Word8 -> a -> a) -> a -> ShortByteString -> afoldr' is like foldr, but strict in the accumulator.
foldr1 :: HasCallStack => (Word8 -> Word8 -> Word8) -> ShortByteString -> Word8foldr1 is a variant of foldr that has no starting value argument,
and thus must be applied to non-empty ShortByteStrings
An exception will be thrown in the case of an empty ShortByteString.
foldr1' :: HasCallStack => (Word8 -> Word8 -> Word8) -> ShortByteString -> Word8foldr1' is a variant of foldr1, but is strict in the
accumulator.
all :: (Word8 -> Bool) -> ShortByteString -> BoolO(n) Applied to a predicate and a ShortByteString, all determines
if all elements of the ShortByteString satisfy the predicate.
any :: (Word8 -> Bool) -> ShortByteString -> BoolO(n) Applied to a predicate and a ShortByteString, any determines if
any element of the ShortByteString satisfies the predicate.
concat :: [ShortByteString] -> ShortByteStringreplicate :: Int -> Word8 -> ShortByteStringO(n) replicate n x is a ShortByteString of length n with x
the value of every element. The following holds:
replicate w c = unfoldr w (\u -> Just (u,u)) cunfoldr :: (a -> Maybe (Word8, a)) -> a -> ShortByteStringO(n), where n is the length of the result. The unfoldr
function is analogous to the List 'unfoldr'. unfoldr builds a
ShortByteString from a seed value. The function takes the element and
returns Nothing if it is done producing the ShortByteString or returns
Just (a,b), in which case, a is the next byte in the string,
and b is the seed value for further production.
This function is not efficient/safe. It will build a list of [Word8]
and run the generator until it returns `Nothing`, otherwise recurse infinitely,
then finally create a ShortByteString.
If you know the maximum length, consider using unfoldrN.
Examples:
unfoldr (\x -> if x <= 5 then Just (x, x + 1) else Nothing) 0
== pack [0, 1, 2, 3, 4, 5]unfoldrN :: forall a. Int -> (a -> Maybe (Word8, a)) -> a -> (ShortByteString, Maybe a)O(n) Like unfoldr, unfoldrN builds a ShortByteString from a seed
value. However, the length of the result is limited by the first
argument to unfoldrN. This function is more efficient than unfoldr
when the maximum length of the result is known.
The following equation relates unfoldrN and unfoldr:
fst (unfoldrN n f s) == take n (unfoldr f s)take :: Int -> ShortByteString -> ShortByteStringO(n) take n, applied to a ShortByteString xs, returns the prefix
of xs of length n, or xs itself if n > .length xs
Note: copies the entire byte array
takeEnd :: Int -> ShortByteString -> ShortByteStringO(n) is equivalent to takeEnd n xs.
Takes drop (length xs - n) xsn elements from end of bytestring.
>>> takeEnd 3 "abcdefg"
"efg"
>>> takeEnd 0 "abcdefg"
""
>>> takeEnd 4 "abc"
"abc"takeWhileEnd :: (Word8 -> Bool) -> ShortByteString -> ShortByteStringReturns the longest (possibly empty) suffix of elements satisfying the predicate.
is equivalent to takeWhileEnd p.reverse . takeWhile p . reverse
takeWhile :: (Word8 -> Bool) -> ShortByteString -> ShortByteStringSimilar to takeWhile,
returns the longest (possibly empty) prefix of elements
satisfying the predicate.
drop :: Int -> ShortByteString -> ShortByteStringO(n) drop n xs returns the suffix of xs after the first n elements, or empty if n > .length xs
Note: copies the entire byte array
dropEnd :: Int -> ShortByteString -> ShortByteStringO(n) is equivalent to dropEnd n xs.
Drops take (length xs - n) xsn elements from end of bytestring.
>>> dropEnd 3 "abcdefg"
"abcd"
>>> dropEnd 0 "abcdefg"
"abcdefg"
>>> dropEnd 4 "abc"
""dropWhile :: (Word8 -> Bool) -> ShortByteString -> ShortByteStringSimilar to dropWhile,
drops the longest (possibly empty) prefix of elements
satisfying the predicate and returns the remainder.
Note: copies the entire byte array
dropWhileEnd :: (Word8 -> Bool) -> ShortByteString -> ShortByteStringSimilar to dropWhileEnd,
drops the longest (possibly empty) suffix of elements
satisfying the predicate and returns the remainder.
is equivalent to dropWhileEnd p.reverse . dropWhile p . reverse
breakEnd :: (Word8 -> Bool) -> ShortByteString -> (ShortByteString, ShortByteString)Returns the longest (possibly empty) suffix of elements which do not satisfy the predicate and the remainder of the string.
breakEnd p is equivalent to and to spanEnd (not . p)(.takeWhileEnd (not . p) &&& dropWhileEnd (not . p))
break :: (Word8 -> Bool) -> ShortByteString -> (ShortByteString, ShortByteString)Similar to break,
returns the longest (possibly empty) prefix of elements which do not
satisfy the predicate and the remainder of the string.
break p is equivalent to and to span (not . p)(.takeWhile (not . p) &&& dropWhile (not . p))
span :: (Word8 -> Bool) -> ShortByteString -> (ShortByteString, ShortByteString)Similar to span,
returns the longest (possibly empty) prefix of elements
satisfying the predicate and the remainder of the string.
span p is equivalent to and to break (not . p)(.takeWhile p &&& dropWhile p)
spanEnd :: (Word8 -> Bool) -> ShortByteString -> (ShortByteString, ShortByteString)Returns the longest (possibly empty) suffix of elements satisfying the predicate and the remainder of the string.
spanEnd p is equivalent to and to breakEnd (not . p)(.takeWhileEnd p &&& dropWhileEnd p)
We have
spanEnd (not . isSpace) "x y z" == ("x y ", "z")and
spanEnd (not . isSpace) sbs
==
let (x, y) = span (not . isSpace) (reverse sbs) in (reverse y, reverse x)splitAt :: Int -> ShortByteString -> (ShortByteString, ShortByteString)O(n) splitAt n sbs is equivalent to (.take n sbs, drop n sbs)
Note: copies the substrings
split :: Word8 -> ShortByteString -> [ShortByteString]O(n) Break a ShortByteString into pieces separated by the byte
argument, consuming the delimiter. I.e.
split 10 "a\nb\nd\ne" == ["a","b","d","e"] -- fromEnum '\n' == 10
split 97 "aXaXaXa" == ["","X","X","X",""] -- fromEnum 'a' == 97
split 120 "x" == ["",""] -- fromEnum 'x' == 120
split undefined "" == [] -- and not [""]and
intercalate [c] . split c == id
split == splitWith . (==)Note: copies the substrings
splitWith :: (Word8 -> Bool) -> ShortByteString -> [ShortByteString]O(n) Splits a ShortByteString into components delimited by
separators, where the predicate returns True for a separator element.
The resulting components do not contain the separators. Two adjacent
separators result in an empty component in the output. eg.
splitWith (==97) "aabbaca" == ["","","bb","c",""] -- fromEnum 'a' == 97
splitWith undefined "" == [] -- and not [""]stripSuffix :: ShortByteString -> ShortByteString -> Maybe ShortByteStringO(n) The stripSuffix function takes two ShortByteStrings and returns Just
the remainder of the second iff the first is its suffix, and otherwise
Nothing.
stripPrefix :: ShortByteString -> ShortByteString -> Maybe ShortByteStringO(n) The stripPrefix function takes two ShortByteStrings and returns Just
the remainder of the second iff the first is its prefix, and otherwise
Nothing.
isInfixOf :: ShortByteString -> ShortByteString -> BoolCheck whether one string is a substring of another.
isPrefixOf :: ShortByteString -> ShortByteString -> BoolO(n) The isPrefixOf function takes two ShortByteStrings and returns True
iff the first is a prefix of the second.
isSuffixOf :: ShortByteString -> ShortByteString -> BoolO(n) The isSuffixOf function takes two ShortByteStrings and returns True
iff the first is a suffix of the second.
The following holds:
isSuffixOf x y == reverse x `isPrefixOf` reverse ybreakSubstring :: ShortByteString -> ShortByteString -> (ShortByteString, ShortByteString)Break a string on a substring, returning a pair of the part of the string prior to the match, and the rest of the string.
The following relationships hold:
break (== c) l == breakSubstring (singleton c) lFor example, to tokenise a string, dropping delimiters:
tokenise x y = h : if null t then [] else tokenise x (drop (length x) t)
where (h,t) = breakSubstring x yTo skip to the first occurrence of a string:
snd (breakSubstring x y)To take the parts of a string before a delimiter:
fst (breakSubstring x y)Note that calling `breakSubstring x` does some preprocessing work, so you should avoid unnecessarily duplicating breakSubstring calls with the same pattern.
ShortByteStringString to search for
ShortByteStringString to search in
(ShortByteString, ShortByteString)Head and tail of string broken at substring
elem :: Word8 -> ShortByteString -> BoolO(n) elem is the ShortByteString membership predicate.
find :: (Word8 -> Bool) -> ShortByteString -> Maybe Word8O(n) The find function takes a predicate and a ShortByteString,
and returns the first element in matching the predicate, or Nothing
if there is no such element.
find f p = case findIndex f p of Just n -> Just (p ! n) ; _ -> Nothingfilter :: (Word8 -> Bool) -> ShortByteString -> ShortByteStringO(n) filter, applied to a predicate and a ShortByteString,
returns a ShortByteString containing those characters that satisfy the
predicate.
partition :: (Word8 -> Bool) -> ShortByteString -> (ShortByteString, ShortByteString)O(n) The partition function takes a predicate a ShortByteString and returns
the pair of ShortByteStrings with elements which do and do not satisfy the
predicate, respectively; i.e.,
partition p bs == (filter p sbs, filter (not . p) sbs)index :: HasCallStack => ShortByteString -> Int -> Word8O(1) ShortByteString index (subscript) operator, starting from 0.
This is a partial function, consider using indexMaybe instead.
indexMaybe :: ShortByteString -> Int -> Maybe Word8O(1) ShortByteString index, starting from 0, that returns Just if:
0 <= n < length bs(!?) :: ShortByteString -> Int -> Maybe Word8O(1) ShortByteString index, starting from 0, that returns Just if:
0 <= n < length bselemIndex :: Word8 -> ShortByteString -> Maybe IntO(n) The elemIndex function returns the index of the first
element in the given ShortByteString which is equal to the query
element, or Nothing if there is no such element.
elemIndices :: Word8 -> ShortByteString -> [Int]O(n) The elemIndices function extends elemIndex, by returning
the indices of all elements equal to the query element, in ascending order.
count :: Word8 -> ShortByteString -> Intcount returns the number of times its argument appears in the ShortByteString
findIndex :: (Word8 -> Bool) -> ShortByteString -> Maybe IntO(n) The findIndex function takes a predicate and a ShortByteString and
returns the index of the first element in the ShortByteString
satisfying the predicate.
findIndices :: (Word8 -> Bool) -> ShortByteString -> [Int]O(n) The findIndices function extends findIndex, by returning the
indices of all elements satisfying the predicate, in ascending order.
CStrings and pointerspackCString :: CString -> IO ShortByteStringO(n). Construct a new ShortByteString from a CString. The
resulting ShortByteString is an immutable copy of the original
CString, and is managed on the Haskell heap. The original
CString must be null terminated.
packCStringLen :: CStringLen -> IO ShortByteStringO(n). Construct a new ShortByteString from a CStringLen. The
resulting ShortByteString is an immutable copy of the original CStringLen.
The ShortByteString is a normal Haskell value and will be managed on the
Haskell heap.
CStringsuseAsCString :: ShortByteString -> (CString -> IO a) -> IO aO(n) construction. Use a ShortByteString with a function requiring a
null-terminated CString. The CString is a copy and will be freed
automatically; it must not be stored or used after the
subcomputation finishes.
useAsCStringLen :: ShortByteString -> (CStringLen -> IO a) -> IO aO(n) construction. Use a ShortByteString with a function requiring a CStringLen.
As for useAsCString this function makes a copy of the original ShortByteString.
It must not be stored or used after the subcomputation finishes.
Beware that this function does not add a terminating NUL byte at the end of CStringLen.
If you need to construct a pointer to a null-terminated sequence, use useAsCString
(and measure length independently if desired).
Community contribution. The service has not verified this documentation. The generator reported 11 diagnostics.
Target: Linux on x86-64. Some inferred types and resolved links are not available.
Generator: aihc-haddock-aa6debbd-web-3
Metadata snapshot: 35f1f56d3dce7f0c7af322ec05f2fba4d1a04564930537462a4ba21b64b756fa
Open the dependency plan · Download the documentation model