HORIZON HASKELLDocslts/ghc-9.14.xb2f3bfd2026-10-11Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · b2f3bfd · 2026-10-11

Modulealfred-margaret-2.1.1.1Haskell2010

Data.Text.Utf8

This module provides functions that allow treating Text values as series of UTF-8 code units instead of characters. Any calls to Text in alfred-margaret go through this module. Therefore we re-export some Text functions, e.g. concat.

  • 5 types
  • 33 values
typetype CodeUnit = Word8
#

A UTF-8 code unit is a byte. A Unicode code point can be encoded as up to four code units.

newtypenewtype CodeUnitIndex
#

An index into the raw UTF-8 data of a Text. This is not the code point index as conventionally accepted by Text, so we wrap it to avoid confusing the two. Incorrect index manipulation can lead to surrogate pairs being sliced, so manipulate indices with care. This type is also used for lengths.

Instances12Bounded, Eq, Num, Ord, Show, Generic, …
datadata Text
#

A space efficient, packed, unboxed Unicode text type.

Constructors

Instances21IsList, Eq, Data, Ord, Read, Show, …
  • IsList TextDefined in text-2.1.3 · Data.Text · orphan

    Performs replacement on invalid scalar values:

    Example2 expressions
    :set -XOverloadedLists['\55555'] :: Text"\65533"
  • Eq TextDefined in text-2.1.3 · Data.Text · orphan
  • Data TextDefined in text-2.1.3 · Data.Text · orphan

    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 Data.Set.Set and Data.Map.Data.Map.Map. If you feel a mistake has been made, please feel free to submit improvements.

    The original discussion is archived here: could we get a Data instance for Data.Text.Text?

    The followup discussion that changed the behavior of Set and Data.Map.Map is archived here: Proposal: Allow gunfold for Data.Map, ...

  • Ord TextDefined in text-2.1.3 · Data.Text · orphan
  • Read TextDefined in text-2.1.3 · Data.Text · orphan
  • Show TextDefined in text-2.1.3 · Data.Text.Show · orphan
  • IsString TextDefined in text-2.1.3 · Data.Text · orphan

    Performs replacement on invalid scalar values:

    Example2 expressions
    :set -XOverloadedStrings"\55555" :: Text"\65533"
  • Semigroup TextDefined in text-2.1.3 · Data.Text · orphan

    Beware: stimes will crash if the given number does not fit into an Int.

  • Monoid TextDefined in text-2.1.3 · Data.Text · orphan
  • PrintfArg TextDefined in text-2.1.3 · Data.Text · orphan
  • NFData TextDefined in text-2.1.3 · Data.Text · orphan
  • Binary TextDefined in text-2.1.3 · Data.Text · orphan
  • Hashable TextDefined in hashable-1.5.1.0 · Data.Hashable.Class
  • FromJSON TextDefined in aeson-2.3.2.0 · Data.Aeson.Types.FromJSON
  • FromJSONKey TextDefined in aeson-2.3.2.0 · Data.Aeson.Types.FromJSON
  • ToJSON TextDefined in aeson-2.3.2.0 · Data.Aeson.Types.ToJSON
  • ToJSONKey TextDefined in aeson-2.3.2.0 · Data.Aeson.Types.ToJSON
  • Lift TextDefined in text-2.1.3 · Data.Text · orphan
  • Strict Text TextDefined in strict-0.5.1 · Data.Strict.Classes
  • Monad m => Stream Text m CharDefined in parsec-3.1.18.0 · Text.Parsec.Prim
  • type Item Text = CharDefined in text-2.1.3 · Data.Text · orphan
valueisCaseInvariant :: Text -> Bool
#

Return whether text has exactly one case variation, such that this function will not return true when Aho–Corasick would differentiate when doing case-insensitive matching.

The return value of this function is not really an index. However the signature is supposed to make it clear that the length is returned in terms of code units, not code points.

valueunlowerCodePoint :: Char -> [Char]
#

Inverse of Char.toLower/Utf8.lowerCodePoint

Returns all the characters that have the given character as their lower case, for example:

