1 57 58 package com.sun.org.apache.xerces.internal.impl.xpath.regex; 59 60 import java.text.CharacterIterator ; 61 62 67 public class BMPattern { 68 char[] pattern; 69 int[] shiftTable; 70 boolean ignoreCase; 71 72 public BMPattern(String pat, boolean ignoreCase) { 73 this(pat, 256, ignoreCase); 74 } 75 76 public BMPattern(String pat, int tableSize, boolean ignoreCase) { 77 this.pattern = pat.toCharArray(); 78 this.shiftTable = new int[tableSize]; 79 this.ignoreCase = ignoreCase; 80 81 int length = pattern.length; 82 for (int i = 0; i < this.shiftTable.length; i ++) 83 this.shiftTable[i] = length; 84 85 for (int i = 0; i < length; i ++) { 86 char ch = this.pattern[i]; 87 int diff = length-i-1; 88 int index = ch % this.shiftTable.length; 89 if (diff < this.shiftTable[index]) 90 this.shiftTable[index] = diff; 91 if (this.ignoreCase) { 92 ch = Character.toUpperCase(ch); 93 index = ch % this.shiftTable.length; 94 if (diff < this.shiftTable[index]) 95 this.shiftTable[index] = diff; 96 ch = Character.toLowerCase(ch); 97 index = ch % this.shiftTable.length; 98 if (diff < this.shiftTable[index]) 99 this.shiftTable[index] = diff; 100 } 101 } 102 } 103 104 108 public int matches(CharacterIterator iterator, int start, int limit) { 109 if (this.ignoreCase) return this.matchesIgnoreCase(iterator, start, limit); 110 int plength = this.pattern.length; 111 if (plength == 0) return start; 112 int index = start+plength; 113 while (index <= limit) { 114 int pindex = plength; 115 int nindex = index+1; 116 char ch; 117 do { 118 if ((ch = iterator.setIndex(--index)) != this.pattern[--pindex]) 119 break; 120 if (pindex == 0) 121 return index; 122 } while (pindex > 0); 123 index += this.shiftTable[ch % this.shiftTable.length]+1; 124 if (index < nindex) index = nindex; 125 } 126 return -1; 127 } 128 129 133 public int matches(String str, int start, int limit) { 134 if (this.ignoreCase) return this.matchesIgnoreCase(str, start, limit); 135 int plength = this.pattern.length; 136 if (plength == 0) return start; 137 int index = start+plength; 138 while (index <= limit) { 139 int pindex = plength; 141 int nindex = index+1; 142 char ch; 143 do { 144 if ((ch = str.charAt(--index)) != this.pattern[--pindex]) 145 break; 146 if (pindex == 0) 147 return index; 148 } while (pindex > 0); 149 index += this.shiftTable[ch % this.shiftTable.length]+1; 150 if (index < nindex) index = nindex; 151 } 152 return -1; 153 } 154 158 public int matches(char[] chars, int start, int limit) { 159 if (this.ignoreCase) return this.matchesIgnoreCase(chars, start, limit); 160 int plength = this.pattern.length; 161 if (plength == 0) return start; 162 int index = start+plength; 163 while (index <= limit) { 164 int pindex = plength; 166 int nindex = index+1; 167 char ch; 168 do { 169 if ((ch = chars[--index]) != this.pattern[--pindex]) 170 break; 171 if (pindex == 0) 172 return index; 173 } while (pindex > 0); 174 index += this.shiftTable[ch % this.shiftTable.length]+1; 175 if (index < nindex) index = nindex; 176 } 177 return -1; 178 } 179 180 int matchesIgnoreCase(CharacterIterator iterator, int start, int limit) { 181 int plength = this.pattern.length; 182 if (plength == 0) return start; 183 int index = start+plength; 184 while (index <= limit) { 185 int pindex = plength; 186 int nindex = index+1; 187 char ch; 188 do { 189 char ch1 = ch = iterator.setIndex(--index); 190 char ch2 = this.pattern[--pindex]; 191 if (ch1 != ch2) { 192 ch1 = Character.toUpperCase(ch1); 193 ch2 = Character.toUpperCase(ch2); 194 if (ch1 != ch2 && Character.toLowerCase(ch1) != Character.toLowerCase(ch2)) 195 break; 196 } 197 if (pindex == 0) 198 return index; 199 } while (pindex > 0); 200 index += this.shiftTable[ch % this.shiftTable.length]+1; 201 if (index < nindex) index = nindex; 202 } 203 return -1; 204 } 205 206 int matchesIgnoreCase(String text, int start, int limit) { 207 int plength = this.pattern.length; 208 if (plength == 0) return start; 209 int index = start+plength; 210 while (index <= limit) { 211 int pindex = plength; 212 int nindex = index+1; 213 char ch; 214 do { 215 char ch1 = ch = text.charAt(--index); 216 char ch2 = this.pattern[--pindex]; 217 if (ch1 != ch2) { 218 ch1 = Character.toUpperCase(ch1); 219 ch2 = Character.toUpperCase(ch2); 220 if (ch1 != ch2 && Character.toLowerCase(ch1) != Character.toLowerCase(ch2)) 221 break; 222 } 223 if (pindex == 0) 224 return index; 225 } while (pindex > 0); 226 index += this.shiftTable[ch % this.shiftTable.length]+1; 227 if (index < nindex) index = nindex; 228 } 229 return -1; 230 } 231 int matchesIgnoreCase(char[] chars, int start, int limit) { 232 int plength = this.pattern.length; 233 if (plength == 0) return start; 234 int index = start+plength; 235 while (index <= limit) { 236 int pindex = plength; 237 int nindex = index+1; 238 char ch; 239 do { 240 char ch1 = ch = chars[--index]; 241 char ch2 = this.pattern[--pindex]; 242 if (ch1 != ch2) { 243 ch1 = Character.toUpperCase(ch1); 244 ch2 = Character.toUpperCase(ch2); 245 if (ch1 != ch2 && Character.toLowerCase(ch1) != Character.toLowerCase(ch2)) 246 break; 247 } 248 if (pindex == 0) 249 return index; 250 } while (pindex > 0); 251 index += this.shiftTable[ch % this.shiftTable.length]+1; 252 if (index < nindex) index = nindex; 253 } 254 return -1; 255 } 256 257 273 } 274 275 | Popular Tags |