Module
std.text.unicode
Enums
public enum GraphemeBreakProperty
One of the UAX #29 Grapheme_Cluster_Break property values.
Returned by graphemeBreakProperty(c:) and consumed by
shouldBreakBetween(...). Variant names match the Unicode property
labels — see UAX #29 for the precise definitions and the boundary
rules (GB1–GB999) that consume them.
Representation
A 14-state tag enum (no payload). ordinal() gives the numeric
encoding used by the stage-2 lookup table.
Functions
public func caseFold(Char) -> Char
Single-codepoint case fold for c. Use when comparing characters
case-insensitively: caseFold(a) == caseFold(b) is the canonical
per-character equality test.
For codepoints whose Unicode fold is multi-codepoint (e.g. ß → ss),
this returns only the first folded codepoint — see
caseFoldExpansion for the full sequence.
Examples
caseFold('A') // 'a'
caseFold('İ') // 'i' — see caseFoldExpansion for "i\u{307}"
caseFold('a') == caseFold('A') // truepublic func caseFoldExpansion(Char) -> String
Full Unicode case fold of c as a String. Returns "" when
c has no multi-codepoint fold — pair with hasCaseFoldExpansion,
or fall back to caseFold for the single-codepoint form.
Examples
caseFoldExpansion('ß') // "ss"
caseFoldExpansion('ffi') // "ffi"
caseFoldExpansion('a') // "" (no expansion; caseFold('a') == 'a')public func graphemeBreakProperty(Char) -> GraphemeBreakProperty
Looks up the UAX #29 Grapheme_Cluster_Break property for c.
O(1) — two array indexings into the trie. Codepoints above
U+10FFFF (which are not valid Unicode scalars) yield .Other.
Examples
graphemeBreakProperty('a') // .Other
graphemeBreakProperty('\r') // .CR
graphemeBreakProperty('\u{200D}') // .ZWJpublic func hasCaseFoldExpansion(Char) -> Bool
true iff folding c produces more than one codepoint. Linear
scan over FOLD_EXPANSIONS (~100 entries) — fine per-character but
quadratic if applied across a large set.
public func hasLowercaseExpansion(Char) -> Bool
true iff lowercasing c produces more than one codepoint.
Same scan caveats as hasUppercaseExpansion.
public func hasTitlecaseExpansion(Char) -> Bool
true iff titlecasing c produces more than one codepoint.
public func hasUppercaseExpansion(Char) -> Bool
true iff uppercasing c produces more than one codepoint.
Linear scan over UPPER_EXPANSIONS (~100 entries); fine for
per-character calls in normal text but quadratic if applied to a
large codepoint set.
public func lowercaseExpansion(Char) -> String
Full Unicode lowercase expansion for c. Empty string when no
multi-codepoint expansion applies — see uppercaseExpansion for
the same shape.
public func shouldBreakBetween(GraphemeBreakProperty, GraphemeBreakProperty, Bool, Bool) -> Bool
Decides whether a grapheme cluster boundary lies between two adjacent codepoints with the given break properties.
Implements the UAX #29 boundary rules GB3–GB13/GB999. The caller must thread two scalar bits across calls to capture rules that look further back than one codepoint:
prevPrevWasRI: was the codepoint beforepreva Regional_Indicator? Needed to keep regional-indicator pairs together while still breaking between successive pairs (GB12/13).prevWasZWJ: was the codepoint beforepreva ZWJ? Needed for the simplified emoji ZWJ-sequence rule (GB11).
Returns true to break (start a new cluster at curr), false to
keep prev and curr in the same cluster.
public func titlecaseExpansion(Char) -> String
Full Unicode titlecase expansion for c. Empty string when no
multi-codepoint expansion applies.
public func toLowercase(Char) -> Char
Single-codepoint lowercase mapping for c. Same caveats as
toUppercase: codepoints with multi-char lowercase forms return
only the first codepoint — see lowercaseExpansion.
public func toTitlecase(Char) -> Char
Single-codepoint titlecase mapping for c. Differs from
toUppercase only for the codepoints (mostly Greek/Croatian
digraphs) where Unicode defines a distinct "Title" form. Multi-char
expansions live in titlecaseExpansion.
public func toUppercase(Char) -> Char
Single-codepoint uppercase mapping for c. Falls back to c for
characters with no mapping and for codepoints above U+10FFFF.
For characters whose Unicode uppercase form expands to multiple
codepoints (e.g. ß → SS, fi → FI), this returns only the first
codepoint of the expansion. Use hasUppercaseExpansion(c:) to detect
the multi-char case and uppercaseExpansion(c:) to retrieve the full
String.
Examples
toUppercase('a') // 'A'
toUppercase('ß') // 'S' — see uppercaseExpansion for "SS"
toUppercase('1') // '1' — no mappingpublic func uppercaseExpansion(Char) -> String
Full Unicode uppercase expansion for c as a String. Returns the
empty string when c has no multi-codepoint expansion — pair with
hasUppercaseExpansion (or call toUppercase instead) to avoid the
scan when you only need the single-codepoint form.
Examples
uppercaseExpansion('ß') // "SS"
uppercaseExpansion('fi') // "FI"
uppercaseExpansion('a') // "" (use toUppercase for 'A')field
public let unicodeVersion: String
Unicode version these tables track. Bump alongside the regeneration
of the underlying data/*.bin files.