HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

  • Packageos-string-2.0.7
  • Exports80
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceTypes.hs

Types

2 declarations
newtypenewtype PosixString
#

Commonly used Posix string as uninterpreted char[] array.

Instances9Eq, Ord, Show, Generic, Semigroup, Monoid, …
newtypenewtype PosixChar
#
Instances6Eq, Ord, Show, Generic, NFData, Rep

String construction

11 declarations
valueencodeFS :: String -> IO PosixString
#

Deprecated. Use System.OsPath.Posix.encodeFS from filepath

This mimics the behavior of the base library when doing filesystem operations (usually filepaths), which uses shady PEP 383 style encoding (based on the current locale, but PEP 383 only works properly on UTF-8 encodings, so good luck).

Looking up the locale requires IO. If you're not worried about calls to setFileSystemEncoding, then unsafePerformIO may be feasible (make sure to deeply evaluate the result to catch exceptions).

valueencodeLE :: String -> IO PosixString
#

This mimics the behavior of the base library when doing string operations, which uses getLocaleEncoding.

Looking up the locale requires IO. If you're not worried about calls to setFileSystemEncoding, then unsafePerformIO may be feasible (make sure to deeply evaluate the result to catch exceptions).

Like fromBytes, but not in IO.

fromBytes was designed to have a symmetric type signature on unix and windows, but morally the function has no IO effects on unix, so we provide this variant without breaking existing API.

This function does not exist on windows.

valuepack :: [PosixChar] -> PosixString
#

Pack a list of platform words to a platform string.

Note that using this in conjunction with unsafeFromChar to convert from [Char] to platform string is probably not what you want, because it will truncate unicode code points.

String deconstruction

5 declarations
valuedecodeFS :: PosixString -> IO String
#

Deprecated. Use System.OsPath.Posix.decodeFS from filepath

This mimics the behavior of the base library when doing filesystem operations (usually filepaths), which uses shady PEP 383 style encoding (based on the current locale, but PEP 383 only works properly on UTF-8 encodings, so good luck).

Looking up the locale requires IO. If you're not worried about calls to setFileSystemEncoding, then unsafePerformIO may be feasible (make sure to deeply evaluate the result to catch exceptions).

valuedecodeLE :: PosixString -> IO String
#

This mimics the behavior of the base library when doing filesystem operations, which uses getLocaleEncoding.

Looking up the locale requires IO. If you're not worried about calls to setFileSystemEncoding, then unsafePerformIO may be feasible (make sure to deeply evaluate the result to catch exceptions).

Word construction

1 declaration

Word deconstruction

1 declaration

Basic interface

10 declarations

O(1) Extract the last element of a OsString, which must be finite and non-empty. An exception will be thrown in the case of an empty OsString.

This is a partial function, consider using unsnoc instead.

O(n) Extract the elements after the head of a OsString, which must be non-empty. An exception will be thrown in the case of an empty OsString.

This is a partial function, consider using uncons instead.

O(1) Extract the first element of a OsString, which must be non-empty. An exception will be thrown in the case of an empty OsString.

This is a partial function, consider using uncons instead.

O(n) Return all the elements of a OsString except the last one. An exception will be thrown in the case of an empty OsString.

This is a partial function, consider using unsnoc instead.

valuelength :: PosixString -> Int
#

O(1) The length of a OsString.

This returns the number of code units (Word8 on unix and Word16 on windows), not bytes.

Example1 expression
length "abc"3

Transforming OsString

3 declarations

Reducing OsStrings (folds)

8 declarations
valuefoldl :: (a -> PosixChar -> a) -> a -> PosixString -> a
#

foldl, applied to a binary operator, a starting value (typically the left-identity of the operator), and a OsString, reduces the OsString using the binary operator, from left to right.

valuefoldr :: (PosixChar -> a -> a) -> a -> PosixString -> a
#

foldr, applied to a binary operator, a starting value (typically the right-identity of the operator), and a OsString, reduces the OsString using the binary operator, from right to left.

Special folds

Generating and unfolding OsStrings

valueunfoldr :: (a -> Maybe (PosixChar, a)) -> a -> PosixString
#

O(n), where n is the length of the result. The unfoldr function is analogous to the List 'unfoldr'. unfoldr builds a OsString from a seed value. The function takes the element and returns Nothing if it is done producing the OsString 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 OsString.

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]

Substrings

0 declarations

Breaking strings

valuetakeEnd :: Int -> PosixString -> PosixString
#

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

Example3 expressions
takeEnd 3 "abcdefg""efg"takeEnd 0 "abcdefg"""takeEnd 4 "abc""abc"
valuedropEnd :: Int -> PosixString -> PosixString
#

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

Example3 expressions
dropEnd 3 "abcdefg""abcd"dropEnd 0 "abcdefg""abcdefg"dropEnd 4 "abc"""

O(n) Break a OsString 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 . (==)
valuesplitWith :: (PosixChar -> Bool) -> PosixString -> [PosixString]
#

O(n) Splits a OsString 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

3 declarations

Search for arbitrary susbstrings

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) l

For 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 y

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

Searching OsStrings

0 declarations

Searching by equality

valuefind :: (PosixChar -> Bool) -> PosixString -> Maybe PosixChar
#

O(n) The find function takes a predicate and a OsString, 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 OsStrings

8 declarations