アルゴリズム

Case Folding

Mapping characters to a common case form for case-insensitive comparison. More comprehensive than lowercasing: German ß → ss, Turkish İ → i (with locale considerations).

What is Case Folding?

Case folding is a Unicode operation that converts text to a form suitable for case-insensitive comparison. It is defined in the Unicode Standard and supported by the CaseFolding.txt data file in the Unicode Character Database. Case folding is closely related to, but distinct from, simple lowercasing: while lowercasing converts a string to its lowercase representation for display, case folding converts a string to a canonical form specifically optimized for string comparison regardless of case.

The practical difference is that case folding handles edge cases that simple lowercasing misses — particularly in languages with complex case mapping behavior.

CaseFolding.txt: The Data File

The Unicode Consortium publishes CaseFolding.txt as part of the Unicode Character Database. It maps each character to its case-folded form using one of four status codes:

Status Meaning
C (Common) Safe for all contexts; included in both simple and full folding
F (Full) Full case folding only; maps one character to multiple characters
S (Simple) Simple case folding only; maps one character to one character
T (Turkic) Special folding for Turkic languages (replaces C/S mappings)

Simple vs. Full Case Folding

Simple case folding maps every character to at most one character — a one-to-one mapping. It is suitable for environments where string length must be preserved.

Full case folding allows one character to map to a sequence of multiple characters. The classic example is the German sharp S:

  • ß (U+00DF, Latin Small Letter Sharp S)
  • Simple fold: ß → ß (no change — no uppercase in simple mapping)
  • Full fold: ß → ss (two characters)

This means that a full case-fold comparison of "STRASSE" and "Straße" would correctly identify them as equal (both fold to "strasse"), while a simple lowercase comparison would not.

# Python uses full case folding via str.casefold()
"STRASSE".casefold() == "Straße".casefold()  # True
"STRASSE".lower() == "Straße".lower()         # False

# The key difference
"Straße".casefold()  # "strasse"
"Straße".lower()     # "straße"  ← ß preserved

Python's str.casefold() implements full Unicode case folding, while str.lower() implements Unicode simple lowercasing.

Locale-Sensitive Folding: The Turkish Problem

The most significant locale-specific case folding issue involves the Turkish and Azerbaijani I. In most languages:

  • Uppercase I → lowercase i
  • Uppercase İ does not exist (or is rare)

In Turkish and Azerbaijani: - Uppercase İ (U+0130, Latin Capital Letter I with Dot Above) → lowercase i (U+0069) - Uppercase I (U+0049, Latin Capital Letter I) → lowercase ı (U+0131, Latin Small Letter Dotless I)

The T (Turkic) status entries in CaseFolding.txt provide the Turkic-specific mappings. Standard Unicode case folding without the T entries is incorrect for Turkish text: it would map Ii rather than Iı, causing "KISA" and "kısa" (meaning "short") to compare as unequal while "KISA" and "kisa" would compare as equal — the wrong result.

# Correct Turkish case comparison requires locale awareness
import locale
# Python's str.casefold() uses C-locale folding (non-Turkic)
# For Turkish: use icu-python or a locale-aware library

How Case Folding Differs from Lowercasing

Operation Purpose Handles ß→ss Handles Turkish İ String length
str.lower() Display (lowercase) No (ß→ß) No (I→i) Preserved
str.casefold() Comparison Yes (ß→ss) No May increase
Turkic case fold Comparison in TR/AZ Yes Yes May increase

Quick Facts

Property Value
Data file CaseFolding.txt in Unicode Character Database
Status codes C (Common), F (Full), S (Simple), T (Turkic)
Key difference from lower() Full folding expands ß→ss
Turkish exception I→ı and İ→i (T status entries)
Python simple fold str.lower()
Python full fold str.casefold()
Use case Case-insensitive string comparison and search

関連用語

アルゴリズム のその他の用語

Grapheme Cluster Boundary

Rules (UAX#29) for determining where one user-perceived character ends and another begins. …

NFC (Canonical Composition)

正規化形式C:分解してから正規再合成し、最短の形式を生成します。データの保存と交換に推奨されており、Webの標準形式です。

NFD (Canonical Decomposition)

正規化形式D:再合成せずに完全分解します。macOSのHFS+ファイルシステムで使われます。é(U+00E9)→ e + ◌́(U+0065 + U+0301)。

NFKC (Compatibility Composition)

正規化形式KC:互換分解後に正規合成。視覚的に類似した文字を統合します(fi→fi、²→2、Ⅳ→IV)。識別子の比較に使われます。

NFKD (Compatibility Decomposition)

正規化形式KD:再合成せずに互換分解。最も強力な正規化で、最も多くの書式情報を失います。

String Comparison

Comparing Unicode strings requires normalization (NFC/NFD) and optionally collation (locale-aware sorting). Binary …

Unicode テキスト分割

テキストの境界を見つけるアルゴリズム:書記素クラスター・単語・文境界。カーソル移動・テキスト選択・テキスト処理に不可欠です。

Unicode 双方向アルゴリズム (UBA)

文字の双方向カテゴリと明示的な方向オーバーライドを使って、混在方向テキスト(例:英語+アラビア語)の表示順序を決定するアルゴリズム。

Unicode 正規化

Unicodeテキストを標準的な正規形に変換するプロセス。4つの形式:NFC(合成)、NFD(分解)、NFKC(互換合成)、NFKD(互換分解)。

Unicode 照合アルゴリズム (UCA)

基本文字 → アクセント → 大小文字 → タイブレーカーの多段階比較でUnicode文字列を比較・ソートする標準アルゴリズム。ロケールのカスタマイズが可能です。