Section 3.13 of the Unicode standard defines algorithms for caselessmatching.
X.casefold() == Y.casefold()
in Python 3 implements the "default caseless matching" (D144).
Casefolding does not preserve the normalization of strings in all instances and therefore the normalization needs to be done ('å'
vs. 'å'
). D145 introduces "canonical caseless matching":
import unicodedatadef NFD(text): return unicodedata.normalize('NFD', text)def canonical_caseless(text): return NFD(NFD(text).casefold())
NFD()
is called twice for very infrequent edge cases involving U+0345 character.
Example:
>>> 'å'.casefold() == 'å'.casefold()False>>> canonical_caseless('å') == canonical_caseless('å')True
There are also compatibility caseless matching (D146) for cases such as '㎒'
(U+3392) and "identifier caseless matching" to simplify and optimize caseless matching of identifiers.