1 /* GENERATED SOURCE. DO NOT MODIFY. */ 2 // © 2016 and later: Unicode, Inc. and others. 3 // License & terms of use: http://www.unicode.org/copyright.html#License 4 /* 5 ******************************************************************************* 6 * Copyright (C) 2009-2016, International Business Machines 7 * Corporation and others. All Rights Reserved. 8 ******************************************************************************* 9 */ 10 11 package ohos.global.icu.impl; 12 13 import java.io.IOException; 14 import java.nio.ByteBuffer; 15 16 import ohos.global.icu.text.Normalizer; 17 import ohos.global.icu.text.Normalizer2; 18 import ohos.global.icu.util.ICUUncheckedIOException; 19 20 /** 21 * @hide exposed on OHOS 22 */ 23 public final class Norm2AllModes { 24 // Public API dispatch via Normalizer2 subclasses -------------------------- *** 25 26 // Normalizer2 implementation for the old UNORM_NONE. 27 /** 28 * @hide exposed on OHOS 29 */ 30 public static final class NoopNormalizer2 extends Normalizer2 { 31 @Override normalize(CharSequence src, StringBuilder dest)32 public StringBuilder normalize(CharSequence src, StringBuilder dest) { 33 if(dest!=src) { 34 dest.setLength(0); 35 return dest.append(src); 36 } else { 37 throw new IllegalArgumentException(); 38 } 39 } 40 @Override normalize(CharSequence src, Appendable dest)41 public Appendable normalize(CharSequence src, Appendable dest) { 42 if(dest!=src) { 43 try { 44 return dest.append(src); 45 } catch(IOException e) { 46 throw new ICUUncheckedIOException(e); // Avoid declaring "throws IOException". 47 } 48 } else { 49 throw new IllegalArgumentException(); 50 } 51 } 52 @Override normalizeSecondAndAppend(StringBuilder first, CharSequence second)53 public StringBuilder normalizeSecondAndAppend(StringBuilder first, CharSequence second) { 54 if(first!=second) { 55 return first.append(second); 56 } else { 57 throw new IllegalArgumentException(); 58 } 59 } 60 @Override append(StringBuilder first, CharSequence second)61 public StringBuilder append(StringBuilder first, CharSequence second) { 62 if(first!=second) { 63 return first.append(second); 64 } else { 65 throw new IllegalArgumentException(); 66 } 67 } 68 @Override getDecomposition(int c)69 public String getDecomposition(int c) { 70 return null; 71 } 72 // No need to override the default getRawDecomposition(). 73 @Override isNormalized(CharSequence s)74 public boolean isNormalized(CharSequence s) { return true; } 75 @Override quickCheck(CharSequence s)76 public Normalizer.QuickCheckResult quickCheck(CharSequence s) { return Normalizer.YES; } 77 @Override spanQuickCheckYes(CharSequence s)78 public int spanQuickCheckYes(CharSequence s) { return s.length(); } 79 @Override hasBoundaryBefore(int c)80 public boolean hasBoundaryBefore(int c) { return true; } 81 @Override hasBoundaryAfter(int c)82 public boolean hasBoundaryAfter(int c) { return true; } 83 @Override isInert(int c)84 public boolean isInert(int c) { return true; } 85 } 86 87 // Intermediate class: 88 // Has Normalizer2Impl and does boilerplate argument checking and setup. 89 /** 90 * @hide exposed on OHOS 91 */ 92 public static abstract class Normalizer2WithImpl extends Normalizer2 { Normalizer2WithImpl(Normalizer2Impl ni)93 public Normalizer2WithImpl(Normalizer2Impl ni) { 94 impl=ni; 95 } 96 97 // normalize 98 @Override normalize(CharSequence src, StringBuilder dest)99 public StringBuilder normalize(CharSequence src, StringBuilder dest) { 100 if(dest==src) { 101 throw new IllegalArgumentException(); 102 } 103 dest.setLength(0); 104 normalize(src, new Normalizer2Impl.ReorderingBuffer(impl, dest, src.length())); 105 return dest; 106 } 107 @Override normalize(CharSequence src, Appendable dest)108 public Appendable normalize(CharSequence src, Appendable dest) { 109 if(dest==src) { 110 throw new IllegalArgumentException(); 111 } 112 Normalizer2Impl.ReorderingBuffer buffer= 113 new Normalizer2Impl.ReorderingBuffer(impl, dest, src.length()); 114 normalize(src, buffer); 115 buffer.flush(); 116 return dest; 117 } normalize(CharSequence src, Normalizer2Impl.ReorderingBuffer buffer)118 protected abstract void normalize(CharSequence src, Normalizer2Impl.ReorderingBuffer buffer); 119 120 // normalize and append 121 @Override normalizeSecondAndAppend(StringBuilder first, CharSequence second)122 public StringBuilder normalizeSecondAndAppend(StringBuilder first, CharSequence second) { 123 return normalizeSecondAndAppend(first, second, true); 124 } 125 @Override append(StringBuilder first, CharSequence second)126 public StringBuilder append(StringBuilder first, CharSequence second) { 127 return normalizeSecondAndAppend(first, second, false); 128 } normalizeSecondAndAppend( StringBuilder first, CharSequence second, boolean doNormalize)129 public StringBuilder normalizeSecondAndAppend( 130 StringBuilder first, CharSequence second, boolean doNormalize) { 131 if(first==second) { 132 throw new IllegalArgumentException(); 133 } 134 normalizeAndAppend( 135 second, doNormalize, 136 new Normalizer2Impl.ReorderingBuffer(impl, first, first.length()+second.length())); 137 return first; 138 } normalizeAndAppend( CharSequence src, boolean doNormalize, Normalizer2Impl.ReorderingBuffer buffer)139 protected abstract void normalizeAndAppend( 140 CharSequence src, boolean doNormalize, Normalizer2Impl.ReorderingBuffer buffer); 141 142 @Override getDecomposition(int c)143 public String getDecomposition(int c) { 144 return impl.getDecomposition(c); 145 } 146 @Override getRawDecomposition(int c)147 public String getRawDecomposition(int c) { 148 return impl.getRawDecomposition(c); 149 } 150 @Override composePair(int a, int b)151 public int composePair(int a, int b) { 152 return impl.composePair(a, b); 153 } 154 155 @Override getCombiningClass(int c)156 public int getCombiningClass(int c) { 157 return impl.getCC(impl.getNorm16(c)); 158 } 159 160 // quick checks 161 @Override isNormalized(CharSequence s)162 public boolean isNormalized(CharSequence s) { 163 return s.length()==spanQuickCheckYes(s); 164 } 165 @Override quickCheck(CharSequence s)166 public Normalizer.QuickCheckResult quickCheck(CharSequence s) { 167 return isNormalized(s) ? Normalizer.YES : Normalizer.NO; 168 } 169 getQuickCheck(int c)170 public abstract int getQuickCheck(int c); 171 172 public final Normalizer2Impl impl; 173 } 174 175 /** 176 * @hide exposed on OHOS 177 */ 178 public static final class DecomposeNormalizer2 extends Normalizer2WithImpl { DecomposeNormalizer2(Normalizer2Impl ni)179 public DecomposeNormalizer2(Normalizer2Impl ni) { 180 super(ni); 181 } 182 183 @Override normalize(CharSequence src, Normalizer2Impl.ReorderingBuffer buffer)184 protected void normalize(CharSequence src, Normalizer2Impl.ReorderingBuffer buffer) { 185 impl.decompose(src, 0, src.length(), buffer); 186 } 187 @Override normalizeAndAppend( CharSequence src, boolean doNormalize, Normalizer2Impl.ReorderingBuffer buffer)188 protected void normalizeAndAppend( 189 CharSequence src, boolean doNormalize, Normalizer2Impl.ReorderingBuffer buffer) { 190 impl.decomposeAndAppend(src, doNormalize, buffer); 191 } 192 @Override spanQuickCheckYes(CharSequence s)193 public int spanQuickCheckYes(CharSequence s) { 194 return impl.decompose(s, 0, s.length(), null); 195 } 196 @Override getQuickCheck(int c)197 public int getQuickCheck(int c) { 198 return impl.isDecompYes(impl.getNorm16(c)) ? 1 : 0; 199 } 200 @Override hasBoundaryBefore(int c)201 public boolean hasBoundaryBefore(int c) { return impl.hasDecompBoundaryBefore(c); } 202 @Override hasBoundaryAfter(int c)203 public boolean hasBoundaryAfter(int c) { return impl.hasDecompBoundaryAfter(c); } 204 @Override isInert(int c)205 public boolean isInert(int c) { return impl.isDecompInert(c); } 206 } 207 208 /** 209 * @hide exposed on OHOS 210 */ 211 public static final class ComposeNormalizer2 extends Normalizer2WithImpl { ComposeNormalizer2(Normalizer2Impl ni, boolean fcc)212 public ComposeNormalizer2(Normalizer2Impl ni, boolean fcc) { 213 super(ni); 214 onlyContiguous=fcc; 215 } 216 217 @Override normalize(CharSequence src, Normalizer2Impl.ReorderingBuffer buffer)218 protected void normalize(CharSequence src, Normalizer2Impl.ReorderingBuffer buffer) { 219 impl.compose(src, 0, src.length(), onlyContiguous, true, buffer); 220 } 221 @Override normalizeAndAppend( CharSequence src, boolean doNormalize, Normalizer2Impl.ReorderingBuffer buffer)222 protected void normalizeAndAppend( 223 CharSequence src, boolean doNormalize, Normalizer2Impl.ReorderingBuffer buffer) { 224 impl.composeAndAppend(src, doNormalize, onlyContiguous, buffer); 225 } 226 227 @Override isNormalized(CharSequence s)228 public boolean isNormalized(CharSequence s) { 229 // 5: small destCapacity for substring normalization 230 return impl.compose(s, 0, s.length(), 231 onlyContiguous, false, 232 new Normalizer2Impl.ReorderingBuffer(impl, new StringBuilder(), 5)); 233 } 234 @Override quickCheck(CharSequence s)235 public Normalizer.QuickCheckResult quickCheck(CharSequence s) { 236 int spanLengthAndMaybe=impl.composeQuickCheck(s, 0, s.length(), onlyContiguous, false); 237 if((spanLengthAndMaybe&1)!=0) { 238 return Normalizer.MAYBE; 239 } else if((spanLengthAndMaybe>>>1)==s.length()) { 240 return Normalizer.YES; 241 } else { 242 return Normalizer.NO; 243 } 244 } 245 @Override spanQuickCheckYes(CharSequence s)246 public int spanQuickCheckYes(CharSequence s) { 247 return impl.composeQuickCheck(s, 0, s.length(), onlyContiguous, true)>>>1; 248 } 249 @Override getQuickCheck(int c)250 public int getQuickCheck(int c) { 251 return impl.getCompQuickCheck(impl.getNorm16(c)); 252 } 253 @Override hasBoundaryBefore(int c)254 public boolean hasBoundaryBefore(int c) { return impl.hasCompBoundaryBefore(c); } 255 @Override hasBoundaryAfter(int c)256 public boolean hasBoundaryAfter(int c) { 257 return impl.hasCompBoundaryAfter(c, onlyContiguous); 258 } 259 @Override isInert(int c)260 public boolean isInert(int c) { 261 return impl.isCompInert(c, onlyContiguous); 262 } 263 264 private final boolean onlyContiguous; 265 } 266 267 /** 268 * @hide exposed on OHOS 269 */ 270 public static final class FCDNormalizer2 extends Normalizer2WithImpl { FCDNormalizer2(Normalizer2Impl ni)271 public FCDNormalizer2(Normalizer2Impl ni) { 272 super(ni); 273 } 274 275 @Override normalize(CharSequence src, Normalizer2Impl.ReorderingBuffer buffer)276 protected void normalize(CharSequence src, Normalizer2Impl.ReorderingBuffer buffer) { 277 impl.makeFCD(src, 0, src.length(), buffer); 278 } 279 @Override normalizeAndAppend( CharSequence src, boolean doNormalize, Normalizer2Impl.ReorderingBuffer buffer)280 protected void normalizeAndAppend( 281 CharSequence src, boolean doNormalize, Normalizer2Impl.ReorderingBuffer buffer) { 282 impl.makeFCDAndAppend(src, doNormalize, buffer); 283 } 284 @Override spanQuickCheckYes(CharSequence s)285 public int spanQuickCheckYes(CharSequence s) { 286 return impl.makeFCD(s, 0, s.length(), null); 287 } 288 @Override getQuickCheck(int c)289 public int getQuickCheck(int c) { 290 return impl.isDecompYes(impl.getNorm16(c)) ? 1 : 0; 291 } 292 @Override hasBoundaryBefore(int c)293 public boolean hasBoundaryBefore(int c) { return impl.hasFCDBoundaryBefore(c); } 294 @Override hasBoundaryAfter(int c)295 public boolean hasBoundaryAfter(int c) { return impl.hasFCDBoundaryAfter(c); } 296 @Override isInert(int c)297 public boolean isInert(int c) { return impl.isFCDInert(c); } 298 } 299 300 // instance cache ---------------------------------------------------------- *** 301 Norm2AllModes(Normalizer2Impl ni)302 private Norm2AllModes(Normalizer2Impl ni) { 303 impl=ni; 304 comp=new ComposeNormalizer2(ni, false); 305 decomp=new DecomposeNormalizer2(ni); 306 fcd=new FCDNormalizer2(ni); 307 fcc=new ComposeNormalizer2(ni, true); 308 } 309 310 public final Normalizer2Impl impl; 311 public final ComposeNormalizer2 comp; 312 public final DecomposeNormalizer2 decomp; 313 public final FCDNormalizer2 fcd; 314 public final ComposeNormalizer2 fcc; 315 getInstanceFromSingleton(Norm2AllModesSingleton singleton)316 private static Norm2AllModes getInstanceFromSingleton(Norm2AllModesSingleton singleton) { 317 if(singleton.exception!=null) { 318 throw singleton.exception; 319 } 320 return singleton.allModes; 321 } getNFCInstance()322 public static Norm2AllModes getNFCInstance() { 323 return getInstanceFromSingleton(NFCSingleton.INSTANCE); 324 } getNFKCInstance()325 public static Norm2AllModes getNFKCInstance() { 326 return getInstanceFromSingleton(NFKCSingleton.INSTANCE); 327 } getNFKC_CFInstance()328 public static Norm2AllModes getNFKC_CFInstance() { 329 return getInstanceFromSingleton(NFKC_CFSingleton.INSTANCE); 330 } 331 // For use in properties APIs. getN2WithImpl(int index)332 public static Normalizer2WithImpl getN2WithImpl(int index) { 333 switch(index) { 334 case 0: return getNFCInstance().decomp; // NFD 335 case 1: return getNFKCInstance().decomp; // NFKD 336 case 2: return getNFCInstance().comp; // NFC 337 case 3: return getNFKCInstance().comp; // NFKC 338 default: return null; 339 } 340 } getInstance(ByteBuffer bytes, String name)341 public static Norm2AllModes getInstance(ByteBuffer bytes, String name) { 342 if(bytes==null) { 343 Norm2AllModesSingleton singleton; 344 if(name.equals("nfc")) { 345 singleton=NFCSingleton.INSTANCE; 346 } else if(name.equals("nfkc")) { 347 singleton=NFKCSingleton.INSTANCE; 348 } else if(name.equals("nfkc_cf")) { 349 singleton=NFKC_CFSingleton.INSTANCE; 350 } else { 351 singleton=null; 352 } 353 if(singleton!=null) { 354 if(singleton.exception!=null) { 355 throw singleton.exception; 356 } 357 return singleton.allModes; 358 } 359 } 360 return cache.getInstance(name, bytes); 361 } 362 private static CacheBase<String, Norm2AllModes, ByteBuffer> cache = 363 new SoftCache<String, Norm2AllModes, ByteBuffer>() { 364 @Override 365 protected Norm2AllModes createInstance(String key, ByteBuffer bytes) { 366 Normalizer2Impl impl; 367 if(bytes==null) { 368 impl=new Normalizer2Impl().load(key+".nrm"); 369 } else { 370 impl=new Normalizer2Impl().load(bytes); 371 } 372 return new Norm2AllModes(impl); 373 } 374 }; 375 376 public static final NoopNormalizer2 NOOP_NORMALIZER2=new NoopNormalizer2(); 377 /** 378 * Gets the FCD normalizer, with the FCD data initialized. 379 * @return FCD normalizer 380 */ getFCDNormalizer2()381 public static Normalizer2 getFCDNormalizer2() { 382 return getNFCInstance().fcd; 383 } 384 385 private static final class Norm2AllModesSingleton { Norm2AllModesSingleton(String name)386 private Norm2AllModesSingleton(String name) { 387 try { 388 Normalizer2Impl impl=new Normalizer2Impl().load(name+".nrm"); 389 allModes=new Norm2AllModes(impl); 390 } catch(RuntimeException e) { 391 exception=e; 392 } 393 } 394 395 private Norm2AllModes allModes; 396 private RuntimeException exception; 397 } 398 private static final class NFCSingleton { 399 private static final Norm2AllModesSingleton INSTANCE=new Norm2AllModesSingleton("nfc"); 400 } 401 private static final class NFKCSingleton { 402 private static final Norm2AllModesSingleton INSTANCE=new Norm2AllModesSingleton("nfkc"); 403 } 404 private static final class NFKC_CFSingleton { 405 private static final Norm2AllModesSingleton INSTANCE=new Norm2AllModesSingleton("nfkc_cf"); 406 } 407 } 408