Path: blob/master/src/java.base/share/classes/jdk/internal/icu/impl/UBiDiProps.java
41161 views
/*1* Copyright (c) 2005, 2020, 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*/24/*25*******************************************************************************26*27* Copyright (C) 2004-2014, International Business Machines28* Corporation and others. All Rights Reserved.29*30*******************************************************************************31* file name: UBiDiProps.java32* encoding: US-ASCII33* tab size: 8 (not used)34* indentation:435*36* created on: 2005jan1637* created by: Markus W. Scherer38*39* Low-level Unicode bidi/shaping properties access.40* Java port of ubidi_props.h/.c.41*/4243package jdk.internal.icu.impl;4445import jdk.internal.icu.lang.UCharacter;46import jdk.internal.icu.util.VersionInfo;4748import java.io.IOException;49import java.nio.ByteBuffer;50import java.util.MissingResourceException;5152public final class UBiDiProps {53// constructors etc. --------------------------------------------------- ***5455// port of ubidi_openProps()56private UBiDiProps() throws IOException{57ByteBuffer bytes=ICUBinary.getRequiredData(DATA_FILE_NAME);58readData(bytes);59}6061private void readData(ByteBuffer bytes) throws IOException {62// read the header63ICUBinary.readHeader(bytes, FMT, new IsAcceptable());6465// read indexes[]66int i, count;67count=bytes.getInt();68if(count<IX_TOP) {69throw new IOException("indexes[0] too small in "+DATA_FILE_NAME);70}71indexes=new int[count];7273indexes[0]=count;74for(i=1; i<count; ++i) {75indexes[i]=bytes.getInt();76}7778// read the trie79trie=Trie2_16.createFromSerialized(bytes);80int expectedTrieLength=indexes[IX_TRIE_SIZE];81int trieLength=trie.getSerializedLength();82if(trieLength>expectedTrieLength) {83throw new IOException(DATA_FILE_NAME+": not enough bytes for the trie");84}85// skip padding after trie bytes86ICUBinary.skipBytes(bytes, expectedTrieLength-trieLength);8788// read mirrors[]89count=indexes[IX_MIRROR_LENGTH];90if(count>0) {91mirrors=new int[count];92for(i=0; i<count; ++i) {93mirrors[i]=bytes.getInt();94}95}9697// read jgArray[]98count=indexes[IX_JG_LIMIT]-indexes[IX_JG_START];99jgArray=new byte[count];100for(i=0; i<count; ++i) {101jgArray[i]=bytes.get();102}103104// read jgArray2[]105count=indexes[IX_JG_LIMIT2]-indexes[IX_JG_START2];106jgArray2=new byte[count];107for(i=0; i<count; ++i) {108jgArray2[i]=bytes.get();109}110}111112// implement ICUBinary.Authenticate113private static final class IsAcceptable implements ICUBinary.Authenticate {114public boolean isDataVersionAcceptable(byte version[]) {115return version[0]==2;116}117}118119// property access functions ------------------------------------------- ***120121public final int getClass(int c) {122return getClassFromProps(trie.get(c));123}124125private final int getMirror(int c, int props) {126int delta=getMirrorDeltaFromProps(props);127if(delta!=ESC_MIRROR_DELTA) {128return c+delta;129} else {130/* look for mirror code point in the mirrors[] table */131int m;132int i, length;133int c2;134135length=indexes[IX_MIRROR_LENGTH];136137/* linear search */138for(i=0; i<length; ++i) {139m=mirrors[i];140c2=getMirrorCodePoint(m);141if(c==c2) {142/* found c, return its mirror code point using the index in m */143return getMirrorCodePoint(mirrors[getMirrorIndex(m)]);144} else if(c<c2) {145break;146}147}148149/* c not found, return it itself */150return c;151}152}153154public final int getMirror(int c) {155int props=trie.get(c);156return getMirror(c, props);157}158159public final int getJoiningType(int c) {160return (trie.get(c)&JT_MASK)>>JT_SHIFT;161}162163public final int getJoiningGroup(int c) {164int start, limit;165166start=indexes[IX_JG_START];167limit=indexes[IX_JG_LIMIT];168if(start<=c && c<limit) {169return (int)jgArray[c-start]&0xff;170}171start=indexes[IX_JG_START2];172limit=indexes[IX_JG_LIMIT2];173if(start<=c && c<limit) {174return (int)jgArray2[c-start]&0xff;175}176return UCharacter.JoiningGroup.NO_JOINING_GROUP;177}178179public final int getPairedBracketType(int c) {180return (trie.get(c)&BPT_MASK)>>BPT_SHIFT;181}182183public final int getPairedBracket(int c) {184int props=trie.get(c);185if((props&BPT_MASK)==0) {186return c;187} else {188return getMirror(c, props);189}190}191192// data members -------------------------------------------------------- ***193private int indexes[];194private int mirrors[];195private byte jgArray[];196private byte jgArray2[];197198private Trie2_16 trie;199200// data format constants ----------------------------------------------- ***201@SuppressWarnings("deprecation")202private static final String DATA_FILE_NAME =203"/jdk/internal/icu/impl/data/icudt" +204VersionInfo.ICU_DATA_VERSION_PATH +205"/ubidi.icu";206207/* format "BiDi" */208private static final int FMT=0x42694469;209210/* indexes into indexes[] */211private static final int IX_TRIE_SIZE=2;212private static final int IX_MIRROR_LENGTH=3;213214private static final int IX_JG_START=4;215private static final int IX_JG_LIMIT=5;216private static final int IX_JG_START2=6; /* new in format version 2.2, ICU 54 */217private static final int IX_JG_LIMIT2=7;218219private static final int IX_TOP=16;220221// definitions for 16-bit bidi/shaping properties word ----------------- ***222223/* CLASS_SHIFT=0, */ /* bidi class: 5 bits (4..0) */224private static final int JT_SHIFT=5; /* joining type: 3 bits (7..5) */225226private static final int BPT_SHIFT=8; /* Bidi_Paired_Bracket_Type(bpt): 2 bits (9..8) */227228private static final int MIRROR_DELTA_SHIFT=13; /* bidi mirroring delta: 3 bits (15..13) */229230private static final int CLASS_MASK= 0x0000001f;231private static final int JT_MASK= 0x000000e0;232private static final int BPT_MASK= 0x00000300;233234private static final int getClassFromProps(int props) {235return props&CLASS_MASK;236}237private static final boolean getFlagFromProps(int props, int shift) {238return ((props>>shift)&1)!=0;239}240private static final int getMirrorDeltaFromProps(int props) {241return (short)props>>MIRROR_DELTA_SHIFT;242}243244private static final int ESC_MIRROR_DELTA=-4;245246// definitions for 32-bit mirror table entry --------------------------- ***247248/* the source Unicode code point takes 21 bits (20..0) */249private static final int MIRROR_INDEX_SHIFT=21;250251private static final int getMirrorCodePoint(int m) {252return m&0x1fffff;253}254private static final int getMirrorIndex(int m) {255return m>>>MIRROR_INDEX_SHIFT;256}257258259/*260* public singleton instance261*/262public static final UBiDiProps INSTANCE;263264// This static initializer block must be placed after265// other static member initialization266static {267try {268INSTANCE = new UBiDiProps();269} catch (IOException e) {270throw new MissingResourceException(e.getMessage(),DATA_FILE_NAME,"");271}272}273}274275276