HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduletext-short-0.1.6Haskell2010

Data.Text.Short

Memory-efficient representation of Unicode text strings.

This module is intended to be imported qualified, to avoid name clashes with Prelude functions, e.g.

import qualified Data.Text.Short as TS
import qualified Data.Text.Short (ShortText)

This modules deliberately omits (common) partial functions, which can be found in Data.Text.Short.Partial instead.

  • 1 type
  • 57 values
  • Packagetext-short-0.1.6
  • Exports58
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceShort.hs

The ShortText type

1 declaration
newtypenewtype ShortText
#

A compact representation of Unicode strings.

A ShortText value is a sequence of Unicode scalar values, as defined in §3.9, definition D76 of the Unicode 5.2 standard; This means that a ShortText is a list of (scalar) Unicode code-points (i.e. code-points in the range [U+00 .. U+D7FF] ∪ [U+E000 .. U+10FFFF]).

This type relates to Text as ShortByteString relates to ByteString by providing a more compact type. Please consult the documentation of Data.ByteString.Short for more information.

Currently, a boxed unshared Text has a memory footprint of 6 words (i.e. 48 bytes on 64-bit systems) plus 1, 2, 3 or 4 bytes per code-point for text-2 (due to the internal UTF-8 representation) or 2 or 4 bytes per code-point for text-1 (due to the internal UTF-16 representation). Each Text value which can share its payload with another Text requires only 4 words additionally. Unlike ByteString, Text use unpinned memory.

In comparison, the footprint of a boxed ShortText is only 4 words (i.e. 32 bytes on 64-bit systems) plus 1, 2, 3, or 4 bytes per code-point (due to the internal UTF-8 representation).

It can be shown that for realistic data UTF-16, which is used by text-1, has a space overhead of 50% over UTF-8.

NOTE: The Typeable instance isn't defined for GHC 7.8 (and older) prior to text-short-0.1.3

Instances15IsList, Eq, Data, Ord, Read, Show, …
  • IsList ShortTextDefined in text-short-0.1.6 · Data.Text.Short.Internal

    Note: Surrogate pairs ([U+D800 .. U+DFFF]) character literals are replaced by U+FFFD.

  • Eq ShortTextDefined in text-short-0.1.6 · Data.Text.Short.Internal
  • Data ShortTextDefined in text-short-0.1.6 · Data.Text.Short.Internal

    It exposes a similar Data instance abstraction as Text (see discussion referenced there for more details), preserving the [Char] data abstraction at the cost of inefficiency.

  • Ord ShortTextDefined in text-short-0.1.6 · Data.Text.Short.Internal
  • Read ShortTextDefined in text-short-0.1.6 · Data.Text.Short.Internal
  • Show ShortTextDefined in text-short-0.1.6 · Data.Text.Short.Internal
  • IsString ShortTextDefined in text-short-0.1.6 · Data.Text.Short.Internal

    Note: Surrogate pairs ([U+D800 .. U+DFFF]) in string literals are replaced by U+FFFD.

    This matches the behaviour of IsString instance for Text.

  • Semigroup ShortTextDefined in text-short-0.1.6 · Data.Text.Short.Internal
  • Monoid ShortTextDefined in text-short-0.1.6 · Data.Text.Short.Internal
  • PrintfArg ShortTextDefined in text-short-0.1.6 · Data.Text.Short.Internal
  • NFData ShortTextDefined in text-short-0.1.6 · Data.Text.Short.Internal
  • Binary ShortTextDefined in text-short-0.1.6 · Data.Text.Short.Internal

    The Binary encoding matches the one for Text

  • Hashable ShortTextDefined in text-short-0.1.6 · Data.Text.Short.Internal
  • Lift ShortTextDefined in text-short-0.1.6 · Data.Text.Short.Internal

    Since 0.1.3

  • type Item ShortText = CharDefined in text-short-0.1.6 · Data.Text.Short.Internal

Basic operations

