Data.Text.Internal.Fusion.Common

text-1.2.1.3: An efficient packed Unicode text type.

Copyright | (c) Bryan O'Sullivan 2009, 2012
License | BSD-style
Maintainer | bos@serpentine.com
Stability | experimental
Portability | GHC
Safe Haskell | None
Language | Haskell98

Data.Text.Internal.Fusion.Common

Contents

- Creation and elimination
- Basic interface
- Transformations
  - Case conversion
  - Justification
- Folds
  - Special folds
- Construction
  - Scans
  - Generation and unfolding
- Substrings
  - Breaking strings
- Predicates
- Searching
- Indexing
- Zipping and unzipping

Description

Warning: this is an internal module, and does not have a stable API or name. Functions in this module may not check or enforce preconditions expected by public modules. Use at your own risk!

Common stream fusion functionality for text.

Synopsis

- singleton :: Char -> Stream Char
- streamList :: [a] -> Stream a
- unstreamList :: Stream a -> [a]
- streamCString# :: Addr# -> Stream Char
- cons :: Char -> Stream Char -> Stream Char
- snoc :: Stream Char -> Char -> Stream Char
- append :: Stream Char -> Stream Char -> Stream Char
- head :: Stream Char -> Char
- uncons :: Stream Char -> Maybe (Char, Stream Char)
- last :: Stream Char -> Char
- tail :: Stream Char -> Stream Char
- init :: Stream Char -> Stream Char
- null :: Stream Char -> Bool
- lengthI :: Integral a => Stream Char -> a
- compareLengthI :: Integral a => Stream Char -> a -> Ordering
- isSingleton :: Stream Char -> Bool
- map :: (Char -> Char) -> Stream Char -> Stream Char
- intercalate :: Stream Char -> [Stream Char] -> Stream Char
- intersperse :: Char -> Stream Char -> Stream Char
- toCaseFold :: Stream Char -> Stream Char
- toLower :: Stream Char -> Stream Char
- toTitle :: Stream Char -> Stream Char
- toUpper :: Stream Char -> Stream Char
- justifyLeftI :: Integral a => a -> Char -> Stream Char -> Stream Char
- foldl :: (b -> Char -> b) -> b -> Stream Char -> b
- foldl' :: (b -> Char -> b) -> b -> Stream Char -> b
- foldl1 :: (Char -> Char -> Char) -> Stream Char -> Char
- foldl1' :: (Char -> Char -> Char) -> Stream Char -> Char
- foldr :: (Char -> b -> b) -> b -> Stream Char -> b
- foldr1 :: (Char -> Char -> Char) -> Stream Char -> Char
- concat :: [Stream Char] -> Stream Char
- concatMap :: (Char -> Stream Char) -> Stream Char -> Stream Char
- any :: (Char -> Bool) -> Stream Char -> Bool
- all :: (Char -> Bool) -> Stream Char -> Bool
- maximum :: Stream Char -> Char
- minimum :: Stream Char -> Char
- scanl :: (Char -> Char -> Char) -> Char -> Stream Char -> Stream Char
- replicateCharI :: Integral a => a -> Char -> Stream Char
- replicateI :: Int64 -> Stream Char -> Stream Char
- unfoldr :: (a -> Maybe (Char, a)) -> a -> Stream Char
- unfoldrNI :: Integral a => a -> (b -> Maybe (Char, b)) -> b -> Stream Char
- take :: Integral a => a -> Stream Char -> Stream Char
- drop :: Integral a => a -> Stream Char -> Stream Char
- takeWhile :: (Char -> Bool) -> Stream Char -> Stream Char
- dropWhile :: (Char -> Bool) -> Stream Char -> Stream Char
- isPrefixOf :: Eq a => Stream a -> Stream a -> Bool
- elem :: Char -> Stream Char -> Bool
- filter :: (Char -> Bool) -> Stream Char -> Stream Char
- findBy :: (Char -> Bool) -> Stream Char -> Maybe Char
- indexI :: Integral a => Stream Char -> a -> Char
- findIndexI :: Integral a => (Char -> Bool) -> Stream Char -> Maybe a
- countCharI :: Integral a => Char -> Stream Char -> a
- zipWith :: (a -> a -> b) -> Stream a -> Stream a -> Stream b

# Creation and elimination

singleton :: Char -> Stream Char

streamList :: [a] -> Stream a

unstreamList :: Stream a -> [a]

streamCString# :: Addr# -> Stream Char

Stream the UTF-8-like packed encoding used by GHC to represent constant strings in generated code.

