2 *******************************************************************************
4 * Copyright (C) 2004-2010, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: ReplaceableContextIterator.java
10 * tab size: 8 (not used)
13 * created on: 2005feb04
14 * created by: Markus W. Scherer
16 * Implementation of UCaseProps.ContextIterator, iterates over a Replaceable.
17 * Java port of casetrn.cpp/utrans_rep_caseContextIterator().
20 package com.ibm.icu.text;
22 import com.ibm.icu.impl.UCaseProps;
25 * Implementation of UCaseProps.ContextIterator, iterates over a Replaceable.
26 * See casetrn.cpp/utrans_rep_caseContextIterator().
27 * See also UCharacter.StringContextIterator.
29 class ReplaceableContextIterator implements UCaseProps.ContextIterator {
32 * @param rep Replaceable to iterate over.
34 ReplaceableContextIterator() {
36 limit=cpStart=cpLimit=index=contextStart=contextLimit=0;
42 * Set the text for iteration.
43 * @param rep Iteration text.
45 public void setText(Replaceable rep) {
47 limit=contextLimit=rep.length();
48 cpStart=cpLimit=index=contextStart=0;
54 * Set the index where nextCaseMapCP() is to start iterating.
55 * @param index Iteration start index for nextCaseMapCP().
57 public void setIndex(int index) {
58 cpStart=cpLimit=index;
65 * Get the index of where the code point currently being case-mapped starts.
66 * @return The start index of the current code point.
68 public int getCaseMapCPStart() {
73 * Set the iteration limit for nextCaseMapCP() to an index within the string.
74 * If the limit parameter is negative or past the string, then the
75 * string length is restored as the iteration limit.
77 * @param lim The iteration limit.
79 public void setLimit(int lim) {
80 if(0<=lim && lim<=rep.length()) {
89 * Set the start and limit indexes for context iteration with next().
90 * @param contextStart Start of context for next().
91 * @param contextLimit Limit of context for next().
93 public void setContextLimits(int contextStart, int contextLimit) {
96 } else if(contextStart<=rep.length()) {
97 this.contextStart=contextStart;
99 this.contextStart=rep.length();
101 if(contextLimit<this.contextStart) {
102 this.contextLimit=this.contextStart;
103 } else if(contextLimit<=rep.length()) {
104 this.contextLimit=contextLimit;
106 this.contextLimit=rep.length();
112 * Iterate forward through the string to fetch the next code point
113 * to be case-mapped, and set the context indexes for it.
115 * @return The next code point to be case-mapped, or <0 when the iteration is done.
117 public int nextCaseMapCP() {
121 c=rep.char32At(cpLimit);
122 cpLimit+=UTF16.getCharCount(c);
130 * Replace the current code point by its case mapping,
131 * and update the indexes.
133 * @param text Replacement text.
134 * @return The delta for the change of the text length.
136 public int replace(String text) {
137 int delta=text.length()-(cpLimit-cpStart);
138 rep.replace(cpStart, cpLimit, text);
146 * Did forward context iteration with next() reach the iteration limit?
147 * @return Boolean value.
149 public boolean didReachLimit() {
153 // implement UCaseProps.ContextIterator
154 public void reset(int direction) {
156 /* reset for forward iteration */
159 } else if(direction<0) {
160 /* reset for backward iteration */
164 // not a valid direction
175 if(index<contextLimit) {
176 c=rep.char32At(index);
177 index+=UTF16.getCharCount(c);
180 // forward context iteration reached the limit
183 } else if(dir<0 && index>contextStart) {
184 c=rep.char32At(index-1);
185 index-=UTF16.getCharCount(c);
192 protected Replaceable rep;
193 protected int index, limit, cpStart, cpLimit, contextStart, contextLimit;
194 protected int dir; // 0=initial state >0=forward <0=backward
195 protected boolean reachedLimit;