HORIZON HASKELLDocslts/ghc-10.0.x77a86622026-10-31Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 77a8662 · 2026-10-31

Moduleos-string-2.0.11Haskell2010

System.OsString.Windows

Contents
  1. Types
  2. String construction
  3. String deconstruction
  4. Word construction
  5. Word deconstruction
  6. Basic interface
  7. Transforming OsString
  8. Reducing OsStrings (folds)
  9. Special folds
  10. Generating and unfolding OsStrings
  11. Substrings
  12. Breaking strings
  13. Predicates
  14. Search for arbitrary susbstrings
  15. Searching OsStrings
  16. Searching by equality
  17. Indexing OsStrings
Synopsis
  • Packageos-string-2.0.11
  • Exports82
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceTypes.hs

Types

newtypenewtype WindowsString

Commonly used windows string as wide character bytes.

Instances9Eq, Ord, Show, Generic, Semigroup, Monoid, …
newtypenewtype WindowsChar

This represents a "wide char" on windows.

A better name would maybe have been WindowsWord, since this is just Word16 in a UCS-2 sequence.

Instances7Eq, Ord, Show, Generic, NFData, Lift, …

String construction

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.

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.

Additionally, a platform word is a byte (or wide char on windows) in an encoded byte sequence, so we're not operating on unicode code points or graphemes here.

String deconstruction

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

Truncates to 2 octets.

Note that WindowsChar is a "wide char" in a UCS-2 encoded byte sequence and is not morally a unicode code point by any means, so this conversion is rarely what you want.

Consider using fromWord instead.

Word deconstruction

Basic interface

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(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

Reducing OsStrings (folds)

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

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

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

Searching by equality

Indexing OsStrings