unlowerCodePoint a == "aA" unlowerCodePoint A == "" unlowerCodePoint '1' == "1" unlowerCodePoint i == İiI unlowerCodePoint ß == ẞß

valuetoLowerAscii :: Char -> Char
#

Lower-case the ASCII code points A-Z and leave the rest of ASCII intact.

valueunicode2utf8 :: (Ord a, Num a, Bits a) => a -> [a]
#

Convert a Unicode Code Point c into a list of UTF-8 code units (bytes).

Decoding

4 declarations

Functions that turns code unit sequences into code point sequences.

valuedecode2 :: CodeUnit -> CodeUnit -> CodePoint
#

Decode 2 UTF-8 code units into their code point. The given code units should have the following format:

┌───────────────┬───────────────┐
│1 1 0 x x x x x│1 0 x x x x x x│
└───────────────┴───────────────┘

Decode 3 UTF-8 code units into their code point. The given code units should have the following format:

┌───────────────┬───────────────┬───────────────┐
│1 1 1 0 x x x x│1 0 x x x x x x│1 0 x x x x x x│
└───────────────┴───────────────┴───────────────┘

Decode 4 UTF-8 code units into their code point. The given code units should have the following format:

┌───────────────┬───────────────┬───────────────┬───────────────┐
│1 1 1 1 0 x x x│1 0 x x x x x x│1 0 x x x x x x│1 0 x x x x x x│
└───────────────┴───────────────┴───────────────┴───────────────┘

Indexing

4 declarations

Text can be indexed by code units or code points. A CodePoint is a 21-bit Unicode code point and can consist of up to four code units. A CodeUnit is a single byte.

Slicing Functions

2 declarations

unsafeCutUtf8 and unsafeSliceUtf8 are used to retrieve slices of Text values. unsafeSliceUtf8 begin length returns a substring of length length starting at begin. unsafeSliceUtf8 begin length returns a tuple of the "surrounding" substrings.

They satisfy the following property:

let (prefix, suffix) = unsafeCutUtf8 begin length t
in concat [prefix, unsafeSliceUtf8 begin length t, suffix] == t

The following diagram visualizes the relevant offsets for begin = CodeUnitIndex 2, length = CodeUnitIndex 6 and t = "BCDEFGHIJKL".

 off                 off+len
  │                     │
  ▼                     ▼
──┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬──
 A│B│C│D│E│F│G│H│I│J│K│L│M│N
──┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴──
      ▲           ▲
      │           │
 off+begin   off+begin+length

unsafeSliceUtf8 begin length t == "DEFGHI"
unsafeCutUtf8 begin length t == ("BC", "JKL")

The shown array is open at each end because in general, t may be a slice as well.

WARNING: As their name implies, these functions are not (necessarily) bounds-checked. Use at your own risk.

Functions on Arrays

7 declarations

Functions for working with Array values.

datadata BackwardsIter
#

Intermediate state when you're iterating backwards through a Utf8 text.

Constructors

Instances2Generic, Rep

Similar to unsafeIndexCodePoint', but assumes that the given index is the end of a utf8 codepoint. It returns the decoded code point and the index _before_ the code point. The resulting index could be passed directly to unsafeIndexEndOfCodePoint' again to decode the _previous_ code point.

General Functions

8 declarations

Re-exported from Text.

valueisInfixOf :: Text -> Text -> Bool
#

O(n+m) The isInfixOf function takes two Texts and returns True if and only if the first is contained, wholly and intact, anywhere within the second.

In (unlikely) bad cases, this function's time complexity degrades towards O(n*m).

valuepack :: String -> Text
#

O(n) Convert a String into a Text. Performs replacement on invalid scalar values, so unpack . pack is not id:

Example1 expression
Data.Text.unpack (pack "\55555")"\65533"
valueindices
  1. :: Text

    Substring to search for (needle)

  2. -> Text

    Text to search in (haystack)

  3. -> [Int]
#

O(n+m) Find the offsets of all non-overlapping indices of needle within haystack.

In (unlikely) bad cases, this algorithm's complexity degrades towards O(n*m).