1 19 20 package com.ibm.icu.text; 21 22 import com.ibm.icu.impl.UCaseProps; 23 import com.ibm.icu.impl.UCharacterProperty; 24 25 import com.ibm.icu.util.ULocale; 26 27 33 class ReplaceableContextIterator implements UCaseProps.ContextIterator { 34 38 ReplaceableContextIterator() { 39 this.rep=null; 40 limit=cpStart=cpLimit=index=contextStart=contextLimit=0; 41 dir=0; 42 reachedLimit=false; 43 } 44 45 49 public void setText(Replaceable rep) { 50 this.rep=rep; 51 limit=contextLimit=rep.length(); 52 cpStart=cpLimit=index=contextStart=0; 53 dir=0; 54 reachedLimit=false; 55 } 56 57 61 public void setIndex(int index) { 62 cpStart=cpLimit=index; 63 this.index=0; 64 dir=0; 65 reachedLimit=false; 66 } 67 68 72 public int getCaseMapCPStart() { 73 return cpStart; 74 } 75 76 83 public void setLimit(int lim) { 84 if(0<=lim && lim<=rep.length()) { 85 limit=lim; 86 } else { 87 limit=rep.length(); 88 } 89 reachedLimit=false; 90 } 91 92 97 public void setContextLimits(int contextStart, int contextLimit) { 98 if(contextStart<0) { 99 this.contextStart=0; 100 } else if(contextStart<=rep.length()) { 101 this.contextStart=contextStart; 102 } else { 103 this.contextStart=rep.length(); 104 } 105 if(contextLimit<this.contextStart) { 106 this.contextLimit=this.contextStart; 107 } else if(contextLimit<=rep.length()) { 108 this.contextLimit=contextLimit; 109 } else { 110 this.contextLimit=rep.length(); 111 } 112 reachedLimit=false; 113 } 114 115 121 public int nextCaseMapCP() { 122 int c; 123 if(cpLimit<limit) { 124 cpStart=cpLimit; 125 c=rep.char32At(cpLimit); 126 cpLimit+=UTF16.getCharCount(c); 127 return c; 128 } else { 129 return -1; 130 } 131 } 132 133 140 public int replace(String text) { 141 int delta=text.length()-(cpLimit-cpStart); 142 rep.replace(cpStart, cpLimit, text); 143 cpLimit+=delta; 144 limit+=delta; 145 contextLimit+=delta; 146 return delta; 147 } 148 149 153 public boolean didReachLimit() { 154 return reachedLimit; 155 } 156 157 public void reset(int dir) { 159 if(dir>0) { 160 161 this.dir=1; 162 index=cpLimit; 163 } else if(dir<0) { 164 165 this.dir=-1; 166 index=cpStart; 167 } else { 168 this.dir=0; 170 index=0; 171 } 172 reachedLimit=false; 173 } 174 175 public int next() { 176 int c; 177 178 if(dir>0) { 179 if(index<contextLimit) { 180 c=rep.char32At(index); 181 index+=UTF16.getCharCount(c); 182 return c; 183 } else { 184 reachedLimit=true; 186 } 187 } else if(dir<0 && index>contextStart) { 188 c=rep.char32At(index-1); 189 index-=UTF16.getCharCount(c); 190 return c; 191 } 192 return -1; 193 } 194 195 protected Replaceable rep; 197 protected int index, limit, cpStart, cpLimit, contextStart, contextLimit; 198 protected int dir; protected boolean reachedLimit; 200 } 201 | Popular Tags |