• 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 package ohos.global.icu.impl;
5 
6 import ohos.global.icu.lang.UCharacter;
7 import ohos.global.icu.lang.UProperty;
8 import ohos.global.icu.text.UnicodeSet;
9 
10 /**
11  * Properties functionality above class UCharacterProperty
12  * but below class CharacterProperties and class UnicodeSet.
13  * @hide exposed on OHOS
14  */
15 public final class CharacterPropertiesImpl {
16     private static final int NUM_INCLUSIONS = UCharacterProperty.SRC_COUNT +
17             UProperty.INT_LIMIT - UProperty.INT_START;
18 
19     /**
20      * A set of all characters _except_ the second through last characters of
21      * certain ranges. These ranges are ranges of characters whose
22      * properties are all exactly alike, e.g. CJK Ideographs from
23      * U+4E00 to U+9FA5.
24      */
25     private static final UnicodeSet inclusions[] = new UnicodeSet[NUM_INCLUSIONS];
26 
27     /** For {@link UnicodeSet#setDefaultXSymbolTable}. */
clear()28     public static synchronized void clear() {
29         for (int i = 0; i < inclusions.length; ++i) {
30             inclusions[i] = null;
31         }
32     }
33 
getInclusionsForSource(int src)34     private static UnicodeSet getInclusionsForSource(int src) {
35         if (inclusions[src] == null) {
36             UnicodeSet incl = new UnicodeSet();
37             switch(src) {
38             case UCharacterProperty.SRC_CHAR:
39                 UCharacterProperty.INSTANCE.addPropertyStarts(incl);
40                 break;
41             case UCharacterProperty.SRC_PROPSVEC:
42                 UCharacterProperty.INSTANCE.upropsvec_addPropertyStarts(incl);
43                 break;
44             case UCharacterProperty.SRC_CHAR_AND_PROPSVEC:
45                 UCharacterProperty.INSTANCE.addPropertyStarts(incl);
46                 UCharacterProperty.INSTANCE.upropsvec_addPropertyStarts(incl);
47                 break;
48             case UCharacterProperty.SRC_CASE_AND_NORM:
49                 Norm2AllModes.getNFCInstance().impl.addPropertyStarts(incl);
50                 UCaseProps.INSTANCE.addPropertyStarts(incl);
51                 break;
52             case UCharacterProperty.SRC_NFC:
53                 Norm2AllModes.getNFCInstance().impl.addPropertyStarts(incl);
54                 break;
55             case UCharacterProperty.SRC_NFKC:
56                 Norm2AllModes.getNFKCInstance().impl.addPropertyStarts(incl);
57                 break;
58             case UCharacterProperty.SRC_NFKC_CF:
59                 Norm2AllModes.getNFKC_CFInstance().impl.addPropertyStarts(incl);
60                 break;
61             case UCharacterProperty.SRC_NFC_CANON_ITER:
62                 Norm2AllModes.getNFCInstance().impl.addCanonIterPropertyStarts(incl);
63                 break;
64             case UCharacterProperty.SRC_CASE:
65                 UCaseProps.INSTANCE.addPropertyStarts(incl);
66                 break;
67             case UCharacterProperty.SRC_BIDI:
68                 UBiDiProps.INSTANCE.addPropertyStarts(incl);
69                 break;
70             case UCharacterProperty.SRC_INPC:
71             case UCharacterProperty.SRC_INSC:
72             case UCharacterProperty.SRC_VO:
73                 UCharacterProperty.ulayout_addPropertyStarts(src, incl);
74                 break;
75             default:
76                 throw new IllegalStateException("getInclusions(unknown src " + src + ")");
77             }
78             // We do not freeze() the set because we only iterate over it,
79             // rather than testing contains(),
80             // so the extra time and memory to optimize that are not necessary.
81             inclusions[src] = incl.compact();
82         }
83         return inclusions[src];
84     }
85 
getIntPropInclusions(int prop)86     private static UnicodeSet getIntPropInclusions(int prop) {
87         assert(UProperty.INT_START <= prop && prop < UProperty.INT_LIMIT);
88         int inclIndex = UCharacterProperty.SRC_COUNT + prop - UProperty.INT_START;
89         if (inclusions[inclIndex] != null) {
90             return inclusions[inclIndex];
91         }
92         int src = UCharacterProperty.INSTANCE.getSource(prop);
93         UnicodeSet incl = getInclusionsForSource(src);
94 
95         UnicodeSet intPropIncl = new UnicodeSet(0, 0);
96         int numRanges = incl.getRangeCount();
97         int prevValue = 0;
98         for (int i = 0; i < numRanges; ++i) {
99             int rangeEnd = incl.getRangeEnd(i);
100             for (int c = incl.getRangeStart(i); c <= rangeEnd; ++c) {
101                 // TODO: Get a UCharacterProperty.IntProperty to avoid the property dispatch.
102                 int value = UCharacter.getIntPropertyValue(c, prop);
103                 if (value != prevValue) {
104                     intPropIncl.add(c);
105                     prevValue = value;
106                 }
107             }
108         }
109 
110         // Compact for caching.
111         return inclusions[inclIndex] = intPropIncl.compact();
112     }
113 
114     /**
115      * Returns a mutable UnicodeSet -- do not modify!
116      */
getInclusionsForProperty(int prop)117     public static synchronized UnicodeSet getInclusionsForProperty(int prop) {
118         if (UProperty.INT_START <= prop && prop < UProperty.INT_LIMIT) {
119             return getIntPropInclusions(prop);
120         } else {
121             int src = UCharacterProperty.INSTANCE.getSource(prop);
122             return getInclusionsForSource(src);
123         }
124     }
125 }
126