Note: on windows, we expect a "wide char" encoding (e.g. UCS-2 or UTF-16). Anything
that works with Word16 boundaries. Picking an incompatible encoding may crash
filepath operations.
Like encodeUtf, except this mimics the behavior of the base library when doing filesystem
operations (usually filepaths), which is:
on unix, uses shady PEP 383 style encoding (based on the current locale,
but PEP 383 only works properly on UTF-8 encodings, so good luck)
on windows does permissive UTF-16 encoding, where coding errors generate
Chars in the surrogate range
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 encodeUtf, except this mimics the behavior of the base library when doing string
operations, which is:
on unix this uses getLocaleEncoding
on windows does permissive UTF-16 encoding, where coding errors generate
Chars in the surrogate range
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).
On windows this decodes as UTF16-LE (strictly), which is a pretty good guess.
On unix this decodes as UTF8 (strictly), which is a good guess. Note that
filenames on unix are encoding agnostic char arrays.
Like decodeUtf, except this mimics the behavior of the base library when doing filesystem
operations (usually filepaths), which is:
on unix, uses shady PEP 383 style encoding (based on the current locale,
but PEP 383 only works properly on UTF-8 encodings, so good luck)
on windows does permissive UTF-16 encoding, where coding errors generate
Chars in the surrogate range
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 decodeUtf, except this mimics the behavior of the base library when doing string operations,
which is:
on unix this uses getLocaleEncoding
on windows does permissive UTF-16 encoding, where coding errors generate
Chars in the surrogate range
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).
QuasiQuote an OsString. This accepts Unicode characters
and encodes as UTF-8 on unix and UTF-16 on windows.
If used as pattern, requires turning on the ViewPatterns extension.
Note that using this in conjunction with unsafeFromChar to
convert from [Char] to OsString is probably not what
you want, because it will truncate unicode code points.
O(n) The intercalate function takes a OsString and a list of
OsStrings and concatenates the list after interspersing the first
argument between each element of the list.
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.
foldl1 is a variant of foldl that has no starting value
argument, and thus must be applied to non-empty OsStrings.
An exception will be thrown in the case of an empty OsString.
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.
foldr1 is a variant of foldr that has no starting value argument,
and thus must be applied to non-empty OsStrings
An exception will be thrown in the case of an empty OsString.
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]
O(n) Like unfoldr, unfoldrN builds a OsString 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.
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 [""]
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.
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
O(n) The partition function takes a predicate a OsString and returns
the pair of OsStrings with elements which do and do not satisfy the
predicate, respectively; i.e.,
partition p bs == (filter p sbs, filter (not . p) sbs)
O(n) The elemIndex function returns the index of the first
element in the given OsString which is equal to the query
element, or Nothing if there is no such element.