0 declarations

Construction

valueempty :: ShortText
#

\mathcal{O}(0) The empty ShortText.

This is a type-specialised alias of mempty.

Example1 expression
empty""
Example1 expression
null emptyTrue
valuesingleton :: Char -> ShortText
#

\mathcal{O}(1) Construct ShortText from single codepoint.

Property
singleton c == pack [c]
Property
length (singleton c) == 1
Example1 expression
singleton 'A'"A"
Example1 expression
map singleton ['\55295','\55296','\57343','\57344'] -- U+D7FF U+D800 U+DFFF U+E000["\55295","\65533","\65533","\57344"]

Note: This function is total because it replaces the (invalid) code-points U+D800 through U+DFFF with the replacement character U+FFFD.

\mathcal{O}(n) Concatenate two ShortTexts

This is a type-specialised alias of <>.

Example1 expression
append "foo" "bar""foobar"
Property
length (append t1 t2) == length t1 + length t2
valueconcat :: [ShortText] -> ShortText
#

\mathcal{O}(n) Concatenate list of ShortTexts

This is a type-specialised alias of mconcat.

Example1 expression
concat []""
Example1 expression
concat ["foo","bar","doo"]"foobardoo"
valuereplicate :: Int -> ShortText -> ShortText
#

\mathcal{O}(n*m) Replicate a ShortText.

A repetition count smaller than 1 results in an empty string result.

Example1 expression
replicate 3 "jobs!""jobs!jobs!jobs!"
Example1 expression
replicate 10000 """"
Example1 expression
replicate 0 "nothing"""
Property
length (replicate n t) == max 0 n * length t

Deconstruction

valueuncons :: ShortText -> Maybe (Char, ShortText)
#

\mathcal{O}(n) Inverse operation to cons

Returns Nothing for empty input ShortText.

Property
uncons (cons c t) == Just (c,t)
Example1 expression
uncons ""Nothing
Example1 expression
uncons "fmap"Just ('f',"map")
valueunsnoc :: ShortText -> Maybe (ShortText, Char)
#

\mathcal{O}(n) Inverse operation to snoc

Returns Nothing for empty input ShortText.

Property
unsnoc (snoc t c) == Just (t,c)
Example1 expression
unsnoc ""Nothing
Example1 expression
unsnoc "fmap"Just ("fma",'p')

Querying & predicates

valuenull :: ShortText -> Bool
#

\mathcal{O}(1) Test whether a ShortText is empty.

Example1 expression
null ""True
Property
null (singleton c) == False
Property
null t == (length t == 0)
valuelength :: ShortText -> Int
#

\mathcal{O}(n) Count the number of Unicode code-points in a ShortText.

Example1 expression
length "abcd€"5
Example1 expression
length ""0
Property
length t >= 0
valueisAscii :: ShortText -> Bool
#

\mathcal{O}(n) Test whether ShortText contains only ASCII code-points (i.e. only U+0000 through U+007F).

This is a more efficient version of all isAscii.

Example1 expression
isAscii ""True
Example1 expression
isAscii "abc\NUL"True
Example1 expression
isAscii "abcd€"False
Property
isAscii t == all (< '\x80') t
valueall :: (Char -> Bool) -> ShortText -> Bool
#

\mathcal{O}(n) Test whether all code points in ShortText satisfy a predicate.

Example1 expression
all (const False) ""True
Example1 expression
all (> 'c') "abcdabcd"False
Example1 expression
all (/= 'c') "abdabd"True
valueany :: (Char -> Bool) -> ShortText -> Bool
#

\mathcal{O}(n) Test whether any code points in ShortText satisfy a predicate.

Example1 expression
any (> 'c') "abcdabcd"True
Example1 expression
any (const True) ""False
Example1 expression
any (== 'c') "abdabd"False
Property
any p t == not (all (not . p) t)
valuefind :: (Char -> Bool) -> ShortText -> Maybe Char
#

