• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GENERATED SOURCE. DO NOT MODIFY. */
2 // © 2018 and later: Unicode, Inc. and others.
3 // License & terms of use: http://www.unicode.org/copyright.html#License
4 
5 package ohos.global.icu.lang;
6 
7 import ohos.global.icu.impl.CharacterPropertiesImpl;
8 import ohos.global.icu.text.UnicodeSet;
9 import ohos.global.icu.util.CodePointMap;
10 import ohos.global.icu.util.CodePointTrie;
11 import ohos.global.icu.util.MutableCodePointTrie;
12 
13 /**
14  * Sets and maps for Unicode properties.
15  * The methods here return an object per property:
16  * A set for each ICU-supported binary property with all code points for which the property is true.
17  * A map for each ICU-supported enumerated/catalog/int-valued property
18  * which maps all Unicode code points to their values for that property.
19  *
20  * <p>For details see the method descriptions.
21  * For lookup of property values by code point see class {@link UCharacter}.
22  *
23  * @hide exposed on OHOS
24  */
25 public final class CharacterProperties {
CharacterProperties()26     private CharacterProperties() {}  // all-static
27 
28     private static final UnicodeSet sets[] = new UnicodeSet[UProperty.BINARY_LIMIT];
29     private static final CodePointMap maps[] = new CodePointMap[UProperty.INT_LIMIT - UProperty.INT_START];
30 
makeSet(int property)31     private static UnicodeSet makeSet(int property) {
32         UnicodeSet set = new UnicodeSet();
33         UnicodeSet inclusions = CharacterPropertiesImpl.getInclusionsForProperty(property);
34         int numRanges = inclusions.getRangeCount();
35         int startHasProperty = -1;
36 
37         for (int i = 0; i < numRanges; ++i) {
38             int rangeEnd = inclusions.getRangeEnd(i);
39             for (int c = inclusions.getRangeStart(i); c <= rangeEnd; ++c) {
40                 // TODO: Get a UCharacterProperty.BinaryProperty to avoid the property dispatch.
41                 if (UCharacter.hasBinaryProperty(c, property)) {
42                     if (startHasProperty < 0) {
43                         // Transition from false to true.
44                         startHasProperty = c;
45                     }
46                 } else if (startHasProperty >= 0) {
47                     // Transition from true to false.
48                     set.add(startHasProperty, c - 1);
49                     startHasProperty = -1;
50                 }
51             }
52         }
53         if (startHasProperty >= 0) {
54             set.add(startHasProperty, 0x10FFFF);
55         }
56 
57         return set.freeze();
58     }
59 
makeMap(int property)60     private static CodePointMap makeMap(int property) {
61         int nullValue = property == UProperty.SCRIPT ? UScript.UNKNOWN : 0;
62         MutableCodePointTrie mutableTrie = new MutableCodePointTrie(nullValue, nullValue);
63         UnicodeSet inclusions = CharacterPropertiesImpl.getInclusionsForProperty(property);
64         int numRanges = inclusions.getRangeCount();
65         int start = 0;
66         int value = nullValue;
67 
68         for (int i = 0; i < numRanges; ++i) {
69             int rangeEnd = inclusions.getRangeEnd(i);
70             for (int c = inclusions.getRangeStart(i); c <= rangeEnd; ++c) {
71                 // TODO: Get a UCharacterProperty.IntProperty to avoid the property dispatch.
72                 int nextValue = UCharacter.getIntPropertyValue(c, property);
73                 if (value != nextValue) {
74                     if (value != nullValue) {
75                         mutableTrie.setRange(start, c - 1, value);
76                     }
77                     start = c;
78                     value = nextValue;
79                 }
80             }
81         }
82         if (value != 0) {
83             mutableTrie.setRange(start, 0x10FFFF, value);
84         }
85 
86         CodePointTrie.Type type;
87         if (property == UProperty.BIDI_CLASS || property == UProperty.GENERAL_CATEGORY) {
88             type = CodePointTrie.Type.FAST;
89         } else {
90             type = CodePointTrie.Type.SMALL;
91         }
92         CodePointTrie.ValueWidth valueWidth;
93         // TODO: UCharacterProperty.IntProperty
94         int max = UCharacter.getIntPropertyMaxValue(property);
95         if (max <= 0xff) {
96             valueWidth = CodePointTrie.ValueWidth.BITS_8;
97         } else if (max <= 0xffff) {
98             valueWidth = CodePointTrie.ValueWidth.BITS_16;
99         } else {
100             valueWidth = CodePointTrie.ValueWidth.BITS_32;
101         }
102         return mutableTrie.buildImmutable(type, valueWidth);
103     }
104 
105     /**
106      * Returns a frozen UnicodeSet for a binary property.
107      * Throws an exception if the property number is not one for a binary property.
108      *
109      * <p>The returned set contains all code points for which the property is true.
110      *
111      * @param property {@link UProperty#BINARY_START}..{@link UProperty#BINARY_LIMIT}-1
112      * @return the property as a set
113      * @see UProperty
114      * @see UCharacter#hasBinaryProperty
115      */
getBinaryPropertySet(int property)116     public static final UnicodeSet getBinaryPropertySet(int property) {
117         if (property < 0 || UProperty.BINARY_LIMIT <= property) {
118             throw new IllegalArgumentException("" + property +
119                     " is not a constant for a UProperty binary property");
120         }
121         synchronized(sets) {
122             UnicodeSet set = sets[property];
123             if (set == null) {
124                 sets[property] = set = makeSet(property);
125             }
126             return set;
127         }
128     }
129 
130     /**
131      * Returns an immutable CodePointMap for an enumerated/catalog/int-valued property.
132      * Throws an exception if the property number is not one for an "int property".
133      *
134      * <p>The returned object maps all Unicode code points to their values for that property.
135      * For documentation of the integer values see {@link UCharacter#getIntPropertyValue(int, int)}.
136      *
137      * <p>The actual type of the returned object differs between properties
138      * and may change over time.
139      *
140      * @param property {@link UProperty#INT_START}..{@link UProperty#INT_LIMIT}-1
141      * @return the property as a map
142      * @see UProperty
143      * @see UCharacter#getIntPropertyValue
144      */
getIntPropertyMap(int property)145     public static final CodePointMap getIntPropertyMap(int property) {
146         if (property < UProperty.INT_START || UProperty.INT_LIMIT <= property) {
147             throw new IllegalArgumentException("" + property +
148                     " is not a constant for a UProperty int property");
149         }
150         synchronized(maps) {
151             CodePointMap map = maps[property - UProperty.INT_START];
152             if (map == null) {
153                 maps[property - UProperty.INT_START] = map = makeMap(property);
154             }
155             return map;
156         }
157     }
158 }
159