i;unicode-casemap - Simple Unicode Collation Algorithm
A simple, case-insensitive way to compare Unicode text for equality, ordering and substring matching.
Crispin, M., "i;unicode-casemap - Simple Unicode Collation Algorithm", RFC 5051, DOI 10.17487/RFC5051, October 2007, <https://www.rfc-editor.org/info/rfc5051>.
@misc{rfc5051,
series = {Request for Comments},
number = 5051,
howpublished = {RFC 5051},
publisher = {RFC Editor},
doi = {10.17487/RFC5051},
url = {https://www.rfc-editor.org/info/rfc5051},
author = {M. Crispin},
title = {{i;unicode-casemap - Simple Unicode Collation Algorithm}},
pagetotal = 7,
year = 2007,
month = oct,
} RFC text
Contents
Network Working Group M. Crispin
Request for Comments: 5051 University of Washington
Category: Standards Track October 2007
i;unicode-casemap - Simple Unicode Collation Algorithm
Status of This Memo
This document specifies an Internet standards track protocol for the
Internet community, and requests discussion and suggestions for
improvements. Please refer to the current edition of the "Internet
Official Protocol Standards" (STD 1) for the standardization state
and status of this protocol. Distribution of this memo is unlimited.
Abstract
This document describes "i;unicode-casemap", a simple case-
insensitive collation for Unicode strings. It provides equality,
substring, and ordering operations.
1. Introduction
The "i;ascii-casemap" collation described in [COMPARATOR] is quite
simple to implement and provides case-independent comparisons for the
26 Latin alphabetics. It is specified as the default and/or baseline
comparator in some application protocols, e.g., [IMAP-SORT].
However, the "i;ascii-casemap" collation does not produce
satisfactory results with non-ASCII characters. It is possible, with
a modest extension, to provide a more sophisticated collation with
greater multilingual applicability than "i;ascii-casemap". This
extension provides case-independent comparisons for a much greater
number of characters. It also collates characters with diacriticals
with the non-diacritical character forms.
This collation, "i;unicode-casemap", is intended to be an alternative
to, and preferred over, "i;ascii-casemap". It does not replace the
"i;basic" collation described in [BASIC].
2. Unicode Casemap Collation Description
The "i;unicode-casemap" collation is a simple collation which is
case-insensitive in its treatment of characters. It provides
equality, substring, and ordering operations. The validity test
operation returns "valid" for any input.
This collation allows strings in arbitrary (and mixed) character
sets, as long as the character set for each string is identified and
it is possible to convert the string to Unicode. Strings which have
an unidentified character set and/or cannot be converted to Unicode
are not rejected, but are treated as binary.
Each input string is prepared by converting it to a "titlecased
canonicalized UTF-8" string according to the following steps, using
UnicodeData.txt ([UNICODE-DATA]):
(1) A Unicode codepoint is obtained from the input string.
(a) If the input string is in a known charset that can be
converted to Unicode, a sequence in the string's charset
is read and checked for validity according to the rules of
that charset. If the sequence is valid, it is converted
to a Unicode codepoint. Note that for input strings in
UTF-8, the UTF-8 sequence must be valid according to the
rules of [UTF-8]; e.g., overlong UTF-8 sequences are
invalid.
(b) If the input string is in an unknown charset, or an
invalid sequence occurs in step (1)(a), conversion ceases.
No further preparation is performed, and any partial
preparation results are discarded. The original string is
used unchanged with the i;octet comparator.
(2) The following steps, using UnicodeData.txt ([UNICODE-DATA]),
are performed on the resulting codepoint from step (1)(a).
(a) If the codepoint has a titlecase property in
UnicodeData.txt (this is normally the same as the
uppercase property), the codepoint is converted to the
codepoints in the titlecase property.
(b) If the resulting codepoint from (2)(a) has a decomposition
property of any type in UnicodeData.txt, the codepoint is
converted to the codepoints in the decomposition property.
This step is recursively applied to each of the resulting
codepoints until no more decomposition is possible
(effectively Normalization Form KD).
Example: codepoint U+01C4 (LATIN CAPITAL LETTER DZ WITH CARON)
has a titlecase property of U+01C5 (LATIN CAPITAL LETTER D
WITH SMALL LETTER Z WITH CARON). Codepoint U+01C5 has a
decomposition property of U+0044 (LATIN CAPITAL LETTER D)
U+017E (LATIN SMALL LETTER Z WITH CARON). U+017E has a
decomposition property of U+007A (LATIN SMALL LETTER Z) U+030c
(COMBINING CARON). Neither U+0044, U+007A, nor U+030C have
any decomposition properties. Therefore, U+01C4 is converted
to U+0044 U+007A U+030C by this step.
(3) The resulting codepoint(s) from step (2) is/are appended, in
UTF-8 format, to the "titlecased canonicalized UTF-8" string.
(4) Repeat from step (1) until there is no more data in the input
string.
Following the above preparation process on each string, the equality,
ordering, and substring operations are as for i;octet.
It is permitted to use an alternative implementation of the above
preparation process if it produces the same results. For example, it
may be more convenient for an implementation to convert all input
strings to a sequence of UTF-16 or UTF-32 values prior to performing
any of the step (2) actions. Similarly, if all input strings are (or
are convertible to) Unicode, it may be possible to use UTF-32 as an
alternative to UTF-8 in step (3).
Note: UTF-16 is unsuitable as an alternative to UTF-8 in step (3),
because UTF-16 surrogates will cause i;octet to collate codepoints
U+E0000 through U+FFFF after non-BMP codepoints.
This collation is not locale sensitive. Consequently, care should be
taken when using OS-supplied functions to implement this collation.
Functions such as strcasecmp and toupper are sometimes locale
sensitive and may inconsistently casemap letters.
The i;unicode-casemap collation is well suited to use with many
Internet protocols and computer languages. Use with natural language
is often inappropriate; even though the collation apparently supports
languages such as Swahili and English, in real-world use it tends to
mis-sort a number of types of string:
o people and place names containing scripts that are not collated
according to "alphabetical order".
o words with characters that have diacriticals. However,
i;unicode-casemap generally does a better job than i;ascii-casemap
for most (but not all) languages. For example, German umlaut
letters will sort correctly, but some Scandinavian letters will
not.
o names such as "Lloyd" (which in Welsh sorts after "Lyon", unlike
in English),
o strings containing other non-letter symbols; e.g., euro and pound
sterling symbols, quotation marks other than '"', dashes/hyphens,
etc.
3. Unicode Casemap Collation Registration
<?xml version='1.0'?>
<!DOCTYPE collation SYSTEM 'collationreg.dtd'>
<collation rfc="5051" scope="global" intendedUse="common">
<identifier>i;unicode-casemap</identifier>
<title>Unicode Casemap</title>
<operations>equality order substring</operations>
<specification>RFC 5051</specification>
<owner>IETF</owner>
<submitter>mrc@cac.washington.edu</submitter>
</collation>
4. Security Considerations
The security considerations for [UTF-8], [STRINGPREP], and [UNICODE-
SECURITY] apply and are normative to this specification.
The results from this comparator will vary depending upon the
implementation for several reasons. Implementations MUST consider
whether these possibilities are a problem for their use case:
1) New characters added in Unicode may have decomposition or
titlecase properties that will not be known to an implementation
based upon an older revision of Unicode. This impacts step (2).
2) Step (2)(b) defines a subset of Normalization Form KD (NFKD) that
does not require normalization of out-of-order diacriticals.
However, an implementation MAY use an NFKD library routine that
does such normalization. This impacts step (2)(b) and possibly
also step (1)(a), and is an issue only with ill-formed UTF-8
input.
3) The set of charsets handled in step (1)(a) is open-ended. UTF-8
(and, by extension, US-ASCII) are the only mandatory-to-implement
charsets. This impacts step (1)(a).
Implementations SHOULD, as far as feasible, support all the
charsets they are likely to encounter in the input data, in order
to avoid poor collation caused by the fall through to the (1)(b)
rule.
4) Other charsets may have revisions which add new characters that
are not known to an implementation based upon an older revision.
This impacts step (1)(a) and possibly also step (1)(b).
An attacker may create input that is ill-formed or in an unknown
charset, with the intention of impacting the results of this
comparator or exploiting other parts of the system which process this
input in different ways. Note, however, that even well-formed data
in a known charset can impact the result of this comparator in
unexpected ways. For example, an attacker can substitute U+0041
(LATIN CAPITAL LETTER A) with U+0391 (GREEK CAPITAL LETTER ALPHA) or
U+0410 (CYRILLIC CAPITAL LETTER A) in the intention of causing a
non-match of strings which visually appear the same and/or causing
the string to appear elsewhere in a sort.
5. IANA Considerations
The i;unicode-casemap collation defined in section 2 has been added
to the registry of collations defined in [COMPARATOR].
6. Normative References
[COMPARATOR] Newman, C., Duerst, M., and A. Gulbrandsen,
"Internet Application Protocol Collation
Registry", RFC 4790, February 2007.
[STRINGPREP] Hoffman, P. and M. Blanchet, "Preparation of
Internationalized Strings ("stringprep")", RFC
3454, December 2002.
[UTF-8] Yergeau, F., "UTF-8, a transformation format of
ISO 10646", STD 63, RFC 3629, November 2003.
[UNICODE-DATA] <http://www.unicode.org/Public/UNIDATA/
UnicodeData.txt>
Although the UnicodeData.txt file referenced
here is part of the Unicode standard, it is
subject to change as new characters are added
to Unicode and errors are corrected in Unicode
revisions. As a result, it may be less stable
than might otherwise be implied by the
standards status of this specification.
[UNICODE-SECURITY] Davis, M. and M. Suignard, "Unicode Security
Considerations", February 2006,
<http://www.unicode.org/reports/tr36/>.
7. Informative References
[BASIC] Newman, C., Duerst, M., and A. Gulbrandsen,
"i;basic - the Unicode Collation Algorithm",
Work in Progress, March 2007.
[IMAP-SORT] Crispin, M. and K. Murchison, "Internet Message
Access Protocol - SORT and THREAD Extensions",
Work in Progress, September 2007.
Author's Address
Mark R. Crispin
Networks and Distributed Computing
University of Washington
4545 15th Avenue NE
Seattle, WA 98105-4527
Phone: +1 (206) 543-5762
EMail: MRC@CAC.Washington.EDU
Full Copyright Statement
Copyright (C) The IETF Trust (2007).
This document is subject to the rights, licenses and restrictions
contained in BCP 78, and except as set forth therein, the authors
retain all their rights.
This document and the information contained herein are provided on an
"AS IS" basis and THE CONTRIBUTOR, THE ORGANIZATION HE/SHE REPRESENTS
OR IS SPONSORED BY (IF ANY), THE INTERNET SOCIETY, THE IETF TRUST AND
THE INTERNET ENGINEERING TASK FORCE DISCLAIM ALL WARRANTIES, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF
THE INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED
WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
Intellectual Property
The IETF takes no position regarding the validity or scope of any
Intellectual Property Rights or other rights that might be claimed to
pertain to the implementation or use of the technology described in
this document or the extent to which any license under such rights
might or might not be available; nor does it represent that it has
made any independent effort to identify any such rights. Information
on the procedures with respect to rights in RFC documents can be
found in BCP 78 and BCP 79.
Copies of IPR disclosures made to the IETF Secretariat and any
assurances of licenses to be made available, or the result of an
attempt made to obtain a general license or permission for the use of
such proprietary rights by implementers or users of this
specification can be obtained from the IETF on-line IPR repository at
http://www.ietf.org/ipr.
The IETF invites any interested party to bring to its attention any
copyrights, patents or patent applications, or other proprietary
rights that may cover technology that may be required to implement
this standard. Please address the information to the IETF at
ietf-ipr@ietf.org.
AI ELI5
On this page
§1Why a Unicode Casemap Collation
A collation is a named set of rules for comparing strings. The i;ascii-casemap collation is simple to implement and ignores case for the 26 Latin letters. It is used as the default or baseline comparator in some application protocols.
It does not give satisfactory results for non-ASCII characters. i;unicode-casemap extends the idea: it ignores case for many more characters and also collates characters with diacriticals together with their plain forms. The RFC says it is intended to be an alternative to, and preferred over, i;ascii-casemap. It does not replace the i;basic collation.
An old rule only knew how to treat big and small English letters as twins. This new rule knows many more letters, and it even treats a letter with a little hat or dots on top as the same as the plain one.
§2Preparing a String for Comparison
The collation provides equality, substring and ordering operations. Its validity test returns 'valid' for any input. Before any comparison, each input string is turned into a titlecased canonicalized UTF-8 string, using UnicodeData.txt.
- Step 1: read a sequence in the string's charset, check that it is valid, and convert it to a Unicode codepoint. UTF-8 input must be valid UTF-8, so overlong sequences are invalid.
- Step 2a: if the codepoint has a titlecase property, which is normally the same as uppercase, replace it with that.
- Step 2b: if the result has a decomposition property of any type, replace it with the decomposition, and repeat until nothing more decomposes. The RFC says this is effectively Normalization Form KD.
- Step 3: append the resulting codepoints in UTF-8 form to the prepared string.
- Step 4: repeat until the input is used up.
After both strings are prepared, equality, ordering and substring operations work as for i;octet, which is a plain octet-by-octet comparison.
First every word is rewritten in one standard form: all letters made capital and fancy letters broken into plain letter plus little mark. Then the two rewritten words are compared piece by piece.
§2.1A Worked Example and Fallback Rules
The RFC's example is codepoint U+01C4 (LATIN CAPITAL LETTER DZ WITH CARON). Its titlecase is U+01C5, which decomposes to U+0044 and U+017E. Then U+017E decomposes to U+007A and U+030C (COMBINING CARON). None of those three has a further decomposition, so U+01C4 becomes U+0044 U+007A U+030C.
Strings may be in arbitrary and mixed character sets, as long as each string's charset is identified and it can be converted to Unicode. If the charset is unknown, or an invalid sequence appears, conversion stops, partial results are thrown away, and the original string is compared unchanged with i;octet. Such strings are not rejected, they are treated as binary.
If the computer cannot read a word's alphabet at all, it does not give up. It just compares the raw letters exactly as they are.
U+01C4 LATIN CAPITAL LETTER DZ WITH CARON
titlecase -> U+01C5
decomposition -> U+0044 U+017E
decomposition -> U+0044 U+007A U+030C§2.2Implementation Notes
An implementation may use a different preparation process if it produces the same results. For example, it can convert every string to UTF-16 or UTF-32 before the step 2 actions, or use UTF-32 instead of UTF-8 in step 3 when all input strings are or can be converted to Unicode.
UTF-16 is not suitable as the form used in step 3. Its surrogates would make i;octet collate codepoints U+E0000 through U+FFFF after non-BMP codepoints.
The collation is not locale sensitive. Functions such as strcasecmp and toupper are sometimes locale sensitive and may inconsistently casemap letters, so take care when using OS-supplied functions to implement it.
You may follow a different recipe if the cake comes out exactly the same. But do not use a recipe that changes depending on which country your oven is in.
§3Where It Works and Where It Does Not
The collation is well suited to use with many Internet protocols and computer languages. Use with natural language is often inappropriate. Even though it apparently supports languages such as Swahili and English, in real-world use it tends to mis-sort several kinds of string.
- People and place names containing scripts that are not collated according to alphabetical order.
- Words with diacriticals. It generally does better than
i;ascii-casemapfor most but not all languages: German umlaut letters sort correctly, but some Scandinavian letters do not. - Names such as 'Lloyd', which in Welsh sorts after 'Lyon', unlike in English.
- Strings with other symbols, such as euro and pound sterling signs, quotation marks other than the plain double quote, and dashes or hyphens.
It is great for computer jobs like looking up a mailbox name. It is not so great for sorting a phone book in every language, because every language has its own ideas about ABC order.
§4Security Considerations
The security considerations of UTF-8, stringprep and the Unicode security report apply and are normative. Results vary by implementation, and implementations MUST consider whether that matters for their use case.
- New Unicode characters may have decomposition or titlecase properties that an implementation built on an older Unicode revision does not know.
- An implementation MAY use an NFKD library routine that also normalizes out-of-order diacriticals, which matters only for ill-formed UTF-8 input.
- The set of charsets in step 1 is open-ended. UTF-8, and by extension US-ASCII, are the only mandatory-to-implement charsets. Implementations SHOULD support as many likely charsets as feasible, to avoid poor collation from the fall-through to binary comparison.
- Other charsets may gain new characters over time.
An attacker may craft ill-formed input or input in an unknown charset to affect the result. Even well-formed input can cause surprises: substituting U+0391 (Greek capital alpha) or U+0410 (Cyrillic capital A) for U+0041 can prevent a match between strings that look the same.
Tricksters can swap a letter for a look-alike from another alphabet. The two words look the same to you, but the computer sees them as different.
§5Registration
The collation is registered in the collation registry defined in RFC 4790. The registration gives the identifier i;unicode-casemap, the title 'Unicode Casemap', global scope, common intended use, and the operations equality, order and substring. The owner is the IETF.
It was written into an official list of comparison rules, with its name and what it can do.