]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/core/src/com/ibm/icu/text/ReplaceableContextIterator.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / core / src / com / ibm / icu / text / ReplaceableContextIterator.java
1 /*\r
2 *******************************************************************************\r
3 *\r
4 *   Copyright (C) 2004-2010, International Business Machines\r
5 *   Corporation and others.  All Rights Reserved.\r
6 *\r
7 *******************************************************************************\r
8 *   file name:  ReplaceableContextIterator.java\r
9 *   encoding:   US-ASCII\r
10 *   tab size:   8 (not used)\r
11 *   indentation:4\r
12 *\r
13 *   created on: 2005feb04\r
14 *   created by: Markus W. Scherer\r
15 *\r
16 *   Implementation of UCaseProps.ContextIterator, iterates over a Replaceable.\r
17 *   Java port of casetrn.cpp/utrans_rep_caseContextIterator().\r
18 */\r
19 \r
20 package com.ibm.icu.text;\r
21 \r
22 import com.ibm.icu.impl.UCaseProps;\r
23 \r
24 /**\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
28  */\r
29 class ReplaceableContextIterator implements UCaseProps.ContextIterator {\r
30     /**\r
31      * Constructor.\r
32      * @param rep Replaceable to iterate over. \r
33      */\r
34     ReplaceableContextIterator() {\r
35         this.rep=null;\r
36         limit=cpStart=cpLimit=index=contextStart=contextLimit=0;\r
37         dir=0;\r
38         reachedLimit=false;\r
39     }\r
40 \r
41     /**\r
42      * Set the text for iteration.\r
43      * @param rep Iteration text.\r
44      */\r
45     public void setText(Replaceable rep) {\r
46         this.rep=rep;\r
47         limit=contextLimit=rep.length();\r
48         cpStart=cpLimit=index=contextStart=0;\r
49         dir=0;\r
50         reachedLimit=false;\r
51     }\r
52 \r
53     /**\r
54      * Set the index where nextCaseMapCP() is to start iterating.\r
55      * @param index Iteration start index for nextCaseMapCP().\r
56      */\r
57     public void setIndex(int index) {\r
58         cpStart=cpLimit=index;\r
59         this.index=0;\r
60         dir=0;\r
61         reachedLimit=false;\r
62     }\r
63 \r
64     /**\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
67      */\r
68     public int getCaseMapCPStart() {\r
69         return cpStart;\r
70     }\r
71 \r
72     /**\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
76      *\r
77      * @param lim The iteration limit.\r
78      */\r
79     public void setLimit(int lim) {\r
80         if(0<=lim && lim<=rep.length()) {\r
81             limit=lim;\r
82         } else {\r
83             limit=rep.length();\r
84         }\r
85         reachedLimit=false;\r
86     }\r
87 \r
88     /**\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
92      */\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
98         } else {\r
99             this.contextStart=rep.length();\r
100         }\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
105         } else {\r
106             this.contextLimit=rep.length();\r
107         }\r
108         reachedLimit=false;\r
109     }\r
110 \r
111     /**\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
114      *\r
115      * @return The next code point to be case-mapped, or <0 when the iteration is done.\r
116      */\r
117     public int nextCaseMapCP() {\r
118         int c;\r
119         if(cpLimit<limit) {\r
120             cpStart=cpLimit;\r
121             c=rep.char32At(cpLimit);\r
122             cpLimit+=UTF16.getCharCount(c);\r
123             return c;\r
124         } else {\r
125             return -1;\r
126         }\r
127     }\r
128 \r
129     /**\r
130      * Replace the current code point by its case mapping,\r
131      * and update the indexes.\r
132      *\r
133      * @param text Replacement text.\r
134      * @return The delta for the change of the text length.\r
135      */\r
136     public int replace(String text) {\r
137         int delta=text.length()-(cpLimit-cpStart);\r
138         rep.replace(cpStart, cpLimit, text);\r
139         cpLimit+=delta;\r
140         limit+=delta;\r
141         contextLimit+=delta;\r
142         return delta;\r
143     }\r
144 \r
145     /**\r
146      * Did forward context iteration with next() reach the iteration limit?\r
147      * @return Boolean value.\r
148      */\r
149     public boolean didReachLimit() {\r
150         return reachedLimit;\r
151     }\r
152 \r
153     // implement UCaseProps.ContextIterator\r
154     public void reset(int direction) {\r
155         if(direction>0) {\r
156             /* reset for forward iteration */\r
157             this.dir=1;\r
158             index=cpLimit;\r
159         } else if(direction<0) {\r
160             /* reset for backward iteration */\r
161             this.dir=-1;\r
162             index=cpStart;\r
163         } else {\r
164             // not a valid direction\r
165             this.dir=0;\r
166             index=0;\r
167         }\r
168         reachedLimit=false;\r
169     }\r
170 \r
171     public int next() {\r
172         int c;\r
173 \r
174         if(dir>0) {\r
175             if(index<contextLimit) {\r
176                 c=rep.char32At(index);\r
177                 index+=UTF16.getCharCount(c);\r
178                 return c;\r
179             } else {\r
180                 // forward context iteration reached the limit\r
181                 reachedLimit=true;\r
182             }\r
183         } else if(dir<0 && index>contextStart) {\r
184             c=rep.char32At(index-1);\r
185             index-=UTF16.getCharCount(c);\r
186             return c;\r
187         }\r
188         return -1;\r
189     }\r
190 \r
191     // variables\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
196 }\r