Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/text/Normalizer.java
38829 views
/*1* Copyright (c) 2005, 2013, 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 sun.text.normalizer.NormalizerBase;40import sun.text.normalizer.NormalizerImpl;4142/**43* This class provides the method <code>normalize</code> which transforms Unicode44* text into an equivalent composed or decomposed form, allowing for easier45* sorting and searching of text.46* The <code>normalize</code> method supports the standard normalization forms47* described in48* <a href="http://www.unicode.org/unicode/reports/tr15/tr15-23.html">49* Unicode Standard Annex #15 — Unicode Normalization Forms</a>.50* <p>51* Characters with accents or other adornments can be encoded in52* several different ways in Unicode. For example, take the character A-acute.53* In Unicode, this can be encoded as a single character (the "composed" form):54*55* <pre>56* U+00C1 LATIN CAPITAL LETTER A WITH ACUTE</pre>57*58* or as two separate characters (the "decomposed" form):59*60* <pre>61* U+0041 LATIN CAPITAL LETTER A62* U+0301 COMBINING ACUTE ACCENT</pre>63*64* To a user of your program, however, both of these sequences should be65* treated as the same "user-level" character "A with acute accent". When you66* are searching or comparing text, you must ensure that these two sequences are67* treated as equivalent. In addition, you must handle characters with more than68* one accent. Sometimes the order of a character's combining accents is69* significant, while in other cases accent sequences in different orders are70* really equivalent.71* <p>72* Similarly, the string "ffi" can be encoded as three separate letters:73*74* <pre>75* U+0066 LATIN SMALL LETTER F76* U+0066 LATIN SMALL LETTER F77* U+0069 LATIN SMALL LETTER I</pre>78*79* or as the single character80*81* <pre>82* U+FB03 LATIN SMALL LIGATURE FFI</pre>83*84* The ffi ligature is not a distinct semantic character, and strictly speaking85* it shouldn't be in Unicode at all, but it was included for compatibility86* with existing character sets that already provided it. The Unicode standard87* identifies such characters by giving them "compatibility" decompositions88* into the corresponding semantic characters. When sorting and searching, you89* will often want to use these mappings.90* <p>91* The <code>normalize</code> method helps solve these problems by transforming92* text into the canonical composed and decomposed forms as shown in the first93* example above. In addition, you can have it perform compatibility94* decompositions so that you can treat compatibility characters the same as95* their equivalents.96* Finally, the <code>normalize</code> method rearranges accents into the97* proper canonical order, so that you do not have to worry about accent98* rearrangement on your own.99* <p>100* The W3C generally recommends to exchange texts in NFC.101* Note also that most legacy character encodings use only precomposed forms and102* often do not encode any combining marks by themselves. For conversion to such103* character encodings the Unicode text needs to be normalized to NFC.104* For more usage examples, see the Unicode Standard Annex.105*106* @since 1.6107*/108public final class Normalizer {109110private Normalizer() {};111112/**113* This enum provides constants of the four Unicode normalization forms114* that are described in115* <a href="http://www.unicode.org/unicode/reports/tr15/tr15-23.html">116* Unicode Standard Annex #15 — Unicode Normalization Forms</a>117* and two methods to access them.118*119* @since 1.6120*/121public static enum Form {122123/**124* Canonical decomposition.125*/126NFD,127128/**129* Canonical decomposition, followed by canonical composition.130*/131NFC,132133/**134* Compatibility decomposition.135*/136NFKD,137138/**139* Compatibility decomposition, followed by canonical composition.140*/141NFKC142}143144/**145* Normalize a sequence of char values.146* The sequence will be normalized according to the specified normalization147* from.148* @param src The sequence of char values to normalize.149* @param form The normalization form; one of150* {@link java.text.Normalizer.Form#NFC},151* {@link java.text.Normalizer.Form#NFD},152* {@link java.text.Normalizer.Form#NFKC},153* {@link java.text.Normalizer.Form#NFKD}154* @return The normalized String155* @throws NullPointerException If <code>src</code> or <code>form</code>156* is null.157*/158public static String normalize(CharSequence src, Form form) {159return NormalizerBase.normalize(src.toString(), form);160}161162/**163* Determines if the given sequence of char values is normalized.164* @param src The sequence of char values to be checked.165* @param form The normalization form; one of166* {@link java.text.Normalizer.Form#NFC},167* {@link java.text.Normalizer.Form#NFD},168* {@link java.text.Normalizer.Form#NFKC},169* {@link java.text.Normalizer.Form#NFKD}170* @return true if the sequence of char values is normalized;171* false otherwise.172* @throws NullPointerException If <code>src</code> or <code>form</code>173* is null.174*/175public static boolean isNormalized(CharSequence src, Form form) {176return NormalizerBase.isNormalized(src.toString(), form);177}178}179180181