2 *******************************************************************************
\r
4 * Copyright (C) 2004-2008, International Business Machines
\r
5 * Corporation and others. All Rights Reserved.
\r
7 *******************************************************************************
\r
8 * file name: ReplaceableContextIterator.java
\r
10 * tab size: 8 (not used)
\r
13 * created on: 2005feb04
\r
14 * created by: Markus W. Scherer
\r
16 * Implementation of UCaseProps.ContextIterator, iterates over a Replaceable.
\r
17 * Java port of casetrn.cpp/utrans_rep_caseContextIterator().
\r
20 package com.ibm.icu.text;
\r
22 import com.ibm.icu.impl.UCaseProps;
\r
25 * Implementation of UCaseProps.ContextIterator, iterates over a Replaceable.
\r
26 * See casetrn.cpp/utrans_rep_caseContextIterator().
\r
27 * See also UCharacter.StringContextIterator.
\r
30 class ReplaceableContextIterator implements UCaseProps.ContextIterator {
\r
33 * @param rep Replaceable to iterate over.
\r
35 ReplaceableContextIterator() {
\r
37 limit=cpStart=cpLimit=index=contextStart=contextLimit=0;
\r
43 * Set the text for iteration.
\r
44 * @param rep Iteration text.
\r
46 public void setText(Replaceable rep) {
\r
48 limit=contextLimit=rep.length();
\r
49 cpStart=cpLimit=index=contextStart=0;
\r
55 * Set the index where nextCaseMapCP() is to start iterating.
\r
56 * @param index Iteration start index for nextCaseMapCP().
\r
58 public void setIndex(int index) {
\r
59 cpStart=cpLimit=index;
\r
66 * Get the index of where the code point currently being case-mapped starts.
\r
67 * @return The start index of the current code point.
\r
69 public int getCaseMapCPStart() {
\r
74 * Set the iteration limit for nextCaseMapCP() to an index within the string.
\r
75 * If the limit parameter is negative or past the string, then the
\r
76 * string length is restored as the iteration limit.
\r
78 * @param lim The iteration limit.
\r
80 public void setLimit(int lim) {
\r
81 if(0<=lim && lim<=rep.length()) {
\r
90 * Set the start and limit indexes for context iteration with next().
\r
91 * @param contextStart Start of context for next().
\r
92 * @param contextLimit Limit of context for next().
\r
94 public void setContextLimits(int contextStart, int contextLimit) {
\r
95 if(contextStart<0) {
\r
96 this.contextStart=0;
\r
97 } else if(contextStart<=rep.length()) {
\r
98 this.contextStart=contextStart;
\r
100 this.contextStart=rep.length();
\r
102 if(contextLimit<this.contextStart) {
\r
103 this.contextLimit=this.contextStart;
\r
104 } else if(contextLimit<=rep.length()) {
\r
105 this.contextLimit=contextLimit;
\r
107 this.contextLimit=rep.length();
\r
109 reachedLimit=false;
\r
113 * Iterate forward through the string to fetch the next code point
\r
114 * to be case-mapped, and set the context indexes for it.
\r
116 * @return The next code point to be case-mapped, or <0 when the iteration is done.
\r
118 public int nextCaseMapCP() {
\r
120 if(cpLimit<limit) {
\r
122 c=rep.char32At(cpLimit);
\r
123 cpLimit+=UTF16.getCharCount(c);
\r
131 * Replace the current code point by its case mapping,
\r
132 * and update the indexes.
\r
134 * @param text Replacement text.
\r
135 * @return The delta for the change of the text length.
\r
137 public int replace(String text) {
\r
138 int delta=text.length()-(cpLimit-cpStart);
\r
139 rep.replace(cpStart, cpLimit, text);
\r
142 contextLimit+=delta;
\r
147 * Did forward context iteration with next() reach the iteration limit?
\r
148 * @return Boolean value.
\r
150 public boolean didReachLimit() {
\r
151 return reachedLimit;
\r
154 // implement UCaseProps.ContextIterator
\r
155 public void reset(int direction) {
\r
157 /* reset for forward iteration */
\r
160 } else if(direction<0) {
\r
161 /* reset for backward iteration */
\r
165 // not a valid direction
\r
169 reachedLimit=false;
\r
172 public int next() {
\r
176 if(index<contextLimit) {
\r
177 c=rep.char32At(index);
\r
178 index+=UTF16.getCharCount(c);
\r
181 // forward context iteration reached the limit
\r
184 } else if(dir<0 && index>contextStart) {
\r
185 c=rep.char32At(index-1);
\r
186 index-=UTF16.getCharCount(c);
\r
193 protected Replaceable rep;
\r
194 protected int index, limit, cpStart, cpLimit, contextStart, contextLimit;
\r
195 protected int dir; // 0=initial state >0=forward <0=backward
\r
196 protected boolean reachedLimit;
\r