This encoding uses the byte sequence "xc0x80" to represent NUL, and the string is NUL-terminated.

# Basic interface

cons :: Char -> Stream Char -> Stream Char

O(n) Adds a character to the front of a Stream Char.

snoc :: Stream Char -> Char -> Stream Char

O(n) Adds a character to the end of a stream.

append :: Stream Char -> Stream Char -> Stream Char

O(n) Appends one Stream to the other.

head :: Stream Char -> Char

O(1) Returns the first character of a Text, which must be non-empty. Subject to array fusion.

uncons :: Stream Char -> Maybe (Char, Stream Char)

O(1) Returns the first character and remainder of a 'Stream Char', or `Nothing` if empty. Subject to array fusion.

last :: Stream Char -> Char

O(n) Returns the last character of a 'Stream Char', which must be non-empty.

tail :: Stream Char -> Stream Char

O(1) Returns all characters after the head of a Stream Char, which must be non-empty.

init :: Stream Char -> Stream Char

O(1) Returns all but the last character of a Stream Char, which must be non-empty.

null :: Stream Char -> Bool

O(1) Tests whether a Stream Char is empty or not.

lengthI :: Integral a => Stream Char -> a

O(n) Returns the number of characters in a string.

compareLengthI :: Integral a => Stream Char -> a -> Ordering

O(n) Compares the count of characters in a string to a number. Subject to fusion.

This function gives the same answer as comparing against the result of `lengthI`, but can short circuit if the count of characters is greater than the number or if the stream can't possibly be as long as the number supplied, and hence be more efficient.

isSingleton :: Stream Char -> Bool

O(n) Indicate whether a string contains exactly one element.

# Transformations

map :: (Char -> Char) -> Stream Char -> Stream Char

O(n) `map` `f` xs is the Stream Char obtained by applying `f` to each element of `xs`.

intercalate :: Stream Char -> [Stream Char] -> Stream Char

intersperse :: Char -> Stream Char -> Stream Char

O(n) Take a character and place it between each of the characters of a 'Stream Char'.

## Case conversion

With Unicode text, it is incorrect to use combinators like `map toUpper` to case convert each character of a string individually. Instead, use the whole-string case conversion functions from this module. For correctness in different writing systems, these functions may map one input character to two or three output characters.

toCaseFold :: Stream Char -> Stream Char

O(n) Convert a string to folded case. This function is mainly useful for performing caseless (or case insensitive) string comparisons.

A string `x` is a caseless match for a string `y` if and only if:

    toCaseFold x == toCaseFold y

The result string may be longer than the input string, and may differ from applying `toLower` to the input string. For instance, the Armenian small ligature men now (U+FB13) is case folded to the bigram men now (U+0574 U+0576), while the micro sign (U+00B5) is case folded to the Greek small letter letter mu (U+03BC) instead of itself.

toLower :: Stream Char -> Stream Char

O(n) Convert a string to lower case, using simple case conversion. The result string may be longer than the input string. For instance, the Latin capital letter I with dot above (U+0130) maps to the sequence Latin small letter i (U+0069) followed by combining dot above (U+0307).

toTitle :: Stream Char -> Stream Char

O(n) Convert a string to title case, using simple case conversion.

The first letter of the input is converted to title case, as is every subsequent letter that immediately follows a non-letter. Every letter that immediately follows another letter is converted to lower case.

The result string may be longer than the input string. For example, the Latin small ligature ﬂ (U+FB02) is converted to the sequence Latin capital letter F (U+0046) followed by Latin small letter l (U+006C).

Note: this function does not take language or culture specific rules into account. For instance, in English, different style guides disagree on whether the book name "The Hill of the Red Fox" is correctly title cased—but this function will capitalize every word.

toUpper :: Stream Char -> Stream Char

O(n) Convert a string to upper case, using simple case conversion. The result string may be longer than the input string. For instance, the German eszett (U+00DF) maps to the two-letter sequence SS.

## Justification

justifyLeftI :: Integral a => a -> Char -> Stream Char -> Stream Char

# Folds

foldl :: (b -> Char -> b) -> b -> Stream Char -> b

foldl, applied to a binary operator, a starting value (typically the left-identity of the operator), and a Stream, reduces the Stream using the binary operator, from left to right.

foldl' :: (b -> Char -> b) -> b -> Stream Char -> b

A strict version of foldl.

foldl1 :: (Char -> Char -> Char) -> Stream Char -> Char

