A Unicode code point.
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
- Packagealfred-margaret-2.1.1.1
- Exports38
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceUtf8.hs
A UTF-8 code unit is a byte. A Unicode code point can be encoded as up to four code units.
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.
Constructors
Instances12Bounded, Eq, Num, Ord, Show, Generic, …
Bounded CodeUnitIndexDefined in alfred-margaret-2.1.1.1 · Data.Text.Utf8Eq CodeUnitIndexDefined in alfred-margaret-2.1.1.1 · Data.Text.Utf8Num CodeUnitIndexDefined in alfred-margaret-2.1.1.1 · Data.Text.Utf8Ord CodeUnitIndexDefined in alfred-margaret-2.1.1.1 · Data.Text.Utf8Show CodeUnitIndexDefined in alfred-margaret-2.1.1.1 · Data.Text.Utf8Generic CodeUnitIndexDefined in alfred-margaret-2.1.1.1 · Data.Text.Utf8NFData CodeUnitIndexDefined in alfred-margaret-2.1.1.1 · Data.Text.Utf8Prim CodeUnitIndexDefined in alfred-margaret-2.1.1.1 · Data.Text.Utf8Hashable CodeUnitIndexDefined in alfred-margaret-2.1.1.1 · Data.Text.Utf8FromJSON CodeUnitIndexDefined in alfred-margaret-2.1.1.1 · Data.Text.Utf8ToJSON CodeUnitIndexDefined in alfred-margaret-2.1.1.1 · Data.Text.Utf8type Rep CodeUnitIndex = D1 ('MetaDataDefined in alfred-margaret-2.1.1.1 · Data.Text.Utf8"CodeUnitIndex"
"Data.Text.Utf8"
"alfred-margaret-2.1.1.1-LmqCjvAcVEl7fSX12XT7qq"
'True) (C1 ('MetaCons"CodeUnitIndex"
'PrefixI 'True) (S1 ('MetaSel ('Just"codeUnitIndex"
) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Int)))
Instances21IsList, Eq, Data, Ord, Read, Show, …
IsList TextDefined in text-2.1.4 · Data.Text · orphanPerforms replacement on invalid scalar values:
Example2 expressions :set -XOverloadedLists['\55555'] :: Text"\65533"
Eq TextDefined in text-2.1.4 · Data.Text · orphanData TextDefined in text-2.1.4 · Data.Text · orphanThis 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 andData.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.Mapis archived here: Proposal: Allow gunfold for Data.Map, ...Ord TextDefined in text-2.1.4 · Data.Text · orphanRead TextDefined in text-2.1.4 · Data.Text · orphanShow TextDefined in text-2.1.4 · Data.Text.Show · orphanIsString TextDefined in text-2.1.4 · Data.Text · orphanPerforms replacement on invalid scalar values:
Example2 expressions :set -XOverloadedStrings"\55555" :: Text"\65533"
Semigroup TextDefined in text-2.1.4 · Data.Text · orphanBeware:
stimeswill crash if the given number does not fit into anInt.Monoid TextDefined in text-2.1.4 · Data.Text · orphanPrintfArg TextDefined in text-2.1.4 · Data.Text · orphanNFData TextDefined in text-2.1.4 · Data.Text · orphanBinary TextDefined in text-2.1.4 · Data.Text · orphanHashable TextDefined in hashable-1.5.1.0 · Data.Hashable.ClassFromJSON TextDefined in aeson-2.3.2.0 · Data.Aeson.Types.FromJSONFromJSONKey TextDefined in aeson-2.3.2.0 · Data.Aeson.Types.FromJSONToJSON TextDefined in aeson-2.3.2.0 · Data.Aeson.Types.ToJSONToJSONKey TextDefined in aeson-2.3.2.0 · Data.Aeson.Types.ToJSONLift TextDefined in text-2.1.4 · Data.Text · orphanStrict Text TextDefined in strict-0.5.1 · Data.Strict.ClassesMonad m => Stream Text m CharDefined in parsec-3.1.18.0 · Text.Parsec.Primtype Item Text = CharDefined in text-2.1.4 · Data.Text · orphan
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.
Lower-Case a UTF-8 codepoint. Uses toLowerAscii for ASCII and toLower otherwise.
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 ß == ẞß
Lowercase a Text by applying lowerCodePoint to each Char.
Lower-case the ASCII code points A-Z and leave the rest of ASCII intact.
Convert a Unicode Code Point c into a list of UTF-8 code units (bytes).
Decoding
4 declarationsFunctions that turns code unit sequences into code point sequences.
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│
└───────────────┴───────────────┴───────────────┴───────────────┘Decode a list of UTF-8 code units into a list of code points.
Indexing
4 declarationsText 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.
Get the code unit at the given CodeUnitIndex. Performs bounds checking.
Does exactly the same thing as unsafeIndexCodePoint', but on Text values.
Scan backwards through the text until we've seen the specified number of codepoints. Assumes that the initial CodeUnitIndex is within a codepoint.
Slicing Functions
2 declarationsunsafeCutUtf8 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] == tThe 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.
unsafeCutUtf8 :: CodeUnitIndexStarting position of substring.
-> CodeUnitIndexLength of substring.
-> TextInitial string.
-> (Text, Text)
Functions on Arrays
7 declarationsFunctions for working with Array values.
See Data.Primitive.byteArrayContents.
See Data.Primitive.isByteArrayPinned.
Decode a code point at the given CodeUnitIndex. Returns garbage if there is no valid code point at that position. Does not perform bounds checking. See decode2, decode3 and decode4 for the expected format of multi-byte code points.
Intermediate state when you're iterating backwards through a Utf8 text.
Constructors
BackwardsIterbackwardsIterNext :: !CodeUnitIndexFirst byte to the left of the codepoint that we're focused on. This can be used with unsafeIndexEndOfCodePoint' to find the next codepoint.
backwardsIterChar :: !CodePointThe codepoint that we're focused on
backwardsIterEndOfChar :: !CodeUnitIndexPoints to the last byte of the codepoint that we're focused on
Instances2Generic, Rep
Generic BackwardsIterDefined in alfred-margaret-2.1.1.1 · Data.Text.Utf8type Rep BackwardsIter = D1 ('MetaDataDefined in alfred-margaret-2.1.1.1 · Data.Text.Utf8"BackwardsIter"
"Data.Text.Utf8"
"alfred-margaret-2.1.1.1-LmqCjvAcVEl7fSX12XT7qq"
'False) (C1 ('MetaCons"BackwardsIter"
'PrefixI 'True) (S1 ('MetaSel ('Just"backwardsIterNext"
) 'SourceUnpack 'SourceStrict 'DecidedUnpack) (Rec0 CodeUnitIndex) :*: (S1 ('MetaSel ('Just"backwardsIterChar"
) 'SourceUnpack 'SourceStrict 'DecidedUnpack) (Rec0 CodePoint) :*: S1 ('MetaSel ('Just"backwardsIterEndOfChar"
) 'SourceUnpack 'SourceStrict 'DecidedUnpack) (Rec0 CodeUnitIndex))))
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 declarationsRe-exported from Text.
O(n) Concatenate a list of Texts.
O(1) Tests whether a Text is empty or not.
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).