Z-Data-0.2.0.0: Array, vector and text

Z.Data.Text.Extra

Contents

Description

Various combinators works on Texts.

Synopsis

# Slice manipulation

cons :: Char -> Text -> Text Source #

O(n) cons is analogous to (:) for lists, but of different complexity, as it requires making a copy.

snoc :: Text -> Char -> Text Source #

O(n) Append a char to the end of a text.

O(1) Extract the head and tail of a text, return Nothing if it is empty.

O(1) Extract the init and last of a text, return Nothing if text is empty.

O(1) Extract the first char of a text.

O(1) Extract the chars after the head of a text.

NOTE: tailMayEmpty return empty text in the case of an empty text.

O(1) Extract the last char of a text.

O(1) Extract the chars before of the last one.

NOTE: initMayEmpty return empty text in the case of an empty text.

O(1) Extract the first char of a text.

Throw EmptyText if text is empty.

O(1) Extract the chars after the head of a text.

Throw EmptyText if text is empty.

O(1) Extract the last char of a text.

Throw EmptyText if text is empty.

O(1) Extract the chars before of the last one.

Throw EmptyText if text is empty.

inits :: Text -> [Text] Source #

O(n) Return all initial segments of the given text, empty first.

tails :: Text -> [Text] Source #

O(n) Return all final segments of the given text, whole text first.

take :: Int -> Text -> Text Source #

O(1) take n, applied to a text xs, returns the prefix of xs of length n, or xs itself if n > length xs.

drop :: Int -> Text -> Text Source #

O(1) drop n xs returns the suffix of xs after the first n char, or [] if n > length xs.

takeR :: Int -> Text -> Text Source #

O(1) takeR n, applied to a text xs, returns the suffix of xs of length n, or xs itself if n > length xs.

dropR :: Int -> Text -> Text Source #

O(1) dropR n xs returns the prefix of xs before the last n char, or [] if n > length xs.

slice :: Int -> Int -> Text -> Text Source #

O(1) Extract a sub-range text with give start index and length.

This function is a total function just like 'takedrop', indexlength exceeds range will be ingored, e.g.

slice 1 3 "hello"   == "ell"
slice -1 -1 "hello" == ""
slice -2 2 "hello"  == ""
slice 2 10 "hello"  == "llo"


This holds for all x y: slice x y vs == drop x . take (x+y) vs

splitAt :: Int -> Text -> (Text, Text) Source #

O(n) splitAt n xs is equivalent to (take n xs, drop n xs).

takeWhile :: (Char -> Bool) -> Text -> Text Source #

O(n) Applied to a predicate p and a text t, returns the longest prefix (possibly empty) of t of elements that satisfy p.

takeWhileR :: (Char -> Bool) -> Text -> Text Source #

O(n) Applied to a predicate p and a text t, returns the longest suffix (possibly empty) of t of elements that satisfy p.

dropWhile :: (Char -> Bool) -> Text -> Text Source #

O(n) Applied to a predicate p and a text vs, returns the suffix (possibly empty) remaining after takeWhile p vs.

dropWhileR :: (Char -> Bool) -> Text -> Text Source #

O(n) Applied to a predicate p and a text vs, returns the prefix (possibly empty) remaining before takeWhileR p vs.

dropAround :: (Char -> Bool) -> Text -> Text Source #

O(n) dropAround f = dropWhile f . dropWhileR f

break :: (Char -> Bool) -> Text -> (Text, Text) Source #

O(n) Split the text into the longest prefix of elements that do not satisfy the predicate and the rest without copying.

span :: (Char -> Bool) -> Text -> (Text, Text) Source #

O(n) Split the text into the longest prefix of elements that satisfy the predicate and the rest without copying.

breakR :: (Char -> Bool) -> Text -> (Text, Text) Source #

breakR behaves like break but from the end of the text.

breakR p == spanR (not.p)

spanR :: (Char -> Bool) -> Text -> (Text, Text) Source #

spanR behaves like span but from the end of the text.

breakOn :: Text -> Text -> (Text, Text) Source #

Break a text on a subtext, returning a pair of the part of the text prior to the match, and the rest of the text, e.g.

break "wor" "hello, world" = ("hello, ", "world")

