1 7 package com.ibm.icu.text; 8 import com.ibm.icu.impl.Utility; 9 10 27 class StringMatcher implements UnicodeMatcher, UnicodeReplacer { 28 29 32 private String pattern; 33 34 38 private int matchStart; 39 40 44 private int matchLimit; 45 46 49 private int segmentNumber; 50 51 55 private final RuleBasedTransliterator.Data data; 56 57 66 public StringMatcher(String theString, 67 int segmentNum, 68 RuleBasedTransliterator.Data theData) { 69 data = theData; 70 pattern = theString; 71 matchStart = matchLimit = -1; 72 segmentNumber = segmentNum; 73 } 74 75 88 public StringMatcher(String theString, 89 int start, 90 int limit, 91 int segmentNum, 92 RuleBasedTransliterator.Data theData) { 93 this(theString.substring(start, limit), segmentNum, theData); 94 } 95 96 99 public int matches(Replaceable text, 100 int[] offset, 101 int limit, 102 boolean incremental) { 103 int i; 108 int[] cursor = new int[] { offset[0] }; 109 if (limit < cursor[0]) { 110 for (i=pattern.length()-1; i>=0; --i) { 112 char keyChar = pattern.charAt(i); UnicodeMatcher subm = data.lookupMatcher(keyChar); 114 if (subm == null) { 115 if (cursor[0] > limit && 116 keyChar == text.charAt(cursor[0])) { --cursor[0]; 118 } else { 119 return U_MISMATCH; 120 } 121 } else { 122 int m = 123 subm.matches(text, cursor, limit, incremental); 124 if (m != U_MATCH) { 125 return m; 126 } 127 } 128 } 129 if (matchStart < 0) { 133 matchStart = cursor[0]+1; 134 matchLimit = offset[0]+1; 135 } 136 } else { 137 for (i=0; i<pattern.length(); ++i) { 138 if (incremental && cursor[0] == limit) { 139 return U_PARTIAL_MATCH; 142 } 143 char keyChar = pattern.charAt(i); UnicodeMatcher subm = data.lookupMatcher(keyChar); 145 if (subm == null) { 146 if (cursor[0] < limit && 150 keyChar == text.charAt(cursor[0])) { ++cursor[0]; 152 } else { 153 return U_MISMATCH; 154 } 155 } else { 156 int m = 157 subm.matches(text, cursor, limit, incremental); 158 if (m != U_MATCH) { 159 return m; 160 } 161 } 162 } 163 matchStart = offset[0]; 165 matchLimit = cursor[0]; 166 } 167 168 offset[0] = cursor[0]; 169 return U_MATCH; 170 } 171 172 175 public String toPattern(boolean escapeUnprintable) { 176 StringBuffer result = new StringBuffer (); 177 StringBuffer quoteBuf = new StringBuffer (); 178 if (segmentNumber > 0) { result.append('('); 180 } 181 for (int i=0; i<pattern.length(); ++i) { 182 char keyChar = pattern.charAt(i); UnicodeMatcher m = data.lookupMatcher(keyChar); 184 if (m == null) { 185 Utility.appendToRule(result, keyChar, false, escapeUnprintable, quoteBuf); 186 } else { 187 Utility.appendToRule(result, m.toPattern(escapeUnprintable), 188 true, escapeUnprintable, quoteBuf); 189 } 190 } 191 if (segmentNumber > 0) { result.append(')'); 193 } 194 Utility.appendToRule(result, -1, 196 true, escapeUnprintable, quoteBuf); 197 return result.toString(); 198 } 199 200 203 public boolean matchesIndexValue(int v) { 204 if (pattern.length() == 0) { 205 return true; 206 } 207 int c = UTF16.charAt(pattern, 0); 208 UnicodeMatcher m = data.lookupMatcher(c); 209 return (m == null) ? ((c & 0xFF) == v) : m.matchesIndexValue(v); 210 } 211 212 218 public void addMatchSetTo(UnicodeSet toUnionTo) { 219 int ch; 220 for (int i=0; i<pattern.length(); i+=UTF16.getCharCount(ch)) { 221 ch = UTF16.charAt(pattern, i); 222 UnicodeMatcher matcher = data.lookupMatcher(ch); 223 if (matcher == null) { 224 toUnionTo.add(ch); 225 } else { 226 matcher.addMatchSetTo(toUnionTo); 227 } 228 } 229 } 230 231 234 public int replace(Replaceable text, 235 int start, 236 int limit, 237 int[] cursor) { 238 239 int outLen = 0; 240 241 int dest = limit; 243 if (matchStart >= 0) { 246 if (matchStart != matchLimit) { 247 text.copy(matchStart, matchLimit, dest); 248 outLen = matchLimit - matchStart; 249 } 250 } 251 252 text.replace(start, limit, ""); 254 return outLen; 255 } 256 257 260 public String toReplacerPattern(boolean escapeUnprintable) { 261 StringBuffer rule = new StringBuffer ("$"); 263 Utility.appendNumber(rule, segmentNumber, 10, 1); 264 return rule.toString(); 265 } 266 267 271 public void resetMatch() { 272 matchStart = matchLimit = -1; 273 } 274 275 280 public void addReplacementSetTo(UnicodeSet toUnionTo) { 281 } 287 } 288 289 | Popular Tags |