Path: blob/master/src/java.base/share/classes/java/text/Normalizer.java
41152 views
/*1* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26*******************************************************************************27* (C) Copyright IBM Corp. 1996-2005 - All Rights Reserved *28* *29* The original version of this source code and documentation is copyrighted *30* and owned by IBM, These materials are provided under terms of a License *31* Agreement between IBM and Sun. This technology is protected by multiple *32* US and International patents. This notice and attribution to IBM may not *33* to removed. *34*******************************************************************************35*/3637package java.text;3839import jdk.internal.icu.text.NormalizerBase;4041/**42* This class provides the method {@code normalize} which transforms Unicode43* text into an equivalent composed or decomposed form, allowing for easier44* sorting and searching of text.45* The {@code normalize} method supports the standard normalization forms46* described in47* <a href="https://www.unicode.org/reports/tr15/">48* Unicode Standard Annex #15 — Unicode Normalization Forms</a>.49* <p>50* Characters with accents or other adornments can be encoded in51* several different ways in Unicode. For example, take the character A-acute.52* In Unicode, this can be encoded as a single character (the "composed" form):53*54* <pre>55* U+00C1 LATIN CAPITAL LETTER A WITH ACUTE</pre>56*57* or as two separate characters (the "decomposed" form):58*59* <pre>60* U+0041 LATIN CAPITAL LETTER A61* U+0301 COMBINING ACUTE ACCENT</pre>62*63* To a user of your program, however, both of these sequences should be64* treated as the same "user-level" character "A with acute accent". When you65* are searching or comparing text, you must ensure that these two sequences are66* treated as equivalent. In addition, you must handle characters with more than67* one accent. Sometimes the order of a character's combining accents is68* significant, while in other cases accent sequences in different orders are69* really equivalent.70* <p>71* Similarly, the string "ffi" can be encoded as three separate letters:72*73* <pre>74* U+0066 LATIN SMALL LETTER F75* U+0066 LATIN SMALL LETTER F76* U+0069 LATIN SMALL LETTER I</pre>77*78* or as the single character79*80* <pre>81* U+FB03 LATIN SMALL LIGATURE FFI</pre>82*83* The ffi ligature is not a distinct semantic character, and strictly speaking84* it shouldn't be in Unicode at all, but it was included for compatibility85* with existing character sets that already provided it. The Unicode standard86* identifies such characters by giving them "compatibility" decompositions87* into the corresponding semantic characters. When sorting and searching, you88* will often want to use these mappings.89* <p>90* The {@code normalize} method helps solve these problems by transforming91* text into the canonical composed and decomposed forms as shown in the first92* example above. In addition, you can have it perform compatibility93* decompositions so that you can treat compatibility characters the same as94* their equivalents.95* Finally, the {@code normalize} method rearranges accents into the96* proper canonical order, so that you do not have to worry about accent97* rearrangement on your own.98* <p>99* The W3C generally recommends to exchange texts in NFC.100* Note also that most legacy character encodings use only precomposed forms and101* often do not encode any combining marks by themselves. For conversion to such102* character encodings the Unicode text needs to be normalized to NFC.103* For more usage examples, see the Unicode Standard Annex.104*105* @since 1.6106*/107public final class Normalizer {108109private Normalizer() {};110111/**112* This enum provides constants of the four Unicode normalization forms113* that are described in114* <a href="https://www.unicode.org/reports/tr15/">115* Unicode Standard Annex #15 — Unicode Normalization Forms</a>116* and two methods to access them.117*118* @since 1.6119*/120public static enum Form {121122/**123* Canonical decomposition.124*/125NFD,126127/**128* Canonical decomposition, followed by canonical composition.129*/130NFC,131132/**133* Compatibility decomposition.134*/135NFKD,136137/**138* Compatibility decomposition, followed by canonical composition.139*/140NFKC141}142143/**144* Normalize a sequence of char values.145* The sequence will be normalized according to the specified normalization146* from.147* @param src The sequence of char values to normalize.148* @param form The normalization form; one of149* {@link java.text.Normalizer.Form#NFC},150* {@link java.text.Normalizer.Form#NFD},151* {@link java.text.Normalizer.Form#NFKC},152* {@link java.text.Normalizer.Form#NFKD}153* @return The normalized String154* @throws NullPointerException If {@code src} or {@code form}155* is null.156*/157public static String normalize(CharSequence src, Form form) {158return NormalizerBase.normalize(src.toString(), form);159}160161/**162* Determines if the given sequence of char values is normalized.163* @param src The sequence of char values to be checked.164* @param form The normalization form; one of165* {@link java.text.Normalizer.Form#NFC},166* {@link java.text.Normalizer.Form#NFD},167* {@link java.text.Normalizer.Form#NFKC},168* {@link java.text.Normalizer.Form#NFKD}169* @return true if the sequence of char values is normalized;170* false otherwise.171* @throws NullPointerException If {@code src} or {@code form}172* is null.173*/174public static boolean isNormalized(CharSequence src, Form form) {175return NormalizerBase.isNormalized(src.toString(), form);176}177}178179180