Python · Case Folding and Normalization

Case Folding and Normalization

import stringzilla 6.0.0-rc.1 · 91a27bdd

API

Types

Utf8UncasedMatchesIterator yielding all uncased matches of needle in haystack.
__init__.pyi:2505 ↗
class Utf8UncasedMatches

Uses Unicode case folding for proper handling of international text.

Each iteration yields a String view of the matched region in the haystack. The matched length may differ from needle length due to case folding expansions, like German 'ß' matching 'SS'.

Created by

  • String.utf8_uncased_matches()
  • sz.utf8_uncased_matches()

Examples

>>> len(list(sz.utf8_uncased_matches('aAaA', 'a')))
4

Functions

utf8_uncased_foldApply Unicode case folding to a UTF-8 string.
__init__.pyi:1829 ↗
def utf8_uncased_fold(
    text: _StringLike,
    validate: bool = False,
    *,
    capabilities: Capability | None = None,
) -> bytes

Case folding normalizes text for uncased comparisons, handling one-to-many expansions, like German sharp S to 'ss'.

Parameters

text_StringLikeThe input UTF-8 string.
validateboolIf True, validate UTF-8 before processing. Default: False.
capabilitiesCapability | NoneCapabilities to run, by default the CPU's enabled ones.

Returns

The case-folded UTF-8 string.

Examples

>>> sz.utf8_uncased_fold('HELLO')
b'hello'
>>> sz.utf8_uncased_fold('Stra\u00dfe')  # German sharp S
b'strasse'
utf8_normNormalize a UTF-8 string to a Unicode normalization form.
__init__.pyi:1852 ↗
def utf8_norm(
    text: _StringLike,
    form: str,
    validate: bool = False,
    *,
    capabilities: Capability | None = None,
) -> bytes

Transforms the input into NFD, NFC, NFKD, or NFKC, mirroring unicodedata.normalize but returning raw UTF-8 bytes, or into NFKC_Casefold, the Unicode toNFKC_Casefold: NFKC with full case folding and no default-ignorable codepoints.

Parameters

text_StringLikeThe input UTF-8 string.
formstrOne of 'NFC', 'NFD', 'NFKC', 'NFKD', 'NFKC_Casefold'.
validateboolIf True, validate UTF-8 before processing. Default: False.
capabilitiesCapability | NoneCapabilities to run, by default the CPU's enabled ones.

Returns

The normalized UTF-8 string.

Examples

>>> sz.utf8_norm('caf\u00e9', 'NFD')  # decompose
b'cafe\xcc\x81'
>>> sz.utf8_norm('\ufb01', 'NFKD')  # fi ligature
b'fi'
>>> sz.utf8_norm('Stra\u00dfe\u00ad', 'NFKC_Casefold')  # folded, soft hyphen dropped
b'strasse'
utf8_find_denormalizedReturn the byte offset of the first normalization violation, or None.
__init__.pyi:1879 ↗
def utf8_find_denormalized(
    text: _StringLike,
    form: str,
    *,
    capabilities: Capability | None = None,
) -> int | None

Scans the UTF-8 string and returns the byte offset of the first codepoint that breaks the given normalization form (first non-Yes Quick-Check result or canonical-ordering violation). Returns None when the string is already fully normalized.

Parameters

text_StringLikeThe input UTF-8 string.
formstrOne of 'NFC', 'NFD', 'NFKC', 'NFKD', 'NFKC_Casefold'.
capabilitiesCapability | NoneCapabilities to run, by default the CPU's enabled ones.

Returns

int | None: Byte offset of first violation, or None if already normalized.

Examples

>>> sz.utf8_find_denormalized('cafe\u0301', 'NFC')  # 'e' + combining acute
3
>>> sz.utf8_find_denormalized('caf\u00e9', 'NFC') is None  # precomposed, already NFC
True
utf8_uncased_orderCompare two UTF-8 strings uncasedly.
__init__.pyi:1942 ↗
def utf8_uncased_order(
    a: _StringLike,
    b: _StringLike,
    validate: bool = False,
    *,
    capabilities: Capability | None = None,
) -> int

Performs lexicographical comparison using Unicode case folding, correctly handling one-to-many expansions, like 'Straße' equaling 'STRASSE'.

Parameters

a_StringLikeFirst string to compare.
b_StringLikeSecond string to compare.
validateboolIf True, validate UTF-8 before processing. Default: False.
capabilitiesCapability | NoneCapabilities to run, by default the CPU's enabled ones.

Returns

Negative if a < b, zero if equal, positive if a > b.

Examples

>>> sz.utf8_uncased_order('hello', 'HELLO')
0
>>> sz.utf8_uncased_order('apple', 'BANANA')
-1
utf8_uncased_matchesIterate over all uncased matches of needle in haystack.
__init__.pyi:1966 ↗
def utf8_uncased_matches(
    haystack: _StringLike,
    needle: _StringLike,
    /,
    include_overlapping: bool = False,
    *,
    capabilities: Capability | None = None,
) -> Utf8UncasedMatches

This function uses Unicode case folding for proper handling of international text. The matched region length may differ from the needle length due to case folding expansions, like 'ß' matching 'SS'. An empty needle matches at every codepoint boundary, the end included.

Parameters

haystack_StringLikeThe string to search in.
needle_StringLikeThe pattern to find.
include_overlappingboolAllow overlapping matches, defaulting to False.
capabilitiesCapability | NoneCapabilities to run, by default the CPU's enabled ones.

Yields

Examples

>>> list(sz.utf8_uncased_matches('Hello HELLO hello', 'hello'))
[sz.String('Hello'), sz.String('HELLO'), sz.String('hello')]
>>> list(sz.utf8_uncased_matches('Straße STRASSE', 'strasse'))
[sz.String('Straße'), sz.String('STRASSE')]