foldl1 is a variant of foldl that has no starting value argument, and thus must be applied to non-empty Streams.

foldl1' :: (Char -> Char -> Char) -> Stream Char -> Char

A strict version of foldl1.

foldr :: (Char -> b -> b) -> b -> Stream Char -> b

`foldr`, applied to a binary operator, a starting value (typically the right-identity of the operator), and a stream, reduces the stream using the binary operator, from right to left.

foldr1 :: (Char -> Char -> Char) -> Stream Char -> Char

foldr1 is a variant of `foldr` that has no starting value argument, and thus must be applied to non-empty streams. Subject to array fusion.

## Special folds

concat :: [Stream Char] -> Stream Char

O(n) Concatenate a list of streams. Subject to array fusion.

concatMap :: (Char -> Stream Char) -> Stream Char -> Stream Char

Map a function over a stream that results in a stream and concatenate the results.

any :: (Char -> Bool) -> Stream Char -> Bool

O(n) any `p` xs determines if any character in the stream `xs` satisifes the predicate `p`.

all :: (Char -> Bool) -> Stream Char -> Bool

O(n) all `p` xs determines if all characters in the `Text` `xs` satisify the predicate `p`.

maximum :: Stream Char -> Char

O(n) maximum returns the maximum value from a stream, which must be non-empty.

minimum :: Stream Char -> Char

O(n) minimum returns the minimum value from a `Text`, which must be non-empty.

# Construction

## Scans

scanl :: (Char -> Char -> Char) -> Char -> Stream Char -> Stream Char

## Generation and unfolding

replicateCharI :: Integral a => a -> Char -> Stream Char

replicateI :: Int64 -> Stream Char -> Stream Char

unfoldr :: (a -> Maybe (Char, a)) -> a -> Stream Char

O(n), where `n` is the length of the result. The unfoldr function is analogous to the List `unfoldr`. unfoldr builds a stream from a seed value. The function takes the element and returns Nothing if it is done producing the stream or returns Just (a,b), in which case, a is the next Char in the string, and b is the seed value for further production.

unfoldrNI :: Integral a => a -> (b -> Maybe (Char, b)) -> b -> Stream Char

O(n) Like `unfoldr`, `unfoldrNI` builds a stream from a seed value. However, the length of the result is limited by the first argument to `unfoldrNI`. This function is more efficient than `unfoldr` when the length of the result is known.

# Substrings

## Breaking strings

take :: Integral a => a -> Stream Char -> Stream Char

O(n) take n, applied to a stream, returns the prefix of the stream of length `n`, or the stream itself if `n` is greater than the length of the stream.

drop :: Integral a => a -> Stream Char -> Stream Char

O(n) drop n, applied to a stream, returns the suffix of the stream after the first `n` characters, or the empty stream if `n` is greater than the length of the stream.

takeWhile :: (Char -> Bool) -> Stream Char -> Stream Char

takeWhile, applied to a predicate `p` and a stream, returns the longest prefix (possibly empty) of elements that satisfy p.

dropWhile :: (Char -> Bool) -> Stream Char -> Stream Char

dropWhile `p` xs returns the suffix remaining after takeWhile `p` xs.

# Predicates

isPrefixOf :: Eq a => Stream a -> Stream a -> Bool

O(n) The `isPrefixOf` function takes two `Stream`s and returns `True` iff the first is a prefix of the second.

# Searching

elem :: Char -> Stream Char -> Bool

O(n) elem is the stream membership predicate.

filter :: (Char -> Bool) -> Stream Char -> Stream Char

O(n) `filter`, applied to a predicate and a stream, returns a stream containing those characters that satisfy the predicate.

# Indexing

findBy :: (Char -> Bool) -> Stream Char -> Maybe Char

O(n) The `findBy` function takes a predicate and a stream, and returns the first element in matching the predicate, or `Nothing` if there is no such element.

indexI :: Integral a => Stream Char -> a -> Char

O(n) Stream index (subscript) operator, starting from 0.

findIndexI :: Integral a => (Char -> Bool) -> Stream Char -> Maybe a

The `findIndexI` function takes a predicate and a stream and returns the index of the first element in the stream satisfying the predicate.

countCharI :: Integral a => Char -> Stream Char -> a

O(n) The `countCharI` function returns the number of times the query element appears in the given stream.

# Zipping and unzipping

zipWith :: (a -> a -> b) -> Stream a -> Stream a -> Stream b

zipWith generalises `zip` by zipping with the function given as the first argument, instead of a tupling function.