\mathcal{O}(n) Return the left-most codepoint in ShortText that satisfies the given predicate.

Example1 expression
find (> 'b') "abcdabcd"Just 'c'
Example1 expression
find (> 'b') "ababab"Nothing
valueisPrefixOf :: ShortText -> ShortText -> Bool
#

\mathcal{O}(n) Tests whether the first ShortText is a prefix of the second ShortText

Example1 expression
isPrefixOf "ab" "abcdef"True
Example1 expression
isPrefixOf "ac" "abcdef"False
Property
isPrefixOf "" t == True
Property
isPrefixOf t t == True
valueisSuffixOf :: ShortText -> ShortText -> Bool
#

\mathcal{O}(n) Tests whether the first ShortText is a suffix of the second ShortText

Example1 expression
isSuffixOf "ef" "abcdef"True
Example1 expression
isPrefixOf "df" "abcdef"False
Property
isSuffixOf "" t == True
Property
isSuffixOf t t == True

Lookup & indexing

valueindexMaybe :: ShortText -> Int -> Maybe Char
#

\mathcal{O}(n) Lookup i-th code-point in ShortText.

Returns Nothing if out of bounds.

Property
indexMaybe (singleton c) 0 == Just c
Property
indexMaybe t 0 == fmap fst (uncons t)
Property
indexMaybe mempty i == Nothing

\mathcal{O}(n) Lookup i-th code-point from the end of ShortText.

Returns Nothing if out of bounds.

Property
indexEndMaybe (singleton c) 0 == Just c
Property
indexEndMaybe t 0 == fmap snd (unsnoc t)
Property
indexEndMaybe mempty i == Nothing
valuefindIndex :: (Char -> Bool) -> ShortText -> Maybe Int
#

\mathcal{O}(n) Return the index of the left-most codepoint in ShortText that satisfies the given predicate.

Example1 expression
findIndex (> 'b') "abcdabcdef"Just 2
Example1 expression
findIndex (> 'b') "ababab"Nothing
Property
(indexMaybe t =<< findIndex p t) == find p t

Splitting ShortTexts

0 declarations

Basic functions

valuetake :: Int -> ShortText -> ShortText
#

\mathcal{O}(n) Take prefix of given length or return whole ShortText if too short.

Example1 expression
take 3 "abcdef""abc"
Example1 expression
take 3 "ab""ab"
valuetakeEnd :: Int -> ShortText -> ShortText
#

\mathcal{O}(n) Take suffix of given length or return whole ShortText if too short.

Example1 expression
takeEnd 3 "abcdefg""efg"
Example1 expression
takeEnd 3 "ab""ab"
valuetakeWhile :: (Char -> Bool) -> ShortText -> ShortText
#

\mathcal{O}(n) Take longest prefix satisfying given predicate.

Property
takeWhile p t == fst (span p t)
Example1 expression
takeWhile (< 'c') "abcdabcd""ab"
valuetakeWhileEnd :: (Char -> Bool) -> ShortText -> ShortText
#

\mathcal{O}(n) Take longest suffix satisfying given predicate.

Property
takeWhileEnd p t == snd (spanEnd p t)
Example1 expression
takeWhileEnd (>= 'c') "abcdabcd""cd"
valuedropWhile :: (Char -> Bool) -> ShortText -> ShortText
#

\mathcal{O}(n) Remove longest prefix satisfying given predicate.

Property
dropWhile p t == snd (span p t)
Example1 expression
dropWhile (< 'c') "abcdabcd""cdabcd"
valuedropWhileEnd :: (Char -> Bool) -> ShortText -> ShortText
#

\mathcal{O}(n) Remove longest suffix satisfying given predicate.

Property
dropWhileEnd p t == fst (spanEnd p t)
Example1 expression
dropWhileEnd (>= 'c') "abcdabcd""abcdab"
valuedropAround :: (Char -> Bool) -> ShortText -> ShortText
#

