Top Description Inners Constructors Methods
com.ibm.icu.text

public abstract Class Normalizer2

extends Object
Class Inheritance
Known Direct Subclasses
com.ibm.icu.impl.Norm2AllModes.NoopNormalizer2, com.ibm.icu.impl.Norm2AllModes.Normalizer2WithImpl, com.ibm.icu.text.FilteredNormalizer2
Imports
java.io.IOException, .InputStream, java.nio.ByteBuffer, com.ibm.icu.impl.ICUBinary, .Norm2AllModes, com.ibm.icu.util.ICUUncheckedIOException

Unicode normalization functionality for standard Unicode normalization or for using custom mapping tables. All instances of this class are unmodifiable/immutable. The Normalizer2 class is not intended for public subclassing.

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.

Author
Markus W. Scherer
ICU Status
Stable since ICU 4.4.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
Normalizer2.Mode

Constants for normalization modes.

Constructor Summary

AccessConstructor and Description
protected
Normalizer2()

Deprecated This API is ICU internal only.
Sole constructor.

Method Summary

Modifier and TypeMethod and Description
public abstract StringBuilder

Returns:

first
append
(StringBuilder
string, should be normalized
first
,
CharSequence
string, should be normalized
second
)

Appends the second string to the first string (merging them at the boundary) and returns the first string.

public int

Returns:

The non-negative composite code point if there is one; otherwise a negative value.
composePair
(int
A (normalization starter) code point.
a
,
int
Another code point.
b
)

Performs pairwise composition of a & b and returns the composite if there is one.

public int

Returns:

c's combining class
getCombiningClass
(int
code point
c
)

Gets the combining class of c.

public abstract String

Returns:

c's decomposition mapping, if any; otherwise null
getDecomposition
(int
code point
c
)

Gets the decomposition mapping of c.

public static Normalizer2

Returns:

