2
*******************************************************************************
3
* Copyright (C) 2001-2010, International Business Machines Corporation and *
4
* others. All Rights Reserved. *
5
*******************************************************************************
7
package com.ibm.icu.text;
8
import com.ibm.icu.impl.Utility;
10
class Quantifier implements UnicodeMatcher {
12
private UnicodeMatcher matcher;
19
* Maximum count a quantifier can have.
21
public static final int MAX = Integer.MAX_VALUE;
23
public Quantifier(UnicodeMatcher theMatcher,
24
int theMinCount, int theMaxCount) {
25
if (theMatcher == null || minCount < 0 || maxCount < 0 || minCount > maxCount) {
26
throw new IllegalArgumentException();
29
minCount = theMinCount;
30
maxCount = theMaxCount;
34
* Implement UnicodeMatcher API.
36
public int matches(Replaceable text,
39
boolean incremental) {
40
int start = offset[0];
42
while (count < maxCount) {
44
int m = matcher.matches(text, offset, limit, incremental);
47
if (pos == offset[0]) {
48
// If offset has not moved we have a zero-width match.
49
// Don't keep matching it infinitely.
52
} else if (incremental && m == U_PARTIAL_MATCH) {
53
return U_PARTIAL_MATCH;
58
if (incremental && offset[0] == limit) {
59
return U_PARTIAL_MATCH;
61
if (count >= minCount) {
69
* Implement UnicodeMatcher API
71
public String toPattern(boolean escapeUnprintable) {
72
StringBuilder result = new StringBuilder();
73
result.append(matcher.toPattern(escapeUnprintable));
76
return result.append('?').toString();
77
} else if (maxCount == MAX) {
78
return result.append('*').toString();
81
} else if (minCount == 1 && maxCount == MAX) {
82
return result.append('+').toString();
85
result.append(Utility.hex(minCount,1));
87
if (maxCount != MAX) {
88
result.append(Utility.hex(maxCount,1));
91
return result.toString();
95
* Implement UnicodeMatcher API
97
public boolean matchesIndexValue(int v) {
98
return (minCount == 0) || matcher.matchesIndexValue(v);
102
* Implementation of UnicodeMatcher API. Union the set of all
103
* characters that may be matched by this object into the given
105
* @param toUnionTo the set into which to union the source characters
106
* @returns a reference to toUnionTo
108
public void addMatchSetTo(UnicodeSet toUnionTo) {
110
matcher.addMatchSetTo(toUnionTo);