\mathcal{O}(n) Strip characters from the beginning end and of ShortText which satisfy given predicate.

Example1 expression
dropAround (== ' ') "   white   space   ""white   space"
Example1 expression
dropAround (> 'a') "bcdefghi"""

Pair-valued functions

valuesplitAt :: Int -> ShortText -> (ShortText, ShortText)
#

\mathcal{O}(n) Split ShortText into two halves.

splitAt n t returns a pair of ShortText with the following properties:

Property
length (fst (splitAt n t)) == min (length t) (max 0 n)
Property
fst (splitAt n t) <> snd (splitAt n t) == t
Example1 expression
splitAt 2 "abcdef"("ab","cdef")
Example1 expression
splitAt 10 "abcdef"("abcdef","")
Example1 expression
splitAt (-1) "abcdef"("","abcdef")

\mathcal{O}(n) Split ShortText into two halves.

splitAtEnd n t returns a pair of ShortText with the following properties:

Property
length (snd (splitAtEnd n t)) == min (length t) (max 0 n)
Property
fst (splitAtEnd n t) <> snd (splitAtEnd n t) == t
Property
splitAtEnd n t == splitAt (length t - n) t
Example1 expression
splitAtEnd 2 "abcdef"("abcd","ef")
Example1 expression
splitAtEnd 10 "abcdef"("","abcdef")
Example1 expression
splitAtEnd (-1) "abcdef"("abcdef","")
valuespan :: (Char -> Bool) -> ShortText -> (ShortText, ShortText)
#

\mathcal{O}(n) Split ShortText into longest prefix satisfying the given predicate and the remaining suffix.

Example1 expression
span (< 'c') "abcdabcd"("ab","cdabcd")
Property
fst (span p t) <> snd (span p t) == t
valuebreak :: (Char -> Bool) -> ShortText -> (ShortText, ShortText)
#

\mathcal{O}(n) Variant of span with negated predicate.

Example1 expression
break (> 'c') "abcdabcd"("abc","dabcd")
Property
break p t == span (not . p) t
Property
fst (break p t) <> snd (break p t) == t
valuespanEnd :: (Char -> Bool) -> ShortText -> (ShortText, ShortText)
#

\mathcal{O}(n) Split ShortText into longest suffix satisfying the given predicate and the preceding prefix.

Example1 expression
spanEnd (> 'c') "abcdabcd"("abcdabc","d")
Property
fst (spanEnd p t) <> snd (spanEnd p t) == t
valuebreakEnd :: (Char -> Bool) -> ShortText -> (ShortText, ShortText)
#

\mathcal{O}(n) Variant of spanEnd with negated predicate.

Example1 expression
breakEnd (< 'c') "abcdabcd"("abcdab","cd")
Property
breakEnd p t == spanEnd (not . p) t
Property
fst (breakEnd p t) <> snd (breakEnd p t) == t

Breaking into many substrings

valuesplit :: (Char -> Bool) -> ShortText -> [ShortText]
#

\mathcal{O}(n) Splits a string 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.

Example1 expression
split (=='a') "aabbaca"["","","bb","c",""]
Example1 expression
split (=='a') ""[""]
Property
intercalate (singleton c) (split (== c) t) = t

NOTE: split never returns an empty list to match the semantics of its counterpart from Data.Text.

Suffix & Prefix operations

\mathcal{O}(n) Strip prefix from second ShortText argument.

Returns Nothing if first argument is not a prefix of the second argument.

Example1 expression
stripPrefix "text-" "text-short"Just "short"
Example1 expression
stripPrefix "test-" "text-short"Nothing

\mathcal{O}(n) Strip suffix from second ShortText argument.

Returns Nothing if first argument is not a suffix of the second argument.

Example1 expression
stripSuffix "-short" "text-short"Just "text"
Example1 expression
stripSuffix "-utf8" "text-short"Nothing

Transformations

4 declarations

\mathcal{O}(n) Insert character between characters of ShortText.