the requested Normalizer2, if successful
getInstance
(InputStream
the binary, big-endian normalization (.nrm file) data, or null for ICU data
data
,
String
"nfc" or "nfkc" or "nfkc_cf" or "nfkc_scf" or name of custom data file
name
,
Normalizer2.Mode
normalization mode (compose or decompose etc.)
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.

public static Normalizer2

Returns:

the requested Normalizer2, if successful
getNFCInstance
()

Returns a Normalizer2 instance for Unicode NFC normalization.

public static Normalizer2

Returns:

the requested Normalizer2, if successful
getNFDInstance
()

Returns a Normalizer2 instance for Unicode NFD normalization.

public static Normalizer2

Returns:

the requested Normalizer2, if successful
getNFKCCasefoldInstance
()

Returns a Normalizer2 instance for Unicode toNFKC_Casefold() normalization which is equivalent to applying the NFKC_Casefold mappings and then NFC.

public static Normalizer2

Returns:

the requested Normalizer2, if successful
getNFKCInstance
()

Returns a Normalizer2 instance for Unicode NFKC normalization.

public static Normalizer2

Returns:

the requested Normalizer2, if successful
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.

public static Normalizer2

Returns:

the requested Normalizer2, if successful
getNFKDInstance
()

Returns a Normalizer2 instance for Unicode NFKD normalization.

public String

Returns:

c's raw decomposition mapping, if any; otherwise null
getRawDecomposition
(int
code point
c
)

Gets the raw decomposition mapping of c.

public abstract boolean

Returns:

true if c has a normalization boundary after it
hasBoundaryAfter
(int
character to test
c
)

Tests if the character always has a normalization boundary after it, regardless of context.

public abstract boolean

Returns:

true if c has a normalization boundary before it
hasBoundaryBefore
(int
character to test
c
)

Tests if the character always has a normalization boundary before it, regardless of context.

public abstract boolean

Returns:

true if c is normalization-inert
isInert
(int
character to test
c
)

Tests if the character is normalization-inert.

public abstract boolean

Returns:

true if s is normalized
isNormalized
(CharSequence
input string
s
)

Tests if the string is normalized.

public String

Returns:

normalized src
normalize
(CharSequence
source string
src
)

Returns the normalized form of the source string.

public abstract StringBuilder

Returns:

dest
normalize
(CharSequence
source string
src
,
StringBuilder
destination string; its contents is replaced with normalized src
dest
)

Writes the normalized form of the source string to the destination string (replacing its contents) and returns the destination string.

public abstract Appendable

Returns:

dest
normalize
(CharSequence
source string
src
,
Appendable
destination Appendable; gets normalized src appended
dest
)

Writes the normalized form of the source string to the destination Appendable and returns the destination Appendable.

public abstract StringBuilder

Returns:

first
normalizeSecondAndAppend
(StringBuilder
string, should be normalized
first
,
CharSequence
string, will be normalized
second
)

Appends the normalized form of the second string to the first string (merging them at the boundary) and returns the first string.

public abstract Normalizer.QuickCheckResult

Returns:

the quick check result
quickCheck
(CharSequence
input string
s
)

Tests if the string is normalized.

public abstract int

Returns:

"yes" span end index
spanQuickCheckYes
(CharSequence
input string
s
)

Returns the end of the normalized substring of the input string.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Normalizer2back to summary
protected Normalizer2()

Deprecated

This API is ICU internal only.

Sole constructor. (For invocation by subclass constructors, typically implicit.)

Annotations
@Deprecated
ICU Status
Internal ICU component.

Method Detail

appendback to summary
public 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. The result is normalized if both the strings were normalized. The first and second strings must be different objects.

Parameters
first:StringBuilder

string, should be normalized

second:CharSequence

string, should be normalized

Returns:StringBuilder

first

ICU Status
Stable since ICU 4.4.
composePairback to summary
public int composePair(int a, int b)

Performs pairwise composition of a & b and returns the composite if there is one.

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.

Parameters
a:int

A (normalization starter) code point.

b:int

Another code point.

Returns:int

The non-negative composite code point if there is one; otherwise a negative value.

ICU Status
Stable since ICU 49.
getCombiningClassback to summary
public int getCombiningClass(int c)

Gets the combining class of c. The default implementation returns 0 but all standard implementations return the Unicode Canonical_Combining_Class value.

Parameters
c:int

code point

Returns:int

c's combining class

ICU Status
Stable since ICU 49.
getDecompositionback to summary
public abstract String getDecomposition(int c)

Gets the decomposition mapping of c. Roughly equivalent to normalizing the String form of c on a DECOMPOSE Normalizer2 instance, but much faster, and except that this function returns null if c does not have a decomposition mapping in this instance's data. This function is independent of the mode of the Normalizer2.

Parameters
c:int

code point

Returns:String

c's decomposition mapping, if any; otherwise null

ICU Status
Stable since ICU 4.6.
getInstanceback to summary
public 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. Returns an unmodifiable singleton instance.

  • Use data=null for data files that are part of ICU's own data.
  • Use name="nfc" and COMPOSE/DECOMPOSE for Unicode standard NFC/NFD.
  • Use name="nfkc" and COMPOSE/DECOMPOSE for Unicode standard NFKC/NFKD.
  • Use name="nfkc_cf" and COMPOSE for Unicode standard NFKC_CF=NFKC_Casefold.
If data!=null, then the binary data is read once and cached using the provided name as the key. If you know or expect the data to be cached already, you can use data!=null for non-ICU data as well.

Any java.io.IOException is wrapped into a com.ibm.icu.util.ICUUncheckedIOException.

Parameters
data:InputStream

the binary, big-endian normalization (.nrm file) data, or null for ICU data

name:String

"nfc" or "nfkc" or "nfkc_cf" or "nfkc_scf" or name of custom data file

mode:Normalizer2.Mode

normalization mode (compose or decompose etc.)

Returns:Normalizer2

the requested Normalizer2, if successful

ICU Status
Stable since ICU 4.4.
getNFCInstanceback to summary
public static Normalizer2 getNFCInstance()

Returns a Normalizer2 instance for Unicode NFC normalization. Same as getInstance(null, "nfc", Mode.COMPOSE). Returns an unmodifiable singleton instance.

Returns:Normalizer2

the requested Normalizer2, if successful

ICU Status
Stable since ICU 49.
getNFDInstanceback to summary
public static Normalizer2 getNFDInstance()

Returns a Normalizer2 instance for Unicode NFD normalization. Same as getInstance(null, "nfc", Mode.DECOMPOSE). Returns an unmodifiable singleton instance.

Returns:Normalizer2

the requested Normalizer2, if successful

ICU Status
Stable since ICU 49.
getNFKCCasefoldInstanceback to summary
public static Normalizer2 getNFKCCasefoldInstance()

Returns a Normalizer2 instance for Unicode toNFKC_Casefold() normalization which is equivalent to applying the NFKC_Casefold mappings and then NFC. See https://www.unicode.org/reports/tr44/#NFKC_Casefold

Same as getInstance(null, "nfkc_cf", Mode.COMPOSE). Returns an unmodifiable singleton instance.

Returns:Normalizer2

the requested Normalizer2, if successful

ICU Status
Stable since ICU 49.
getNFKCInstanceback to summary
public static Normalizer2 getNFKCInstance()

Returns a Normalizer2 instance for Unicode NFKC normalization. Same as getInstance(null, "nfkc", Mode.COMPOSE). Returns an unmodifiable singleton instance.

Returns:Normalizer2

the requested Normalizer2, if successful

ICU Status
Stable since ICU 49.
getNFKCSimpleCasefoldInstanceback to summary
public 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. See https://www.unicode.org/reports/tr44/#NFKC_Simple_Casefold

Same as getInstance(null, "nfkc_scf", Mode.COMPOSE). Returns an unmodifiable singleton instance.

Returns:Normalizer2

the requested Normalizer2, if successful

ICU Status
Draft since ICU 74.
getNFKDInstanceback to summary
public static Normalizer2 getNFKDInstance()

Returns a Normalizer2 instance for Unicode NFKD normalization. Same as getInstance(null, "nfkc", Mode.DECOMPOSE). Returns an unmodifiable singleton instance.

Returns:Normalizer2

the requested Normalizer2, if successful

ICU Status
Stable since ICU 49.
getRawDecompositionback to summary
public String getRawDecomposition(int c)

Gets the raw decomposition mapping of 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.

Parameters
c:int

code point

Returns:String

c's raw decomposition mapping, if any; otherwise null

ICU Status
Stable since ICU 49.
hasBoundaryAfterback to summary
public abstract boolean hasBoundaryAfter(int c)

Tests if the character always has a normalization boundary after it, regardless of context. If true, then the character does not normalization-interact with following characters. In other words, a string containing this character can be normalized by processing portions up to this character and after this character independently. This is used for iterative normalization. See the class documentation for details.

Note that this operation may be significantly slower than hasBoundaryBefore().

Parameters
c:int

character to test

Returns:boolean

true if c has a normalization boundary after it

ICU Status
Stable since ICU 4.4.
hasBoundaryBeforeback to summary
public abstract boolean hasBoundaryBefore(int c)

Tests if the character always has a normalization boundary before it, regardless of context. If true, then the character does not normalization-interact with preceding characters. In other words, a string containing this character can be normalized by processing portions before this character and starting from this character independently. This is used for iterative normalization. See the class documentation for details.

Parameters
c:int

character to test

Returns:boolean

true if c has a normalization boundary before it

ICU Status
Stable since ICU 4.4.
isInertback to summary
public abstract boolean isInert(int c)

Tests if the character is normalization-inert. If true, then the character does not change, nor normalization-interact with preceding or following characters. In other words, a string containing this character can be normalized by processing portions before this character and after this character independently. This is used for iterative normalization. See the class documentation for details.

Note that this operation may be significantly slower than hasBoundaryBefore().

Parameters
c:int

character to test

Returns:boolean

true if c is normalization-inert

ICU Status
Stable since ICU 4.4.
isNormalizedback to summary
public abstract boolean isNormalized(CharSequence s)

Tests if the string is normalized. Internally, in cases where the quickCheck() method would return "maybe" (which is only possible for the two COMPOSE modes) this method resolves to "yes" or "no" to provide a definitive result, at the cost of doing more work in those cases.

Parameters
s:CharSequence

input string

Returns:boolean

true if s is normalized

ICU Status
Stable since ICU 4.4.
normalizeback to summary
public String normalize(CharSequence src)

Returns the normalized form of the source string.

Parameters
src:CharSequence

source string

Returns:String

normalized src

ICU Status
Stable since ICU 4.4.
normalizeback to summary
public 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. The source and destination strings must be different objects.

Parameters
src:CharSequence

source string

dest:StringBuilder

destination string; its contents is replaced with normalized src

Returns:StringBuilder

dest

ICU Status
Stable since ICU 4.4.
normalizeback to summary
public abstract Appendable normalize(CharSequence src, Appendable dest)

Writes the normalized form of the source string to the destination Appendable and returns the destination Appendable. The source and destination strings must be different objects.

Any java.io.IOException is wrapped into a com.ibm.icu.util.ICUUncheckedIOException.

Parameters
src:CharSequence

source string

dest:Appendable

destination Appendable; gets normalized src appended

Returns:Appendable

dest

ICU Status
Stable since ICU 4.6.
normalizeSecondAndAppendback to summary
public 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. The result is normalized if the first string was normalized. The first and second strings must be different objects.

Parameters
first:StringBuilder

string, should be normalized

second:CharSequence

string, will be normalized

Returns:StringBuilder

first

ICU Status
Stable since ICU 4.4.
quickCheckback to summary
public abstract Normalizer.QuickCheckResult quickCheck(CharSequence s)

Tests if the string is normalized. For the two COMPOSE modes, the result could be "maybe" in cases that would take a little more work to resolve definitively. Use spanQuickCheckYes() and normalizeSecondAndAppend() for a faster combination of quick check + normalization, to avoid re-checking the "yes" prefix.

Parameters
s:CharSequence

input string

Returns:Normalizer.QuickCheckResult

the quick check result

ICU Status
Stable since ICU 4.4.
spanQuickCheckYesback to summary
public abstract int spanQuickCheckYes(CharSequence s)

Returns the end of the normalized substring of the input string. In other words, with 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.

Parameters
s:CharSequence

input string

Returns:int

"yes" span end index

ICU Status
Stable since ICU 4.4.
com.ibm.icu.text back to summary

public final Enum Normalizer2.Mode

extends Enum<Normalizer2.Mode>
Class Inheritance

Constants for normalization modes. For details about standard Unicode normalization forms and about the algorithms which are also used with custom mapping tables see https://www.unicode.org/reports/tr15/
ICU Status
Stable since ICU 4.4.

Field Summary

Modifier and TypeField and Description
public static final Normalizer2.Mode
COMPOSE

Decomposition followed by composition.

public static final Normalizer2.Mode
COMPOSE_CONTIGUOUS

Compose only contiguously.

public static final Normalizer2.Mode
DECOMPOSE

Map, and reorder canonically.

public static final Normalizer2.Mode
FCD

"Fast C or D" form.

Constructor Summary

AccessConstructor and Description
private
Mode()

Method Summary

Modifier and TypeMethod and Description
public static Normalizer2.Mode
public static Normalizer2.Mode[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

COMPOSEback to summary
public static final Normalizer2.Mode COMPOSE

Decomposition followed by composition. Same as standard NFC when using an "nfc" instance. Same as standard NFKC when using an "nfkc" instance. For details about standard Unicode normalization forms see https://www.unicode.org/reports/tr15/

ICU Status
Stable since ICU 4.4.
COMPOSE_CONTIGUOUSback to summary
public static final Normalizer2.Mode COMPOSE_CONTIGUOUS

Compose only contiguously. Also known as "FCC" or "Fast C Contiguous". The result will often but not always be in NFC. The result will conform to FCD which is useful for processing.
Not a standard Unicode normalization form.
For details see http://www.unicode.org/notes/tn5/#FCC

ICU Status
Stable since ICU 4.4.
DECOMPOSEback to summary
public static final Normalizer2.Mode DECOMPOSE

Map, and reorder canonically. Same as standard NFD when using an "nfc" instance. Same as standard NFKD when using an "nfkc" instance. For details about standard Unicode normalization forms see https://www.unicode.org/reports/tr15/

ICU Status
Stable since ICU 4.4.
FCDback to summary
public static final Normalizer2.Mode FCD

"Fast C or D" form. If a string is in this form, then further decomposition without reordering would yield the same form as DECOMPOSE. Text in "Fast C or D" form can be processed efficiently with data tables that are "canonically closed", that is, that provide equivalent data for equivalent text, without having to be fully normalized.
Not a standard Unicode normalization form.
Not a unique form: Different FCD strings can be canonically equivalent.
For details see http://www.unicode.org/notes/tn5/#FCD

ICU Status
Stable since ICU 4.4.

Constructor Detail

Modeback to summary
private Mode()

Method Detail

valueOfback to summary
public static Normalizer2.Mode valueOf(String name)
valuesback to summary
public static Normalizer2.Mode[] values()