2 *******************************************************************************
\r
4 * Copyright (C) 2004-2010, 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
29 class ReplaceableContextIterator implements UCaseProps.ContextIterator {
\r
32 * @param rep Replaceable to iterate over.
\r
34 ReplaceableContextIterator() {
\r
36 limit=cpStart=cpLimit=index=contextStart=contextLimit=0;
\r
42 * Set the text for iteration.
\r
43 * @param rep Iteration text.
\r
45 public void setText(Replaceable rep) {
\r
47 limit=contextLimit=rep.length();
\r
48 cpStart=cpLimit=index=contextStart=0;
\r
54 * Set the index where nextCaseMapCP() is to start iterating.
\r
55 * @param index Iteration start index for nextCaseMapCP().
\r
57 public void setIndex(int index) {
\r
58 cpStart=cpLimit=index;
\r
65 * Get the index of where the code point currently being case-mapped starts.
\r
66 * @return The start index of the current code point.
\r
68 public int getCaseMapCPStart() {
\r
73 * Set the iteration limit for nextCaseMapCP() to an index within the string.
\r
74 * If the limit parameter is negative or past the string, then the
\r
75 * string length is restored as the iteration limit.
\r
77 * @param lim The iteration limit.
\r
79 public void setLimit(int lim) {
\r
80 if(0<=lim && lim<=rep.length()) {
\r
89 * Set the start and limit indexes for context iteration with next().
\r
90 * @param contextStart Start of context for next().
\r
91 * @param contextLimit Limit of context for next().
\r
93 public void setContextLimits(int contextStart, int contextLimit) {
\r
94 if(contextStart<0) {
\r
95 this.contextStart=0;
\r
96 } else if(contextStart<=rep.length()) {
\r
97 this.contextStart=contextStart;
\r
99 this.contextStart=rep.length();
\r
101 if(contextLimit<this.contextStart) {
\r
102 this.contextLimit=this.contextStart;
\r
103 } else if(contextLimit<=rep.length()) {
\r
104 this.contextLimit=contextLimit;
\r
106 this.contextLimit=rep.length();
\r
108 reachedLimit=false;
\r
112 * Iterate forward through the string to fetch the next code point
\r
113 * to be case-mapped, and set the context indexes for it.
\r
115 * @return The next code point to be case-mapped, or <0 when the iteration is done.
\r
117 public int nextCaseMapCP() {
\r
119 if(cpLimit<limit) {
\r
121 c=rep.char32At(cpLimit);
\r
122 cpLimit+=UTF16.getCharCount(c);
\r
130 * Replace the current code point by its case mapping,
\r
131 * and update the indexes.
\r
133 * @param text Replacement text.
\r
134 * @return The delta for the change of the text length.
\r
136 public int replace(String text) {
\r
137 int delta=text.length()-(cpLimit-cpStart);
\r
138 rep.replace(cpStart, cpLimit, text);
\r
141 contextLimit+=delta;
\r
146 * Did forward context iteration with next() reach the iteration limit?
\r
147 * @return Boolean value.
\r
149 public boolean didReachLimit() {
\r
150 return reachedLimit;
\r
153 // implement UCaseProps.ContextIterator
\r
154 public void reset(int direction) {
\r
156 /* reset for forward iteration */
\r
159 } else if(direction<0) {
\r
160 /* reset for backward iteration */
\r
164 // not a valid direction
\r
168 reachedLimit=false;
\r
171 public int next() {
\r
175 if(index<contextLimit) {
\r
176 c=rep.char32At(index);
\r
177 index+=UTF16.getCharCount(c);
\r
180 // forward context iteration reached the limit
\r
183 } else if(dir<0 && index>contextStart) {
\r
184 c=rep.char32At(index-1);
\r
185 index-=UTF16.getCharCount(c);
\r
192 protected Replaceable rep;
\r
193 protected int index, limit, cpStart, cpLimit, contextStart, contextLimit;
\r
194 protected int dir; // 0=initial state >0=forward <0=backward
\r
195 protected boolean reachedLimit;
\r