Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
MCSP.Data.String
Description
Generic strings using backed by a contiguous array of unboxed characters.
Synopsis
- data String a where
- Unbox a => String !(Vector a)
- pattern Unboxed :: () => Unbox a => String a
- pattern Null :: () => Unbox a => String a
- pattern NonNull :: () => Unbox a => String a -> String a
- pattern Head :: () => Unbox a => a -> String a
- pattern Last :: () => Unbox a => a -> String a
- pattern Singleton :: () => Unbox a => a -> String a
- pattern (:<) :: () => Unbox a => a -> String a -> String a
- pattern (:>) :: () => Unbox a => String a -> a -> String a
- pattern (:<:) :: () => Unbox a => String a -> String a -> String a
- pattern (:>:) :: () => Unbox a => String a -> String a -> String a
- class (Vector Vector a, MVector MVector a) => Unbox a
- module MCSP.Data.String.Text
- length :: String a -> Int
- null :: String a -> Bool
- (!) :: String a -> Int -> a
- (!?) :: String a -> Int -> Maybe a
- head :: String a -> a
- last :: String a -> a
- unsafeIndex :: String a -> Int -> a
- single :: String a -> Maybe a
- indexM :: (Alternative m, Monad m) => String a -> Int -> m a
- headM :: (Alternative m, Monad m) => String a -> m a
- lastM :: (Alternative m, Monad m) => String a -> m a
- slice :: Int -> Int -> String a -> String a
- init :: String a -> String a
- tail :: String a -> String a
- take :: Int -> String a -> String a
- drop :: Int -> String a -> String a
- splitAt :: Int -> String a -> (String a, String a)
- uncons :: String a -> Maybe (a, String a)
- unsnoc :: String a -> Maybe (String a, a)
- unsafeSlice :: Int -> Int -> String a -> String a
- empty :: Unbox a => String a
- singleton :: Unbox a => a -> String a
- replicate :: Unbox a => Int -> a -> String a
- generate :: Unbox a => Int -> (Int -> a) -> String a
- replicateM :: (Unbox a, Monad m) => Int -> m a -> m (String a)
- generateM :: (Unbox a, Monad m) => Int -> (Int -> m a) -> m (String a)
- create :: Unbox a => (forall s. ST s (MVector s a)) -> String a
- unfoldr :: Unbox a => (b -> Maybe (a, b)) -> b -> String a
- unfoldrExactN :: Unbox a => Int -> (b -> (a, b)) -> b -> String a
- unfoldrM :: (Unbox a, Monad m) => (b -> m (Maybe (a, b))) -> b -> m (String a)
- unfoldrExactNM :: (Unbox a, Monad m) => Int -> (b -> m (a, b)) -> b -> m (String a)
- enumFromN :: (Unbox a, Num a) => a -> Int -> String a
- enumFromStepN :: (Unbox a, Num a) => a -> a -> Int -> String a
- cons :: a -> String a -> String a
- snoc :: String a -> a -> String a
- (++) :: String a -> String a -> String a
- concat :: Unbox a => [String a] -> String a
- concatNE :: NonEmpty (String a) -> String a
- force :: String a -> String a
- (//) :: String a -> [(Int, a)] -> String a
- update :: String a -> [Int] -> String a -> String a
- accum :: (a -> b -> a) -> String a -> [(Int, b)] -> String a
- accumulate :: (a -> b -> a) -> String a -> [Int] -> String b -> String a
- reverse :: String a -> String a
- backpermute :: String a -> [Int] -> String a
- modify :: (forall s. MVector s a -> ST s ()) -> String a -> String a
- indexed :: String a -> String (Int, a)
- map :: Unbox b => (a -> b) -> String a -> String b
- map_ :: (a -> a) -> String a -> String a
- imap :: Unbox b => (Int -> a -> b) -> String a -> String b
- imap_ :: (Int -> a -> a) -> String a -> String a
- concatMap :: Unbox b => (a -> [b]) -> String a -> String b
- concatMap_ :: (a -> String a) -> String a -> String a
- mapM :: (Monad m, Unbox b) => (a -> m b) -> String a -> m (String b)
- mapM_ :: Monad m => (a -> m a) -> String a -> m (String a)
- forM :: Monad m => String a -> (a -> m b) -> m ()
- iforM :: Monad m => String a -> (Int -> a -> m b) -> m ()
- zipWith :: Unbox c => (a -> b -> c) -> String a -> String b -> String c
- zipWith3 :: Unbox d => (a -> b -> c -> d) -> String a -> String b -> String c -> String d
- zip :: String a -> String b -> String (a, b)
- zip3 :: String a -> String b -> String c -> String (a, b, c)
- zipWithM :: (Monad m, Unbox c) => (a -> b -> m c) -> String a -> String b -> m (String c)
- zipWithM_ :: Monad m => (a -> b -> m c) -> String a -> String b -> m ()
- unzip :: (Unbox a, Unbox b) => String (a, b) -> (String a, String b)
- unzip3 :: (Unbox a, Unbox b) => String (a, b) -> (String a, String b)
- filter :: (a -> Bool) -> String a -> String a
- ifilter :: (Int -> a -> Bool) -> String a -> String a
- filterM :: Monad m => (a -> m Bool) -> String a -> m (String a)
- uniq :: Eq a => String a -> String a
- mapMaybe :: Unbox b => (a -> Maybe b) -> String a -> String b
- mapMaybeM :: (Monad m, Unbox b) => (a -> m (Maybe b)) -> String a -> m (String b)
- takeWhile :: (a -> Bool) -> String a -> String a
- dropWhile :: (a -> Bool) -> String a -> String a
- partition :: (a -> Bool) -> String a -> (String a, String a)
- partitionWith :: (Unbox b, Unbox c) => (a -> Either b c) -> String a -> (String b, String c)
- unstablePartition :: (a -> Bool) -> String a -> (String a, String a)
- span :: (a -> Bool) -> String a -> (String a, String a)
- break :: (a -> Bool) -> String a -> (String a, String a)
- groupBy :: (a -> a -> Bool) -> String a -> [String a]
- group :: Eq a => String a -> [String a]
- elem :: Eq a => a -> String a -> Bool
- notElem :: Eq a => a -> String a -> Bool
- find :: (a -> Bool) -> String a -> Maybe a
- findIndex :: (a -> Bool) -> String a -> Maybe Int
- findIndexR :: (a -> Bool) -> String a -> Maybe Int
- findIndices :: (a -> Bool) -> String a -> String Int
- elemIndex :: Eq a => a -> String a -> Maybe Int
- elemIndices :: Eq a => a -> String a -> String Int
- eqBy :: (a -> b -> Bool) -> String a -> String b -> Bool
- cmpBy :: (a -> b -> Ordering) -> String a -> String b -> Ordering
- convert :: Vector v a => String a -> v a
Unboxed string
An unboxed string of characters a
.
Implemented as a contiguous vector of unboxed characters.
Constructors
Unbox a => String !(Vector a) | Construct a string from a unboxed vector. Note that |
Bundled Patterns
pattern Unboxed :: () => Unbox a => String a | Proves This pattern is useful for matching in operations where
|
pattern Null :: () => Unbox a => String a | O(1) Matches the
|
pattern NonNull :: () => Unbox a => String a -> String a | O(1) Matches any non-
|
pattern Head :: () => Unbox a => a -> String a | O(1) Matches the first character in a string.
|
pattern Last :: () => Unbox a => a -> String a | O(1) Matches the last character in a string.
|
pattern Singleton :: () => Unbox a => a -> String a | O(1) Matches a string composed of a single character.
|
pattern (:<) :: () => Unbox a => a -> String a -> String a | O(1) Matches
|
pattern (:>) :: () => Unbox a => String a -> a -> String a | O(1) Matches
|
pattern (:<:) :: () => Unbox a => String a -> String a -> String a | O(1) Stringified
|
pattern (:>:) :: () => Unbox a => String a -> String a -> String a | O(1) Stringified
|
Instances
Foldable String Source # | |
Defined in MCSP.Data.String Methods fold :: Monoid m => String m -> m # foldMap :: Monoid m => (a -> m) -> String a -> m # foldMap' :: Monoid m => (a -> m) -> String a -> m # foldr :: (a -> b -> b) -> b -> String a -> b # foldr' :: (a -> b -> b) -> b -> String a -> b # foldl :: (b -> a -> b) -> b -> String a -> b # foldl' :: (b -> a -> b) -> b -> String a -> b # foldr1 :: (a -> a -> a) -> String a -> a # foldl1 :: (a -> a -> a) -> String a -> a # elem :: Eq a => a -> String a -> Bool # maximum :: Ord a => String a -> a # minimum :: Ord a => String a -> a # | |
NFData1 String Source # | |
Defined in MCSP.Data.String | |
Unbox a => Vector String a Source # | |
Defined in MCSP.Data.String Methods basicUnsafeFreeze :: Mutable String s a -> ST s (String a) basicUnsafeThaw :: String a -> ST s (Mutable String s a) basicLength :: String a -> Int basicUnsafeSlice :: Int -> Int -> String a -> String a basicUnsafeIndexM :: String a -> Int -> Box a basicUnsafeCopy :: Mutable String s a -> String a -> ST s () | |
a ~ Char => IsString (String a) Source # | |
Defined in MCSP.Data.String Methods fromString :: String0 -> String a # | |
Unbox a => Monoid (String a) Source # |
|
Semigroup (String a) Source # |
|
Unbox a => IsList (String a) Source # | |
(Unbox a, ReadString a) => Read (String a) Source # | |
ShowString a => Show (String a) Source # | |
NFData (String a) Source # | |
Defined in MCSP.Data.String | |
Eq a => Eq (String a) Source # | |
Ord a => Ord (String a) Source # | |
Defined in MCSP.Data.String | |
type Mutable String Source # | |
Defined in MCSP.Data.String type Mutable String | |
type Item (String a) Source # | |
Defined in MCSP.Data.String |
class (Vector Vector a, MVector MVector a) => Unbox a #
Instances
Text IO
module MCSP.Data.String.Text
Accessors
Length information
Indexing
(!?) :: String a -> Int -> Maybe a Source #
O(1) Safe indexing.
>>>
"abc" !? 1
Just 'b'
>>>
"abc" !? 3
Nothing
unsafeIndex :: String a -> Int -> a Source #
O(1) Unsafe indexing without bounds checking.
single :: String a -> Maybe a Source #
O(1) The character of a singleton string.
>>>
single ""
Nothing
>>>
single "x"
Just 'x'
>>>
single "xy"
Nothing
Substrings (slicing)
slice :: Int -> Int -> String a -> String a Source #
O(1) Yield the slice `s[i:i+n]` of the string without copying it.
The string must contain at least `i+n` characters.
>>>
slice 2 3 "genome"
nom
init :: String a -> String a Source #
O(1) Yield all but the last character without copying.
The string may not be empty.
>>>
init "genome"
genom
tail :: String a -> String a Source #
O(1) Yield all but the first character without copying.
The string may not be empty.
>>>
tail "genome"
enome
take :: Int -> String a -> String a Source #
O(1) Yield at the first n
characters without copying.
The string may contain less than n
characters, in which case it is returned unchanged.
>>>
take 2 "hello"
he
>>>
take 10 "hello"
hello
drop :: Int -> String a -> String a Source #
O(1) Yield all but the first n
characters without copying.
The string may contain less than n
characters, in which case an empty string is returned.
>>>
drop 2 "hello"
llo
>>>
drop 10 "hello"
splitAt :: Int -> String a -> (String a, String a) Source #
O(1) Yield the first n
characters paired with the remainder, without copying.
Note that `splitAt n s` is equivalent to `(take n s, drop n s)`, but slightly more efficient.
>>>
splitAt 6 "babushka"
(babush,ka)
unsafeSlice :: Int -> Int -> String a -> String a Source #
O(1) Yield a slice of the string without copying.
The string must contain at least `i+n` characters, but this is not checked.
Construction
Initialisation
singleton :: Unbox a => a -> String a Source #
O(1) A string with exactly one character.
>>>
singleton 's'
s
replicate :: Unbox a => Int -> a -> String a Source #
O(n) A string of the given length with the same character in each position.
>>>
replicate 10 'a'
aaaaaaaaaa
generate :: Unbox a => Int -> (Int -> a) -> String a Source #
O(n) Construct a string of the given length by applying the function to each index.
>>>
import Prelude (head, show)
>>>
generate 5 (\i -> Prelude.head (show i))
01234
Monadic initialisation
replicateM :: (Unbox a, Monad m) => Int -> m a -> m (String a) Source #
O(n) Execute the monadic action the given number of times and store the results in a string.
>>>
replicateM 4 (Just 'v')
Just vvvv
generateM :: (Unbox a, Monad m) => Int -> (Int -> m a) -> m (String a) Source #
O(n) Construct a string of the given length by applying the monadic action to each index.
>>>
generateM 5 (\i -> Just i)
Just 0 1 2 3 4
create :: Unbox a => (forall s. ST s (MVector s a)) -> String a Source #
O(f(n)) Execute the monadic action and freeze the resulting string.
>>>
import Control.Applicative (pure)
>>>
import Data.Vector.Unboxed.Mutable (new, write)
>>>
create (do v <- new 2; write v 0 'a'; write v 1 'b'; pure v)
ab
Unfolding
unfoldrExactN :: Unbox a => Int -> (b -> (a, b)) -> b -> String a Source #
O(n) Construct a vector with exactly n
characters by repeatedly applying the generator
function to a seed.
The generator function yields the next character and the new seed.
>>>
unfoldrExactN @Int 3 (\n -> (n, n-1)) 10
10 9 8
unfoldrExactNM :: (Unbox a, Monad m) => Int -> (b -> m (a, b)) -> b -> m (String a) Source #
O(n) Construct a string with exactly n
characters by repeatedly applying the generator
function to a seed.
The generator function yields the next character and the new seed.
>>>
unfoldrExactN @Int 3 (\n -> (n, n-1)) 10
10 9 8
Enumeration
enumFromN :: (Unbox a, Num a) => a -> Int -> String a Source #
O(n) Yield a string of the given length, containing the characters x
, x+1
etc.
This operation is usually more efficient than enumFromTo
.
>>>
enumFromN @Int 5 3
5 6 7
enumFromStepN :: (Unbox a, Num a) => a -> a -> Int -> String a Source #
O(n) Yield a string of the given length, containing the characters x
, x+y
, x+y+y
etc.
This operations is usually more efficient than enumFromThenTo
.
>>>
enumFromStepN @Int 1 2 5
1 3 5 7 9
Concatenation
(++) :: String a -> String a -> String a Source #
O(m+n) Concatenate two strings.
>>>
"abc" ++ "xyz"
abcxyz
concat :: Unbox a => [String a] -> String a Source #
O(n) Concatenate all strings in the list.
This is the simplest variant, but requires `Unbox a`.
>>>
concat ["abc", "123", "def"]
abc123def
>>>
concat @Char []
concatNE :: NonEmpty (String a) -> String a Source #
O(n) Concatenate all strings in the non-empty list.
>>>
concatNE ("abc" :| ["123", "def"])
abc123def
Restricting memory usage
force :: String a -> String a Source #
O(n) Yield the argument, but force it not to retain any extra memory, possibly by copying it.
See Data.Vector.Unbox.
Modifying vectors
Bulk updates
(//) :: String a -> [(Int, a)] -> String a Source #
O(m+n) For each pair `(i,a)` from the list of index/value pairs, replace the character at
position i
by a
.
>>>
"test" // [(2,'x'),(0,'y'),(2,'z')]
yezt
update :: String a -> [Int] -> String a -> String a Source #
O(m+min(n1,n2)) For each index i
from the index list and the corresponding value a from
another string, replace the character of the initial string at position i
by a
.
>>>
update "test" [2,0,2] "xyz"
yezt
Accumulations
accum :: (a -> b -> a) -> String a -> [(Int, b)] -> String a Source #
O(m+n) For each pair (i,b)
from the list, replace the character at position i
by f a b
.
>>>
import GHC.Num ((+))
>>>
accum @Int (+) [1000,2000,3000] [(2,4),(1,6),(0,3),(1,10)]
1003 2016 3004
accumulate :: (a -> b -> a) -> String a -> [Int] -> String b -> String a Source #
O(m+min(n1,n2)) For each index i
from the index list and the corresponding value b
from the string,
replace the character of the initial string at position i
by f a b
.
>>>
import GHC.Num ((+))
>>>
accumulate @Int (+) [5,9,2] [2,1,0,1] [4,6,3,7]
8 22 6
Permutations
backpermute :: String a -> [Int] -> String a Source #
O(n) Yield the string obtained by replacing each element i
of the index list by `xs!i`.
This is equivalent to `map (xs!)` is, but is often much more efficient.
>>>
backpermute "abcd" [0,3,2,3,1,0]
adcdba
Safe destructive updates
modify :: (forall s. MVector s a -> ST s ()) -> String a -> String a Source #
O(f(n)) Apply a destructive operation to a string.
The operation will be performed in place if it is safe to do so and will modify a copy of the vector otherwise.
>>>
import Data.Vector.Unboxed.Mutable (write)
>>>
modify (\v -> write v 3 'X') (replicate 10 'a')
aaaXaaaaaa
Elementwise operations
Indexing
indexed :: String a -> String (Int, a) Source #
O(n) Pair each character in a string with its index.
>>>
indexed "greedy"
(0,'g') (1,'r') (2,'e') (3,'e') (4,'d') (5,'y')
Mapping
map :: Unbox b => (a -> b) -> String a -> String b Source #
O(n) Map a function over a string.
>>>
import Data.Char (ord)
>>>
map ord "genome"
103 101 110 111 109 101
map_ :: (a -> a) -> String a -> String a Source #
O(n) Map an endofunction over a string.
>>>
import Data.Char (toUpper)
>>>
map_ toUpper "genome"
GENOME
imap :: Unbox b => (Int -> a -> b) -> String a -> String b Source #
O(n) Apply a function to every character of a string and its index.
>>>
import Data.Char (ord)
>>>
import GHC.Num ((+))
>>>
imap (\i c -> i + ord c) "genome"
103 102 112 114 113 106
imap_ :: (Int -> a -> a) -> String a -> String a Source #
O(n) Apply an endofunction to every character of a string and its index.
>>>
import Data.Char (chr, ord)
>>>
import GHC.Num ((+))
>>>
imap_ (\i c -> chr (i + ord c)) "genome"
gfprqj
concatMap :: Unbox b => (a -> [b]) -> String a -> String b Source #
O(?) Map a function over a string and concatenate the results.
>>>
concatMap (\c -> [c, c]) "genome"
ggeennoommee
concatMap_ :: (a -> String a) -> String a -> String a Source #
O(?) Map a function over a string and concatenate the resulting strings.
>>>
concatMap_ (\c -> replicate 3 c) "gen"
gggeeennn
mapM :: (Monad m, Unbox b) => (a -> m b) -> String a -> m (String b) Source #
O(n) Apply the monadic action to all characters of the string, yielding a string of results.
mapM_ :: Monad m => (a -> m a) -> String a -> m (String a) Source #
O(n) Apply the monadic action to all characters of the string, yielding a string of results.
forM :: Monad m => String a -> (a -> m b) -> m () Source #
O(n) Apply the monadic action to all characters of a string and ignore the results.
iforM :: Monad m => String a -> (Int -> a -> m b) -> m () Source #
O(n) Apply the monadic action to all characters of a string and their indices and ignore the results.
Zipping
zipWith :: Unbox c => (a -> b -> c) -> String a -> String b -> String c Source #
O(min(m,n)) Zip two strings with the given function.
zipWith3 :: Unbox d => (a -> b -> c -> d) -> String a -> String b -> String c -> String d Source #
O(min(m,n,k)) Zip three strings with the given function.
zipWithM :: (Monad m, Unbox c) => (a -> b -> m c) -> String a -> String b -> m (String c) Source #
O(min(m,n)) Zip the two strings with the monadic action and yield a vector of results.
zipWithM_ :: Monad m => (a -> b -> m c) -> String a -> String b -> m () Source #
O(min(m,n)) Zip the two strings with the monadic action and ignore the results.
unzip :: (Unbox a, Unbox b) => String (a, b) -> (String a, String b) Source #
O(n) Unzip a string of pairs.
unzip3 :: (Unbox a, Unbox b) => String (a, b) -> (String a, String b) Source #
O(n) Unzip a string of triples.
Working with predicates
Filtering
filter :: (a -> Bool) -> String a -> String a Source #
O(n) Drop all characters that do not satisfy the predicate.
>>>
import Data.Char (isUpper)
>>>
filter isUpper "ABCdefGHI"
ABCGHI
ifilter :: (Int -> a -> Bool) -> String a -> String a Source #
O(n) Drop all characters that do not satisfy the predicate which is applied to the values and their indices.
filterM :: Monad m => (a -> m Bool) -> String a -> m (String a) Source #
O(n) Drop all characters that do not satisfy the monadic predicate.
uniq :: Eq a => String a -> String a Source #
O(n) Drop repeated adjacent characters.
>>>
uniq "aaaabbbcccaabc"
abcabc
mapMaybe :: Unbox b => (a -> Maybe b) -> String a -> String b Source #
O(n) Map the values and collect the Just
results.
mapMaybeM :: (Monad m, Unbox b) => (a -> m (Maybe b)) -> String a -> m (String b) Source #
O(n) Apply the monadic function to each element of the string and discard characters
returning Nothing
.
takeWhile :: (a -> Bool) -> String a -> String a Source #
O(n) Yield the longest prefix of characters satisfying the predicate.
The current implementation is not copy-free, unless the result string is fused away.
dropWhile :: (a -> Bool) -> String a -> String a Source #
O(n) Drop the longest prefix of characters that satisfy the predicate without copying.
Partitioning
partition :: (a -> Bool) -> String a -> (String a, String a) Source #
O(n) Split the string in two parts, the first one containing those characters that satisfy the predicate and the second one those that don't.
The relative order of the characters is preserved at the cost of a sometimes reduced performance
compared to unstablePartition
.
partitionWith :: (Unbox b, Unbox c) => (a -> Either b c) -> String a -> (String b, String c) Source #
unstablePartition :: (a -> Bool) -> String a -> (String a, String a) Source #
O(n) Split the string in two parts, the first one containing those characters that satisfy the predicate and the second one those that don't.
The order of the characters is not preserved, but the operation is often faster than
partition
.
span :: (a -> Bool) -> String a -> (String a, String a) Source #
O(n) Split the string into the longest prefix of characters that satisfy the predicate and the rest without copying.
break :: (a -> Bool) -> String a -> (String a, String a) Source #
O(n) Split the string into the longest prefix of characters that do not satisfy the predicate and the rest without copying.
groupBy :: (a -> a -> Bool) -> String a -> [String a] Source #
O(n) Split a string into a list of slices.
The concatenation of this list of slices is equal to the argument string, and each slice contains only equal characters, as determined by the equality predicate function.
>>>
import Data.Char (isUpper)
>>>
groupBy (\x y -> isUpper x == isUpper y) "Rio Grande"
[R,io ,G,rande]
group :: Eq a => String a -> [String a] Source #
O(n) Split a string into a list of slices.
The concatenation of this list of slices is equal to the argument string, and each slice contains only equal characters.
This is the equivalent of 'groupBy (==)'.
>>>
group "Mississippi"
[M,i,ss,i,ss,i,pp,i]
Searching
notElem :: Eq a => a -> String a -> Bool Source #
O(n) Check if the string does not contain a character (inverse of elem
).
findIndices :: (a -> Bool) -> String a -> String Int Source #
O(n) Yield the indices of character satisfying the predicate in ascending order.
elemIndices :: Eq a => a -> String a -> String Int Source #
O(n) Yield the indices of all occurrences of the given character in ascending order.
This is a specialised version of findIndices
.
Utilities
eqBy :: (a -> b -> Bool) -> String a -> String b -> Bool Source #
O(n) Check if two strings are equal using the supplied equality predicate.
>>>
import Data.Char (toLower)
>>>
eqBy (\x y -> toLower x == toLower y) "ABcd" "abcD"
True