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) 2001-2011, International Business Machines Corporation and * 7 * others. All Rights Reserved. * 8 ******************************************************************************* 9 */ 10 package android.icu.text; 11 import android.icu.impl.Utility; 12 13 class Quantifier implements UnicodeMatcher { 14 15 private UnicodeMatcher matcher; 16 17 private int minCount; 18 19 private int maxCount; 20 21 /** 22 * Maximum count a quantifier can have. 23 */ 24 public static final int MAX = Integer.MAX_VALUE; 25 26 public Quantifier(UnicodeMatcher theMatcher, 27 int theMinCount, int theMaxCount) { 28 if (theMatcher == null || theMinCount < 0 || theMaxCount < 0 || theMinCount > theMaxCount) { 29 throw new IllegalArgumentException(); 30 } 31 matcher = theMatcher; 32 minCount = theMinCount; 33 maxCount = theMaxCount; 34 } 35 36 /** 37 * Implement UnicodeMatcher API. 38 */ 39 public int matches(Replaceable text, 40 int[] offset, 41 int limit, 42 boolean incremental) { 43 int start = offset[0]; 44 int count = 0; 45 while (count < maxCount) { 46 int pos = offset[0]; 47 int m = matcher.matches(text, offset, limit, incremental); 48 if (m == U_MATCH) { 49 ++count; 50 if (pos == offset[0]) { 51 // If offset has not moved we have a zero-width match. 52 // Don't keep matching it infinitely. 53 break; 54 } 55 } else if (incremental && m == U_PARTIAL_MATCH) { 56 return U_PARTIAL_MATCH; 57 } else { 58 break; 59 } 60 } 61 if (incremental && offset[0] == limit) { 62 return U_PARTIAL_MATCH; 63 } 64 if (count >= minCount) { 65 return U_MATCH; 66 } 67 offset[0] = start; 68 return U_MISMATCH; 69 } 70 71 /** 72 * Implement UnicodeMatcher API 73 */ 74 public String toPattern(boolean escapeUnprintable) { 75 StringBuilder result = new StringBuilder(); 76 result.append(matcher.toPattern(escapeUnprintable)); 77 if (minCount == 0) { 78 if (maxCount == 1) { 79 return result.append('?').toString(); 80 } else if (maxCount == MAX) { 81 return result.append('*').toString(); 82 } 83 // else fall through 84 } else if (minCount == 1 && maxCount == MAX) { 85 return result.append('+').toString(); 86 } 87 result.append('{'); 88 result.append(Utility.hex(minCount,1)); 89 result.append(','); 90 if (maxCount != MAX) { 91 result.append(Utility.hex(maxCount,1)); 92 } 93 result.append('}'); 94 return result.toString(); 95 } 96 97 /** 98 * Implement UnicodeMatcher API 99 */ 100 public boolean matchesIndexValue(int v) { 101 return (minCount == 0) || matcher.matchesIndexValue(v); 102 } 103 104 /** 105 * Implementation of UnicodeMatcher API. Union the set of all 106 * characters that may be matched by this object into the given 107 * set. 108 * @param toUnionTo the set into which to union the source characters 109 * @returns a reference to toUnionTo 110 */ 111 public void addMatchSetTo(UnicodeSet toUnionTo) { 112 if (maxCount > 0) { 113 matcher.addMatchSetTo(toUnionTo); 114 } 115 } 116 } 117 118 //eof 119