Safe Haskell | None |
---|---|
Language | Haskell98 |
Module to be shared between server and client.
This module must be valid for both GHC and Fay.
For GHC this is an alias for Data.Text, for Fay it's an opaque data type represented by JavaScript strings.
- data Text :: *
- pack :: String -> Text
- unpack :: Text -> String
- fromString :: String -> Text
- empty :: Text
- splitOn :: Text -> Text -> [Text]
- stripSuffix :: Text -> Text -> Maybe Text
- cons :: Char -> Text -> Text
- snoc :: Text -> Char -> Text
- append :: Text -> Text -> Text
- uncons :: Text -> Maybe (Char, Text)
- head :: Text -> Char
- init :: Text -> Text
- last :: Text -> Char
- tail :: Text -> Text
- null :: Text -> Bool
- length :: Text -> Int
- maximum :: Text -> Char
- all :: (Char -> Bool) -> Text -> Bool
- any :: (Char -> Bool) -> Text -> Bool
- concatMap :: (Char -> Text) -> Text -> Text
- concat :: [Text] -> Text
- minimum :: Text -> Char
- toLower :: Text -> Text
- toUpper :: Text -> Text
- map :: (Char -> Char) -> Text -> Text
- intercalate :: Text -> [Text] -> Text
- intersperse :: Char -> Text -> Text
- reverse :: Text -> Text
- isPrefixOf :: Text -> Text -> Bool
- drop :: Int -> Text -> Text
- take :: Int -> Text -> Text
- unlines :: [Text] -> Text
- lines :: Text -> [Text]
Documentation
data Text :: *
A space efficient, packed, unboxed Unicode text type.
IsList Text | |
Eq Text | |
Data Text | This instance preserves data abstraction at the cost of inefficiency. We omit reflection services for the sake of data abstraction. This instance was created by copying the updated behavior of
The original discussion is archived here: could we get a Data instance for Data.Text.Text? The followup discussion that changed the behavior of |
Ord Text | |
Read Text | |
Show Text | |
IsString Text | |
Monoid Text | |
NFData Text | |
Typeable * Text | |
type Item Text = Char |
Creation and elimination
fromString :: String -> Text Source
Have this in scope with the OverloadedStrings and BindableSyntax extensions and Fay will replace all string literals with Text.
Breaking into many substrings
O(m+n) Break a Text
into pieces separated by the first Text
argument (which cannot be empty), consuming the delimiter. An empty
delimiter is invalid, and will cause an error to be raised.
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)
(Note: the string s
to split on above cannot be empty.)
In (unlikely) bad cases, this function's time complexity degrades towards O(n*m).
stripSuffix :: Text -> Text -> Maybe Text
O(n) Return the prefix of the second string if its suffix matches the entire first string.
Examples:
stripSuffix "bar" "foobar" == Just "foo" stripSuffix "" "baz" == Just "baz" stripSuffix "foo" "quux" == Nothing
This is particularly useful with the ViewPatterns
extension to
GHC, as follows:
{-# LANGUAGE ViewPatterns #-} import Data.Text as T quuxLength :: Text -> Int quuxLength (stripSuffix "quux" -> Just pre) = T.length pre quuxLength _ = -1
Basic interface
cons :: Char -> Text -> Text infixr 5
O(n) Adds a character to the front of a Text
. This function
is more costly than its List
counterpart because it requires
copying a new array. Subject to fusion. Performs replacement on
invalid scalar values.
O(n) Adds a character to the end of a Text
. This copies the
entire array in the process, unless fused. Subject to fusion.
Performs replacement on invalid scalar values.
O(1) Returns the first character of a Text
, which must be
non-empty. Subject to fusion.
O(1) Returns all but the last character of a Text
, which must
be non-empty. Subject to fusion.
O(1) Returns the last character of a Text
, which must be
non-empty. Subject to fusion.
O(1) Returns all characters after the head of a Text
, which
must be non-empty. Subject to fusion.
Special folds
Case conversion
O(n) Convert a string to lower case, using simple case conversion. Subject to fusion.
The result string may be longer than the input string. For instance, "İ" (Latin capital letter I with dot above, U+0130) maps to the sequence "i" (Latin small letter i, U+0069) followed by " ̇" (combining dot above, U+0307).
O(n) Convert a string to upper case, using simple case conversion. Subject to fusion.
The result string may be longer than the input string. For instance, the German "ß" (eszett, U+00DF) maps to the two-letter sequence "SS".
Transformations
intercalate :: Text -> [Text] -> Text
O(n) The intercalate
function takes a Text
and a list of
Text
s and concatenates the list after interspersing the first
argument between each element of the list.
intersperse :: Char -> Text -> Text
O(n) The intersperse
function takes a character and places it
between the characters of a Text
. Subject to fusion. Performs
replacement on invalid scalar values.
Predicates
isPrefixOf :: Text -> Text -> Bool
O(n) The isPrefixOf
function takes two Text
s and returns
True
iff the first is a prefix of the second. Subject to fusion.