Example1 expression
intersperse '*' "_""_"
Example1 expression
intersperse '*' "MASH""M*A*S*H"

\mathcal{O}(n) Insert ShortText inbetween list of ShortTexts.

Example1 expression
intercalate ", " []""
Example1 expression
intercalate ", " ["foo"]"foo"
Example1 expression
intercalate ", " ["foo","bar","doo"]"foo, bar, doo"
Property
intercalate "" ts == concat ts
valuereverse :: ShortText -> ShortText
#

\mathcal{O}(n) Reverse characters in ShortText.

Example1 expression
reverse "star live desserts""stressed evil rats"
Property
reverse (singleton c) == singleton c
Property
reverse (reverse t) == t
valuefilter :: (Char -> Bool) -> ShortText -> ShortText
#

\mathcal{O}(n) Remove characters from ShortText which don't satisfy given predicate.

Example1 expression
filter (`notElem` ['a','e','i','o','u']) "You don't need vowels to convey information!""Y dn't nd vwls t cnvy nfrmtn!"
Property
filter (const False) t == ""
Property
filter (const True) t == t
Property
length (filter p t) <= length t
Property
filter p t == pack [ c | c <- unpack t, p c ]

Folds

3 declarations
valuefoldl :: (a -> Char -> a) -> a -> ShortText -> a
#

\mathcal{O}(n) Reduces the characters of the ShortText with the binary operator and an initial in forward direction (i.e. from left to right).

Example1 expression
foldl (\_ _ -> True) False ""False
Example1 expression
foldl (\s c -> c : s) ['.'] "abcd""dcba."
valuefoldr :: (Char -> a -> a) -> a -> ShortText -> a
#

\mathcal{O}(n) Reduces the characters of the ShortText with the binary operator and an initial in reverse direction (i.e. from right to left).

Example1 expression
foldr (\_ _ -> True) False ""False
Example1 expression
foldr (:) ['.'] "abcd""abcd."

Conversions

0 declarations

String

valuefromString :: String -> ShortText
#

\mathcal{O}(n) Construct/pack from String

Example1 expression
fromString []""
Example1 expression
fromString ['a','b','c']"abc"
Example1 expression
fromString ['\55295','\55296','\57343','\57344'] -- U+D7FF U+D800 U+DFFF U+E000"\55295\65533\65533\57344"

Note: This function is total because it replaces the (invalid) code-points U+D800 through U+DFFF with the replacement character U+FFFD.

Text

valuefromText :: Text -> ShortText
#

\mathcal{O}(n) Construct ShortText from Text

This is \mathcal{O}(1) with text-2 when the Text is not sliced. Previously it wasn't because Text used UTF-16 as its internal representation.

valuetoText :: ShortText -> Text
#

\mathcal{O}(n) Convert to Text

Property
(fromText . toText) t == t
Property
(toText . fromText) t == t

This is \mathcal{O}(1) with text-2. Previously it wasn't because Text used UTF-16 as its internal representation.

ByteString

\mathcal{O}(n) Construct ShortText from UTF-8 encoded ShortByteString

This operation doesn't copy the input ShortByteString but it cannot be \mathcal{O}(1) because we need to validate the UTF-8 encoding.

Returns Nothing in case of invalid UTF-8 encoding.

Example1 expression
fromShortByteString "\x00\x38\xF0\x90\x8C\x9A" -- U+00 U+38 U+1031AJust "\NUL8\66330"
Example1 expression
fromShortByteString "\xC0\x80" -- invalid denormalised U+00Nothing
Example1 expression
fromShortByteString "\xED\xA0\x80" -- U+D800 (non-scalar code-point)Nothing
Example1 expression
fromShortByteString "\xF4\x8f\xbf\xbf" -- U+10FFFFJust "\1114111"
Example1 expression
fromShortByteString "\xF4\x90\x80\x80" -- U+110000 (invalid)Nothing
Property
fromShortByteString (toShortByteString t) == Just t