Arguments

 :: Text needle to search for -> Text haystack in which to search -> [(Text, Text)]

O(n+m) Find all non-overlapping instances of needle in haystack. Each element of the returned list consists of a pair:

• The entire string prior to the kth match (i.e. the prefix)
• The kth match, followed by the remainder of the string

Examples:

breakOnAll "::" ""
==> []
breakOnAll "" "abc"
==> [("a", "bc"), ("ab", "c"), ("abc", "/")]


The result list is lazy, search is performed when you force the list.

group :: Text -> [Text] Source #

The group function takes a text and returns a list of texts such that the concatenation of the result is equal to the argument. Moreover, each sublist in the result contains only equal elements. For example,

group Mississippi = [M,"i","ss","i","ss","i","pp","i"]


It is a special case of groupBy, which allows the programmer to supply their own equality test.

groupBy :: (Char -> Char -> Bool) -> Text -> [Text] Source #

The groupBy function is the non-overloaded version of group.

O(n) The stripPrefix function takes two texts and returns Just the remainder of the second iff the first is its prefix, and otherwise Nothing.

O(n) The stripSuffix function takes two texts and returns Just the remainder of the second iff the first is its suffix, and otherwise Nothing.

split :: Char -> Text -> [Text] Source #

O(n) Break a text into pieces separated by the delimiter element consuming the delimiter. I.e.

split '\n' "a\nb\nd\ne" == ["a","b","d","e"]
split 'a'  "aXaXaXa"    == ["","X","X","X",""]
split 'x'  "x"          == ["",""]

and

intercalate [c] . split c == id
split == splitWith . (==)

NOTE, this function behavior different with bytestring's. see #56.

splitWith :: (Char -> Bool) -> Text -> [Text] Source #

O(n) Splits a text into components delimited by separators, where the predicate returns True for a separator char. The resulting components do not contain the separators. Two adjacent separators result in an empty component in the output. eg.

splitWith (=='a') "aabbaca" == ["","","bb","c",""]
splitWith (=='a') []        == [""]

splitOn :: Text -> Text -> [Text] Source #

O(m+n) Break haystack into pieces separated by needle.

Note: An empty needle will essentially split haystack element by element.

Examples:

>>> splitOn "\r\n" "a\r\nb\r\nd\r\ne"
["a","b","d","e"]

>>> splitOn "aaa"  "aaaXaaaXaaaXaaa"
["","X","X","X",""]

>>> splitOn "x"  "x"
["",""]


and

intercalate s . splitOn s         == id
splitOn (singleton c)             == split (==c)

The isPrefix function returns True if the first argument is a prefix of the second.

O(n) The isSuffixOf function takes two text and returns True if the first is a suffix of the second.

Check whether one text is a subtext of another.

needle isInfixOf haystack === null haystack || indices needle haystake /= [].

commonPrefix :: Text -> Text -> (Text, Text, Text) Source #

O(n) Find the longest non-empty common prefix of two strings and return it, along with the suffixes of each string at which they no longer match. e.g.

>>> commonPrefix "foobar" "fooquux"
("foo","bar","quux")

>>> commonPrefix "veeble" "fetzer"
("","veeble","fetzer")


words :: Text -> [Text] Source #

O(n) Breaks a Bytes up into a list of words, delimited by unicode space.

lines :: Text -> [Text] Source #

O(n) Breaks a text up into a list of lines, delimited by ascii n.

unwords :: [Text] -> Text Source #

O(n) Joins words with ascii space.

unlines :: [Text] -> Text Source #

O(n) Joins lines with ascii n.

NOTE: This functions is different from unlines, it DOES NOT add a trailing n.

padLeft :: Int -> Char -> Text -> Text Source #

Add padding to the left so that the whole text's length is at least n.

padRight :: Int -> Char -> Text -> Text Source #

Add padding to the right so that the whole text's length is at least n.

# Transform

O(n) Reverse the characters of a string.

O(n) The intersperse function takes a character and places it between the characters of a Text. Performs replacement on invalid scalar values.

intercalate :: Text -> [Text] -> Text Source #

O(n) The intercalate function takes a Text and a list of Texts and concatenates the list after interspersing the first argument between each element of the list.

transpose :: [Text] -> [Text] Source #

The transpose function transposes the rows and columns of its text argument.