1 10 11 package com.ibm.icu.text; 12 import com.ibm.icu.impl.Utility; 13 14 23 class StringReplacer implements UnicodeReplacer { 24 25 29 private String output; 30 31 34 private int cursorPos; 35 36 39 private boolean hasCursor; 40 41 48 private boolean isComplex; 49 50 54 private final RuleBasedTransliterator.Data data; 55 56 67 public StringReplacer(String theOutput, 68 int theCursorPos, 69 RuleBasedTransliterator.Data theData) { 70 output = theOutput; 71 cursorPos = theCursorPos; 72 hasCursor = true; 73 data = theData; 74 isComplex = true; 75 } 76 77 86 public StringReplacer(String theOutput, 87 RuleBasedTransliterator.Data theData) { 88 output = theOutput; 89 cursorPos = 0; 90 hasCursor = false; 91 data = theData; 92 isComplex = true; 93 } 94 95 108 111 public int replace(Replaceable text, 112 int start, 113 int limit, 114 int[] cursor) { 115 int outLen; 116 int newStart = 0; 117 118 122 if (!isComplex) { 124 text.replace(start, limit, output); 125 outLen = output.length(); 126 127 newStart = cursorPos; 129 } 130 131 else { 133 139 StringBuffer buf = new StringBuffer (); 140 int oOutput; isComplex = false; 142 143 int tempStart = text.length(); int destStart = tempStart; if (start > 0) { 155 int len = UTF16.getCharCount(text.char32At(start-1)); 156 text.copy(start-len, start, tempStart); 157 destStart += len; 158 } else { 159 text.replace(tempStart, tempStart, "\uFFFF"); 160 destStart++; 161 } 162 int destLimit = destStart; 163 int tempExtra = 0; 165 for (oOutput=0; oOutput<output.length(); ) { 166 if (oOutput == cursorPos) { 167 newStart = destLimit - destStart; } 170 int c = UTF16.charAt(output, oOutput); 171 172 int nextIndex = oOutput + UTF16.getCharCount(c); 177 if (nextIndex == output.length()) { 178 tempExtra = UTF16.getCharCount(text.char32At(limit)); 179 text.copy(limit, limit+tempExtra, destLimit); 180 } 181 182 UnicodeReplacer r = data.lookupReplacer(c); 183 if (r == null) { 184 UTF16.append(buf, c); 186 } else { 187 isComplex = true; 188 189 if (buf.length() > 0) { 191 text.replace(destLimit, destLimit, buf.toString()); 192 destLimit += buf.length(); 193 buf.setLength(0); 194 } 195 196 int len = r.replace(text, destLimit, destLimit, cursor); 198 destLimit += len; 199 } 200 oOutput = nextIndex; 201 } 202 if (buf.length() > 0) { 204 text.replace(destLimit, destLimit, buf.toString()); 205 destLimit += buf.length(); 206 } 207 if (oOutput == cursorPos) { 208 newStart = destLimit - destStart; } 211 212 outLen = destLimit - destStart; 213 214 text.copy(destStart, destLimit, start); 216 text.replace(tempStart + outLen, destLimit + tempExtra + outLen, ""); 217 218 text.replace(start + outLen, limit + outLen, ""); 220 } 221 222 if (hasCursor) { 223 if (cursorPos < 0) { 228 newStart = start; 229 int n = cursorPos; 230 while (n < 0 && newStart > 0) { 232 newStart -= UTF16.getCharCount(text.char32At(newStart-1)); 233 ++n; 234 } 235 newStart += n; 236 } else if (cursorPos > output.length()) { 237 newStart = start + outLen; 238 int n = cursorPos - output.length(); 239 while (n > 0 && newStart < text.length()) { 241 newStart += UTF16.getCharCount(text.char32At(newStart)); 242 --n; 243 } 244 newStart += n; 245 } else { 246 newStart += start; 249 } 250 251 cursor[0] = newStart; 252 } 253 254 return outLen; 255 } 256 257 260 public String toReplacerPattern(boolean escapeUnprintable) { 261 StringBuffer rule = new StringBuffer (); 262 StringBuffer quoteBuf = new StringBuffer (); 263 264 int cursor = cursorPos; 265 266 if (hasCursor && cursor < 0) { 268 while (cursor++ < 0) { 269 Utility.appendToRule(rule, '@', true, escapeUnprintable, quoteBuf); 270 } 271 } 273 274 for (int i=0; i<output.length(); ++i) { 275 if (hasCursor && i == cursor) { 276 Utility.appendToRule(rule, '|', true, escapeUnprintable, quoteBuf); 277 } 278 char c = output.charAt(i); 280 UnicodeReplacer r = data.lookupReplacer(c); 281 if (r == null) { 282 Utility.appendToRule(rule, c, false, escapeUnprintable, quoteBuf); 283 } else { 284 StringBuffer buf = new StringBuffer (" "); 285 buf.append(r.toReplacerPattern(escapeUnprintable)); 286 buf.append(' '); 287 Utility.appendToRule(rule, buf.toString(), 288 true, escapeUnprintable, quoteBuf); 289 } 290 } 291 292 if (hasCursor && cursor > output.length()) { 296 cursor -= output.length(); 297 while (cursor-- > 0) { 298 Utility.appendToRule(rule, '@', true, escapeUnprintable, quoteBuf); 299 } 300 Utility.appendToRule(rule, '|', true, escapeUnprintable, quoteBuf); 301 } 302 Utility.appendToRule(rule, -1, 304 true, escapeUnprintable, quoteBuf); 305 306 return rule.toString(); 307 } 308 309 314 public void addReplacementSetTo(UnicodeSet toUnionTo) { 315 int ch; 316 for (int i=0; i<output.length(); i+=UTF16.getCharCount(ch)) { 317 ch = UTF16.charAt(output, i); 318 UnicodeReplacer r = data.lookupReplacer(ch); 319 if (r == null) { 320 toUnionTo.add(ch); 321 } else { 322 r.addReplacementSetTo(toUnionTo); 323 } 324 } 325 } 326 } 327 328 | Popular Tags |