HORIZON HASKELLDocslts/ghc-10.0.x8fef28d2026-11-02Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8fef28d · 2026-11-02

Moduleos-string-2.0.11Haskell2010

System.OsString.Data.ByteString.Short.Word16

ShortByteStrings encoded as UTF16-LE, suitable for windows FFI calls.

Word16s are *always* in BE encoding (both input and output), so e.g. pack takes a list of BE encoded [Word16] and produces a UTF16-LE encoded ShortByteString.

Likewise, unpack takes a UTF16-LE encoded ShortByteString and produces a list of BE encoded [Word16].

Indices and lengths are always in respect to Word16, not Word8.

All functions will error out if the input string is not a valid UTF16 stream (uneven number of bytes). So use this module with caution.

Contents
  1. The ShortByteString type and representation
  2. Introducing and eliminating ShortByteStrings
  3. Basic interface
  4. Transforming ShortByteStrings
  5. Reducing ShortByteStrings (folds)
  6. Special folds
  7. Generating and unfolding ByteStrings
  8. Substrings
  9. Breaking strings
  10. Predicates
  11. Search for arbitrary substrings
  12. Searching ShortByteStrings
  13. Searching by equality
  14. Searching with a predicate
  15. Indexing ShortByteStrings
  16. Encoding validation
  17. Low level conversions
  18. Packing CStrings and pointers
  19. Using ShortByteStrings as CStrings
Synopsis
  • Packageos-string-2.0.11
  • Exports76
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceWord16.hs

The ShortByteString type and representation

newtypenewtype ShortByteString

A 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.

Instances14IsList, Eq, Data, Ord, Read, Show, …

Introducing and eliminating ShortByteStrings

Basic interface

O(1) Extract the last element of a ShortByteString, which must be finite and at least one Word16. An exception will be thrown in the case of an empty ShortByteString.

O(n) Extract the elements after the head of a ShortByteString, which must at least one Word16. An exception will be thrown in the case of an empty ShortByteString.

Note: copies the entire byte array

O(1) Extract the first element of a ShortByteString, which must be at least one Word16. An exception will be thrown in the case of an empty ShortByteString.

Transforming ShortByteStrings

Reducing ShortByteStrings (folds)

valuefoldl :: (a -> Word16 -> a) -> a -> ShortByteString -> a

foldl, 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.

valuefoldr :: (Word16 -> a -> a) -> a -> ShortByteString -> a

foldr, 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.

Special folds

Generating and unfolding ByteStrings

valueunfoldr :: (a -> Maybe (Word16, a)) -> a -> ShortByteString

O(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 [Word16] and run the generator until it returns Nothing, otherwise recurse infinitely, then finally create a ShortByteString.

Examples:

   unfoldr (\x -> if x <= 5 then Just (x, x + 1) else Nothing) 0
== pack [0, 1, 2, 3, 4, 5]

Substrings

Breaking strings

O(1) takeEnd n xs is equivalent to drop (length xs - n) xs. Takes n elements from end of bytestring.

Example3 expressions
takeEnd 3 "a\NULb\NULc\NULd\NULe\NULf\NULg\NUL""e\NULf\NULg\NUL"takeEnd 0 "a\NULb\NULc\NULd\NULe\NULf\NULg\NUL"""takeEnd 4 "a\NULb\NULc\NUL""a\NULb\NULc\NUL"

O(1) dropEnd n xs is equivalent to take (length xs - n) xs. Drops n elements from end of bytestring.

Example3 expressions
dropEnd 3 "a\NULb\NULc\NULd\NULe\NULf\NULg\NUL""a\NULb\NULc\NULd\NUL"dropEnd 0 "a\NULb\NULc\NULd\NULe\NULf\NULg\NUL""a\NULb\NULc\NULd\NULe\NULf\NULg\NUL"dropEnd 4 "a\NULb\NULc\NUL"""

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

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 [""]

Predicates

Search for arbitrary substrings

Searching ShortByteStrings

Searching by equality

Searching with a predicate

O(n) The find function takes a predicate and a ByteString, 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) ; _ -> Nothing

Indexing ShortByteStrings

Encoding validation

Low level conversions

Packing CStrings and pointers

O(n). Construct a new ShortByteString from a CWString. The resulting ShortByteString is an immutable copy of the original CWString, and is managed on the Haskell heap. The original CWString must be null terminated.

O(n). Construct a new ShortByteString from a CWStringLen. The resulting ShortByteString is an immutable copy of the original CWStringLen. The ShortByteString is a normal Haskell value and will be managed on the Haskell heap.

O(n) construction. Use a ShortByteString with a function requiring a CWStringLen. As for useAsCWString this function makes a copy of the original ShortByteString. It must not be stored or used after the subcomputation finishes.

Using ShortByteStrings as CStrings

valueuseAsCWString :: ShortByteString -> (Ptr Word16 -> IO a) -> IO a

O(n) construction. Use a ShortByteString with a function requiring a null-terminated CWString. The CWString is a copy and will be freed automatically; it must not be stored or used after the subcomputation finishes.

valueuseAsCWStringLen :: ShortByteString -> ((Ptr Word16, Int) -> IO a) -> IO a

O(n) construction. Use a ShortByteString with a function requiring a CWStringLen. As for useAsCWString this function makes a copy of the original ShortByteString. It must not be stored or used after the subcomputation finishes.