Case-Insensitive Decisions on Text

A case-insensitive text comparison needs the same normalization on both operands and a rule suited to the kind of identifier.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Normalize the comparison key
  2. Check scoped behavior
  3. Trace equal comparison keys and distinct display forms
  4. Test identity rules independently of search convenience

Normalize the comparison key

A search for "north" may match "North" after case folding. Keep the original text for display while storing or deriving its normalized search key. Canonical label rules determine whether to remove spaces and which possible collisions are accepted.

Check scoped behavior

User-visible search labels and machine-readable identifiers often need distinct equality rules. A test can expect "Done" to match a search query while rejecting it as an exact protocol code if that code is lowercase. See text membership for checking entire labels.[1][2]

Trace equal comparison keys and distinct display forms

A directory editor may choose case-insensitive searches for titles, while preserving their original capitalization when displayed. The index can compare a case-folded key derived from a title against a key derived the same way from the query. For plain ASCII, “Workshop” and “workshop” map alike. Unicode case folding handles additional cases and can change the length of the resulting string. Treat the folded key as a comparison representation. Keep matching offsets and the source spelling separate when presenting excerpts. See canonicalizing labels.

Test identity rules independently of search convenience

A resource code may have case-sensitive identity while its descriptive label can be found without regard to case. Build a sample with differently cased descriptions and codes so the two operations can be tested separately. If a domain also normalizes canonically equivalent Unicode sequences, apply the agreed transforms consistently at both index and query time. Unicode normalization covers the separate structure of those equivalences.

Before merging two records with the same key, decide whether the key was intended to mean identity or only similarity for retrieval. Store original values when readers still need them.

References

  1. Python Software Foundation, Python Tutorial: Control Flow ↩
  2. Python Software Foundation, Python Language Reference: Expressions ↩