Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- data WindowsString
- data WindowsChar
- encodeUtf :: MonadThrow m => String -> m WindowsString
- unsafeEncodeUtf :: HasCallStack => String -> WindowsString
- encodeWith :: TextEncoding -> String -> Either EncodingException WindowsString
- encodeFS :: String -> IO WindowsString
- fromBytes :: MonadThrow m => ByteString -> m WindowsString
- pstr :: QuasiQuoter
- singleton :: WindowsChar -> WindowsString
- empty :: WindowsString
- pack :: [WindowsChar] -> WindowsString
- decodeUtf :: MonadThrow m => WindowsString -> m String
- decodeWith :: TextEncoding -> WindowsString -> Either EncodingException String
- decodeFS :: WindowsString -> IO String
- unpack :: WindowsString -> [WindowsChar]
- unsafeFromChar :: Char -> WindowsChar
- toChar :: WindowsChar -> Char
- snoc :: WindowsString -> WindowsChar -> WindowsString
- cons :: WindowsChar -> WindowsString -> WindowsString
- last :: HasCallStack => WindowsString -> WindowsChar
- tail :: HasCallStack => WindowsString -> WindowsString
- uncons :: WindowsString -> Maybe (WindowsChar, WindowsString)
- head :: HasCallStack => WindowsString -> WindowsChar
- init :: HasCallStack => WindowsString -> WindowsString
- unsnoc :: WindowsString -> Maybe (WindowsString, WindowsChar)
- null :: WindowsString -> Bool
- length :: WindowsString -> Int
- map :: (WindowsChar -> WindowsChar) -> WindowsString -> WindowsString
- reverse :: WindowsString -> WindowsString
- intercalate :: WindowsString -> [WindowsString] -> WindowsString
- foldl :: forall a. (a -> WindowsChar -> a) -> a -> WindowsString -> a
- foldl' :: forall a. (a -> WindowsChar -> a) -> a -> WindowsString -> a
- foldl1 :: (WindowsChar -> WindowsChar -> WindowsChar) -> WindowsString -> WindowsChar
- foldl1' :: (WindowsChar -> WindowsChar -> WindowsChar) -> WindowsString -> WindowsChar
- foldr :: forall a. (WindowsChar -> a -> a) -> a -> WindowsString -> a
- foldr' :: forall a. (WindowsChar -> a -> a) -> a -> WindowsString -> a
- foldr1 :: (WindowsChar -> WindowsChar -> WindowsChar) -> WindowsString -> WindowsChar
- foldr1' :: (WindowsChar -> WindowsChar -> WindowsChar) -> WindowsString -> WindowsChar
- all :: (WindowsChar -> Bool) -> WindowsString -> Bool
- any :: (WindowsChar -> Bool) -> WindowsString -> Bool
- concat :: [WindowsString] -> WindowsString
- replicate :: Int -> WindowsChar -> WindowsString
- unfoldr :: forall a. (a -> Maybe (WindowsChar, a)) -> a -> WindowsString
- unfoldrN :: forall a. Int -> (a -> Maybe (WindowsChar, a)) -> a -> (WindowsString, Maybe a)
- take :: Int -> WindowsString -> WindowsString
- takeEnd :: Int -> WindowsString -> WindowsString
- takeWhileEnd :: (WindowsChar -> Bool) -> WindowsString -> WindowsString
- takeWhile :: (WindowsChar -> Bool) -> WindowsString -> WindowsString
- drop :: Int -> WindowsString -> WindowsString
- dropEnd :: Int -> WindowsString -> WindowsString
- dropWhileEnd :: (WindowsChar -> Bool) -> WindowsString -> WindowsString
- dropWhile :: (WindowsChar -> Bool) -> WindowsString -> WindowsString
- break :: (WindowsChar -> Bool) -> WindowsString -> (WindowsString, WindowsString)
- breakEnd :: (WindowsChar -> Bool) -> WindowsString -> (WindowsString, WindowsString)
- span :: (WindowsChar -> Bool) -> WindowsString -> (WindowsString, WindowsString)
- spanEnd :: (WindowsChar -> Bool) -> WindowsString -> (WindowsString, WindowsString)
- splitAt :: Int -> WindowsString -> (WindowsString, WindowsString)
- split :: WindowsChar -> WindowsString -> [WindowsString]
- splitWith :: (WindowsChar -> Bool) -> WindowsString -> [WindowsString]
- stripSuffix :: WindowsString -> WindowsString -> Maybe WindowsString
- stripPrefix :: WindowsString -> WindowsString -> Maybe WindowsString
- isInfixOf :: WindowsString -> WindowsString -> Bool
- isPrefixOf :: WindowsString -> WindowsString -> Bool
- isSuffixOf :: WindowsString -> WindowsString -> Bool
- breakSubstring :: WindowsString -> WindowsString -> (WindowsString, WindowsString)
- elem :: WindowsChar -> WindowsString -> Bool
- find :: (WindowsChar -> Bool) -> WindowsString -> Maybe WindowsChar
- filter :: (WindowsChar -> Bool) -> WindowsString -> WindowsString
- partition :: (WindowsChar -> Bool) -> WindowsString -> (WindowsString, WindowsString)
- index :: HasCallStack => WindowsString -> Int -> WindowsChar
- indexMaybe :: WindowsString -> Int -> Maybe WindowsChar
- (!?) :: WindowsString -> Int -> Maybe WindowsChar
- elemIndex :: WindowsChar -> WindowsString -> Maybe Int
- elemIndices :: WindowsChar -> WindowsString -> [Int]
- count :: WindowsChar -> WindowsString -> Int
- findIndex :: (WindowsChar -> Bool) -> WindowsString -> Maybe Int
- findIndices :: (WindowsChar -> Bool) -> WindowsString -> [Int]
Types
data WindowsString Source #
Commonly used windows string as wide character bytes.
Instances
data WindowsChar Source #
Instances
String construction
encodeUtf :: MonadThrow m => String -> m WindowsString Source #
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
.
unsafeEncodeUtf :: HasCallStack => String -> WindowsString Source #
Unsafe unicode friendly encoding.
Like encodeUtf
, except it crashes when the input contains
surrogate chars. For sanitized input, this can be useful.
:: TextEncoding | text encoding (wide char) |
-> String | |
-> Either EncodingException WindowsString |
Encode a String
with the specified encoding.
Note: 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.
encodeFS :: String -> IO WindowsString Source #
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. This is safe to unsafePerformIO
/unsafeDupablePerformIO
.
fromBytes :: MonadThrow m => ByteString -> m WindowsString Source #
Constructs a platform string from a ByteString.
This ensures valid UCS-2LE. Note that this doesn't expand Word8 to Word16 on windows, so you may get invalid UTF-16.
Throws EncodingException
on invalid UCS-2LE (although unlikely).
pstr :: QuasiQuoter Source #
QuasiQuote a WindowsString
. This accepts Unicode characters
and encodes as UTF-16LE on windows.
singleton :: WindowsChar -> WindowsString Source #
pack :: [WindowsChar] -> WindowsString Source #
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
decodeUtf :: MonadThrow m => WindowsString -> m String Source #
Partial unicode friendly decoding.
This decodes as UTF16-LE (strictly), which is a pretty good.
Throws a EncodingException
if decoding fails.
decodeWith :: TextEncoding -> WindowsString -> Either EncodingException String Source #
Decode a WindowsString
with the specified encoding.
The String is forced into memory to catch all exceptions.
decodeFS :: WindowsString -> IO String Source #
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.
unpack :: WindowsString -> [WindowsChar] Source #
Unpack a platform string to a list of platform words.
Word construction
unsafeFromChar :: Char -> WindowsChar Source #
Truncates to 2 octets.
Word deconstruction
toChar :: WindowsChar -> Char Source #
Converts back to a unicode codepoint (total).
Basic interface
snoc :: WindowsString -> WindowsChar -> WindowsString Source #
O(n) Append a byte to the end of a OsString
Since: 1.4.200.0
cons :: WindowsChar -> WindowsString -> WindowsString Source #
O(n) cons
is analogous to (:) for lists.
Since: 1.4.200.0
last :: HasCallStack => WindowsString -> WindowsChar Source #
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.
Since: 1.4.200.0
tail :: HasCallStack => WindowsString -> WindowsString Source #
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.
Since: 1.4.200.0
uncons :: WindowsString -> Maybe (WindowsChar, WindowsString) Source #
head :: HasCallStack => WindowsString -> WindowsChar Source #
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.
Since: 1.4.200.0
init :: HasCallStack => WindowsString -> WindowsString Source #
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.
Since: 1.4.200.0
unsnoc :: WindowsString -> Maybe (WindowsString, WindowsChar) Source #
null :: WindowsString -> Bool Source #
O(1). The empty OsString
.
Since: 1.4.200.0
length :: WindowsString -> Int Source #
O(1) The length of a OsString
.
This returns the number of code units
(Word8
on unix and Word16
on windows), not
bytes.
>>>
length "abc"
3
Since: 1.4.200.0
Transforming OsString
map :: (WindowsChar -> WindowsChar) -> WindowsString -> WindowsString Source #
O(n) map
f xs
is the OsString obtained by applying f
to each
element of xs
.
Since: 1.4.200.0
reverse :: WindowsString -> WindowsString Source #
O(n) reverse
xs
efficiently returns the elements of xs
in reverse order.
Since: 1.4.200.0
intercalate :: WindowsString -> [WindowsString] -> WindowsString Source #
O(n) The intercalate
function takes a OsString
and a list of
OsString
s and concatenates the list after interspersing the first
argument between each element of the list.
Since: 1.4.200.0
Reducing OsStrings (folds)
foldl :: forall a. (a -> WindowsChar -> a) -> a -> WindowsString -> a Source #
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.
Since: 1.4.200.0
foldl' :: forall a. (a -> WindowsChar -> a) -> a -> WindowsString -> a Source #
foldl1 :: (WindowsChar -> WindowsChar -> WindowsChar) -> WindowsString -> WindowsChar Source #
foldl1' :: (WindowsChar -> WindowsChar -> WindowsChar) -> WindowsString -> WindowsChar Source #
foldr :: forall a. (WindowsChar -> a -> a) -> a -> WindowsString -> a Source #
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.
Since: 1.4.200.0
foldr' :: forall a. (WindowsChar -> a -> a) -> a -> WindowsString -> a Source #
foldr1 :: (WindowsChar -> WindowsChar -> WindowsChar) -> WindowsString -> WindowsChar Source #
foldr1' :: (WindowsChar -> WindowsChar -> WindowsChar) -> WindowsString -> WindowsChar Source #
Special folds
all :: (WindowsChar -> Bool) -> WindowsString -> Bool Source #
O(n) Applied to a predicate and a OsString
, all
determines
if all elements of the OsString
satisfy the predicate.
Since: 1.4.200.0
any :: (WindowsChar -> Bool) -> WindowsString -> Bool Source #
O(n) Applied to a predicate and a OsString
, any
determines if
any element of the OsString
satisfies the predicate.
Since: 1.4.200.0
concat :: [WindowsString] -> WindowsString Source #
Generating and unfolding OsStrings
replicate :: Int -> WindowsChar -> WindowsString Source #
O(n) replicate
n x
is a OsString of length n
with x
the value of every element. The following holds:
replicate w c = unfoldr w (\u -> Just (u,u)) c
Since: 1.4.200.0
unfoldr :: forall a. (a -> Maybe (WindowsChar, a)) -> a -> WindowsString Source #
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]
Since: 1.4.200.0
unfoldrN :: forall a. Int -> (a -> Maybe (WindowsChar, a)) -> a -> (WindowsString, Maybe a) Source #
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.
The following equation relates unfoldrN
and unfoldr
:
fst (unfoldrN n f s) == take n (unfoldr f s)
Since: 1.4.200.0
Substrings
Breaking strings
take :: Int -> WindowsString -> WindowsString Source #
takeEnd :: Int -> WindowsString -> WindowsString Source #
takeWhileEnd :: (WindowsChar -> Bool) -> WindowsString -> WindowsString Source #
Returns the longest (possibly empty) suffix of elements satisfying the predicate.
is equivalent to takeWhileEnd
p
.reverse
. takeWhile
p . reverse
Since: 1.4.200.0
takeWhile :: (WindowsChar -> Bool) -> WindowsString -> WindowsString Source #
Similar to takeWhile
,
returns the longest (possibly empty) prefix of elements
satisfying the predicate.
Since: 1.4.200.0
drop :: Int -> WindowsString -> WindowsString Source #
dropEnd :: Int -> WindowsString -> WindowsString Source #
dropWhileEnd :: (WindowsChar -> Bool) -> WindowsString -> WindowsString Source #
Similar to dropWhileEnd
,
drops the longest (possibly empty) suffix of elements
satisfying the predicate and returns the remainder.
is equivalent to dropWhileEnd
p
.reverse
. dropWhile
p . reverse
Since: 1.4.200.0
dropWhile :: (WindowsChar -> Bool) -> WindowsString -> WindowsString Source #
Similar to dropWhile
,
drops the longest (possibly empty) prefix of elements
satisfying the predicate and returns the remainder.
Since: 1.4.200.0
break :: (WindowsChar -> Bool) -> WindowsString -> (WindowsString, WindowsString) Source #
breakEnd :: (WindowsChar -> Bool) -> WindowsString -> (WindowsString, WindowsString) Source #
Returns the longest (possibly empty) suffix of elements which do not satisfy the predicate and the remainder of the string.
breakEnd
p
is equivalent to
and to spanEnd
(not . p)(
.takeWhileEnd
(not . p) &&& dropWhileEnd
(not . p))
Since: 1.4.200.0
span :: (WindowsChar -> Bool) -> WindowsString -> (WindowsString, WindowsString) Source #
spanEnd :: (WindowsChar -> Bool) -> WindowsString -> (WindowsString, WindowsString) Source #
Returns the longest (possibly empty) suffix of elements satisfying the predicate and the remainder of the string.
spanEnd
p
is equivalent to
and to breakEnd
(not . p)(
.takeWhileEnd
p &&& dropWhileEnd
p)
We have
spanEnd (not . isSpace) "x y z" == ("x y ", "z")
and
spanEnd (not . isSpace) sbs == let (x, y) = span (not . isSpace) (reverse sbs) in (reverse y, reverse x)
Since: 1.4.200.0
splitAt :: Int -> WindowsString -> (WindowsString, WindowsString) Source #
split :: WindowsChar -> WindowsString -> [WindowsString] Source #
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 . (==)
Since: 1.4.200.0
splitWith :: (WindowsChar -> Bool) -> WindowsString -> [WindowsString] Source #
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 [""]
Since: 1.4.200.0
stripSuffix :: WindowsString -> WindowsString -> Maybe WindowsString Source #
O(n) The stripSuffix
function takes two OsStrings and returns Just
the remainder of the second iff the first is its suffix, and otherwise
Nothing
.
Since: 1.4.200.0
stripPrefix :: WindowsString -> WindowsString -> Maybe WindowsString Source #
O(n) The stripPrefix
function takes two OsStrings and returns Just
the remainder of the second iff the first is its prefix, and otherwise
Nothing
.
Since: 1.4.200.0
Predicates
isInfixOf :: WindowsString -> WindowsString -> Bool Source #
Check whether one string is a substring of another.
Since: 1.4.200.0
isPrefixOf :: WindowsString -> WindowsString -> Bool Source #
O(n) The isPrefixOf
function takes two OsStrings and returns True
Since: 1.4.200.0
isSuffixOf :: WindowsString -> WindowsString -> Bool Source #
O(n) The isSuffixOf
function takes two OsStrings and returns True
iff the first is a suffix of the second.
The following holds:
isSuffixOf x y == reverse x `isPrefixOf` reverse y
Since: 1.4.200.0
Search for arbitrary susbstrings
breakSubstring :: WindowsString -> WindowsString -> (WindowsString, WindowsString) Source #
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.
Since: 1.4.200.0
Searching OsStrings
Searching by equality
elem :: WindowsChar -> WindowsString -> Bool Source #
O(n) elem
is the OsString
membership predicate.
Since: 1.4.200.0
find :: (WindowsChar -> Bool) -> WindowsString -> Maybe WindowsChar Source #
filter :: (WindowsChar -> Bool) -> WindowsString -> WindowsString Source #
O(n) filter
, applied to a predicate and a OsString,
returns a OsString containing those characters that satisfy the
predicate.
Since: 1.4.200.0
partition :: (WindowsChar -> Bool) -> WindowsString -> (WindowsString, WindowsString) Source #
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)
Since: 1.4.200.0
Indexing OsStrings
index :: HasCallStack => WindowsString -> Int -> WindowsChar Source #
O(1) OsString
index (subscript) operator, starting from 0.
Since: 1.4.200.0
indexMaybe :: WindowsString -> Int -> Maybe WindowsChar Source #
(!?) :: WindowsString -> Int -> Maybe WindowsChar Source #
elemIndex :: WindowsChar -> WindowsString -> Maybe Int Source #
elemIndices :: WindowsChar -> WindowsString -> [Int] Source #
O(n) The elemIndices
function extends elemIndex
, by returning
the indices of all elements equal to the query element, in ascending order.
Since: 1.4.200.0
count :: WindowsChar -> WindowsString -> Int Source #
count returns the number of times its argument appears in the OsString
Since: 1.4.200.0
findIndex :: (WindowsChar -> Bool) -> WindowsString -> Maybe Int Source #
O(n) The findIndex
function takes a predicate and a OsString
and
returns the index of the first element in the OsString
satisfying the predicate.
Since: 1.4.200.0
findIndices :: (WindowsChar -> Bool) -> WindowsString -> [Int] Source #
O(n) The findIndices
function extends findIndex
, by returning the
indices of all elements satisfying the predicate, in ascending order.
Since: 1.4.200.0