public abstract class Normalizer2 extends Object
The primary functions are to produce a normalized string and to detect whether a string is already normalized. The most commonly used normalization forms are those defined in https://www.unicode.org/reports/tr15/ However, this API supports additional normalization forms for specialized purposes. For example, NFKC_Casefold is provided via getInstance("nfkc_cf", COMPOSE) and can be used in implementations of UTS #46.
Not only are the standard compose and decompose modes supplied, but additional modes are provided as documented in the Mode enum.
Some of the functions in this class identify normalization boundaries. At a normalization boundary, the portions of the string before it and starting from it do not interact and can be handled independently.
The spanQuickCheckYes() stops at a normalization boundary. When the goal is a normalized string, then the text before the boundary can be copied, and the remainder can be processed with normalizeSecondAndAppend().
The hasBoundaryBefore(), hasBoundaryAfter() and isInert() functions test whether a character is guaranteed to be at a normalization boundary, regardless of context. This is used for moving from one normalization boundary to the next or preceding boundary, and for performing iterative normalization.
Iterative normalization is useful when only a small portion of a longer string needs to be processed. For example, in ICU, iterative normalization is used by the NormalizationTransliterator (to avoid replacing already-normalized text) and ucol_nextSortKeyPart() (to process only the substring for which sort key bytes are computed).
The set of normalization boundaries returned by these functions may not be complete: There may be more boundaries that could be returned. Different functions may return different boundaries.
Modifier and Type | Class and Description |
---|---|
static class |
Normalizer2.Mode
Constants for normalization modes.
|
Modifier | Constructor and Description |
---|---|
protected |
Normalizer2()
Deprecated.
This API is ICU internal only.
|
Modifier and Type | Method and Description |
---|---|
abstract StringBuilder |
append(StringBuilder first,
CharSequence second)
Appends the second string to the first string
(merging them at the boundary) and returns the first string.
|
int |
composePair(int a,
int b)
Performs pairwise composition of a & b and returns the composite if there is one.
|
int |
getCombiningClass(int c)
Gets the combining class of c.
|
abstract String |
getDecomposition(int c)
Gets the decomposition mapping of c.
|
static Normalizer2 |
getInstance(InputStream data,
String name,
Normalizer2.Mode mode)
Returns a Normalizer2 instance which uses the specified data file
(an ICU data file if data=null, or else custom binary data)
and which composes or decomposes text according to the specified mode.
|
static Normalizer2 |
getNFCInstance()
Returns a Normalizer2 instance for Unicode NFC normalization.
|
static Normalizer2 |
getNFDInstance()
Returns a Normalizer2 instance for Unicode NFD normalization.
|
static Normalizer2 |
getNFKCCasefoldInstance()
Returns a Normalizer2 instance for Unicode toNFKC_Casefold() normalization
which is equivalent to applying the NFKC_Casefold mappings and then NFC.
|
static Normalizer2 |
getNFKCInstance()
Returns a Normalizer2 instance for Unicode NFKC normalization.
|
static Normalizer2 |
getNFKCSimpleCasefoldInstance()
Returns a Normalizer2 instance for a variant of Unicode toNFKC_Casefold() normalization
which is equivalent to applying the NFKC_Simple_Casefold mappings and then NFC.
|
static Normalizer2 |
getNFKDInstance()
Returns a Normalizer2 instance for Unicode NFKD normalization.
|
String |
getRawDecomposition(int c)
Gets the raw decomposition mapping of c.
|
abstract boolean |
hasBoundaryAfter(int c)
Tests if the character always has a normalization boundary after it,
regardless of context.
|
abstract boolean |
hasBoundaryBefore(int c)
Tests if the character always has a normalization boundary before it,
regardless of context.
|
abstract boolean |
isInert(int c)
Tests if the character is normalization-inert.
|
abstract boolean |
isNormalized(CharSequence s)
Tests if the string is normalized.
|
String |
normalize(CharSequence src)
Returns the normalized form of the source string.
|
abstract Appendable |
normalize(CharSequence src,
Appendable dest)
Writes the normalized form of the source string to the destination Appendable
and returns the destination Appendable.
|
abstract StringBuilder |
normalize(CharSequence src,
StringBuilder dest)
Writes the normalized form of the source string to the destination string
(replacing its contents) and returns the destination string.
|
abstract StringBuilder |
normalizeSecondAndAppend(StringBuilder first,
CharSequence second)
Appends the normalized form of the second string to the first string
(merging them at the boundary) and returns the first string.
|
abstract Normalizer.QuickCheckResult |
quickCheck(CharSequence s)
Tests if the string is normalized.
|
abstract int |
spanQuickCheckYes(CharSequence s)
Returns the end of the normalized substring of the input string.
|
@Deprecated protected Normalizer2()
public static Normalizer2 getNFCInstance()
public static Normalizer2 getNFDInstance()
public static Normalizer2 getNFKCInstance()
public static Normalizer2 getNFKDInstance()
public static Normalizer2 getNFKCCasefoldInstance()
Same as getInstance(null, "nfkc_cf", Mode.COMPOSE). Returns an unmodifiable singleton instance.
public static Normalizer2 getNFKCSimpleCasefoldInstance()
Same as getInstance(null, "nfkc_scf", Mode.COMPOSE). Returns an unmodifiable singleton instance.
public static Normalizer2 getInstance(InputStream data, String name, Normalizer2.Mode mode)
Any IOException
is wrapped into a ICUUncheckedIOException
.
data
- the binary, big-endian normalization (.nrm file) data, or null for ICU dataname
- "nfc" or "nfkc" or "nfkc_cf" or "nfkc_scf" or name of custom data filemode
- normalization mode (compose or decompose etc.)public String normalize(CharSequence src)
src
- source stringpublic abstract StringBuilder normalize(CharSequence src, StringBuilder dest)
src
- source stringdest
- destination string; its contents is replaced with normalized srcpublic abstract Appendable normalize(CharSequence src, Appendable dest)
Any IOException
is wrapped into a ICUUncheckedIOException
.
src
- source stringdest
- destination Appendable; gets normalized src appendedpublic abstract StringBuilder normalizeSecondAndAppend(StringBuilder first, CharSequence second)
first
- string, should be normalizedsecond
- string, will be normalizedpublic abstract StringBuilder append(StringBuilder first, CharSequence second)
first
- string, should be normalizedsecond
- string, should be normalizedpublic abstract String getDecomposition(int c)
c
- code pointpublic String getRawDecomposition(int c)
This is similar to the getDecomposition() method but returns the raw decomposition mapping as specified in UnicodeData.txt or (for custom data) in the mapping files processed by the gennorm2 tool. By contrast, getDecomposition() returns the processed, recursively-decomposed version of this mapping.
When used on a standard NFKC Normalizer2 instance, getRawDecomposition() returns the Unicode Decomposition_Mapping (dm) property.
When used on a standard NFC Normalizer2 instance, it returns the Decomposition_Mapping only if the Decomposition_Type (dt) is Canonical (Can); in this case, the result contains either one or two code points (=1..4 Java chars).
This function is independent of the mode of the Normalizer2. The default implementation returns null.
c
- code pointpublic int composePair(int a, int b)
Returns a composite code point c only if c has a two-way mapping to a+b. In standard Unicode normalization, this means that c has a canonical decomposition to a+b and c does not have the Full_Composition_Exclusion property.
This function is independent of the mode of the Normalizer2. The default implementation returns a negative value.
a
- A (normalization starter) code point.b
- Another code point.public int getCombiningClass(int c)
c
- code pointpublic abstract boolean isNormalized(CharSequence s)
s
- input stringpublic abstract Normalizer.QuickCheckResult quickCheck(CharSequence s)
s
- input stringpublic abstract int spanQuickCheckYes(CharSequence s)
end=spanQuickCheckYes(s);
the substring s.subSequence(0, end)
will pass the quick check with a "yes" result.
The returned end index is usually one or more characters before the "no" or "maybe" character: The end index is at a normalization boundary. (See the class documentation for more about normalization boundaries.)
When the goal is a normalized string and most input strings are expected to be normalized already, then call this method, and if it returns a prefix shorter than the input string, copy that prefix and use normalizeSecondAndAppend() for the remainder.
s
- input stringpublic abstract boolean hasBoundaryBefore(int c)
c
- character to testpublic abstract boolean hasBoundaryAfter(int c)
Note that this operation may be significantly slower than hasBoundaryBefore().
c
- character to testpublic abstract boolean isInert(int c)
Note that this operation may be significantly slower than hasBoundaryBefore().
c
- character to testCopyright © 2016 Unicode, Inc. and others.