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 WindowsString
#

Commonly used windows string as wide character bytes.

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

String construction

11 declarations
valueencodeUtf :: MonadThrow m => String -> m WindowsString
#

Partial unicode friendly encoding.

This encodes as UTF16-LE (strictly), which is a pretty good guess.

Throws an EncodingException if encoding fails. If the input does not contain surrogate chars, you can use unsafeEncodeUtf.

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

This mimics the behavior of the base library when doing filesystem operations (usually filepaths), which does permissive UTF-16 encoding, where coding errors generate Chars in the surrogate range.

The reason this is in IO is because it unifies with the Posix counterpart, which does require IO. This is safe to unsafePerformIO/unsafeDupablePerformIO.

This mimics the behavior of the base library when doing string operations, which does permissive UTF-16 encoding, where coding errors generate Chars in the surrogate range.

The reason this is in IO is because it unifies with the Posix counterpart, which does require IO. This is safe to unsafePerformIO/unsafeDupablePerformIO.

Like 'encodeLE but not in IO.

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

On windows, encodeLE is equivalent to encodeFS.

This function does not exist on unix.

valuepack :: [WindowsChar] -> WindowsString
#

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

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

Like decodeUtf, except this mimics the behavior of the base library when doing filesystem operations (usually filepaths), which does permissive UTF-16 encoding, where coding errors generate Chars in the surrogate range.

The reason this is in IO is because it unifies with the Posix counterpart, which does require IO. unsafePerformIO/unsafeDupablePerformIO are safe, however.

Like decodeUtf, except this mimics the behavior of the base library when doing filesystem operations, which does permissive UTF-16 encoding, where coding errors generate Chars in the surrogate range.

The reason this is in IO is because it unifies with the Posix counterpart, which does require IO. unsafePerformIO/unsafeDupablePerformIO are safe, however.

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 :: WindowsString -> 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 -> WindowsChar -> a) -> a -> WindowsString -> 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 :: (WindowsChar -> a -> a) -> a -> WindowsString -> 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 (WindowsChar, a)) -> a -> WindowsString
#

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

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 . (==)

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

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