1 16 17 package org.apache.xerces.impl.xpath.regex; 18 19 import java.text.CharacterIterator ; 20 21 28 public class BMPattern { 29 char[] pattern; 30 int[] shiftTable; 31 boolean ignoreCase; 32 33 public BMPattern(String pat, boolean ignoreCase) { 34 this(pat, 256, ignoreCase); 35 } 36 37 public BMPattern(String pat, int tableSize, boolean ignoreCase) { 38 this.pattern = pat.toCharArray(); 39 this.shiftTable = new int[tableSize]; 40 this.ignoreCase = ignoreCase; 41 42 int length = pattern.length; 43 for (int i = 0; i < this.shiftTable.length; i ++) 44 this.shiftTable[i] = length; 45 46 for (int i = 0; i < length; i ++) { 47 char ch = this.pattern[i]; 48 int diff = length-i-1; 49 int index = ch % this.shiftTable.length; 50 if (diff < this.shiftTable[index]) 51 this.shiftTable[index] = diff; 52 if (this.ignoreCase) { 53 ch = Character.toUpperCase(ch); 54 index = ch % this.shiftTable.length; 55 if (diff < this.shiftTable[index]) 56 this.shiftTable[index] = diff; 57 ch = Character.toLowerCase(ch); 58 index = ch % this.shiftTable.length; 59 if (diff < this.shiftTable[index]) 60 this.shiftTable[index] = diff; 61 } 62 } 63 } 64 65 69 public int matches(CharacterIterator iterator, int start, int limit) { 70 if (this.ignoreCase) return this.matchesIgnoreCase(iterator, start, limit); 71 int plength = this.pattern.length; 72 if (plength == 0) return start; 73 int index = start+plength; 74 while (index <= limit) { 75 int pindex = plength; 76 int nindex = index+1; 77 char ch; 78 do { 79 if ((ch = iterator.setIndex(--index)) != this.pattern[--pindex]) 80 break; 81 if (pindex == 0) 82 return index; 83 } while (pindex > 0); 84 index += this.shiftTable[ch % this.shiftTable.length]+1; 85 if (index < nindex) index = nindex; 86 } 87 return -1; 88 } 89 90 94 public int matches(String str, int start, int limit) { 95 if (this.ignoreCase) return this.matchesIgnoreCase(str, start, limit); 96 int plength = this.pattern.length; 97 if (plength == 0) return start; 98 int index = start+plength; 99 while (index <= limit) { 100 int pindex = plength; 102 int nindex = index+1; 103 char ch; 104 do { 105 if ((ch = str.charAt(--index)) != this.pattern[--pindex]) 106 break; 107 if (pindex == 0) 108 return index; 109 } while (pindex > 0); 110 index += this.shiftTable[ch % this.shiftTable.length]+1; 111 if (index < nindex) index = nindex; 112 } 113 return -1; 114 } 115 119 public int matches(char[] chars, int start, int limit) { 120 if (this.ignoreCase) return this.matchesIgnoreCase(chars, start, limit); 121 int plength = this.pattern.length; 122 if (plength == 0) return start; 123 int index = start+plength; 124 while (index <= limit) { 125 int pindex = plength; 127 int nindex = index+1; 128 char ch; 129 do { 130 if ((ch = chars[--index]) != this.pattern[--pindex]) 131 break; 132 if (pindex == 0) 133 return index; 134 } while (pindex > 0); 135 index += this.shiftTable[ch % this.shiftTable.length]+1; 136 if (index < nindex) index = nindex; 137 } 138 return -1; 139 } 140 141 int matchesIgnoreCase(CharacterIterator iterator, int start, int limit) { 142 int plength = this.pattern.length; 143 if (plength == 0) return start; 144 int index = start+plength; 145 while (index <= limit) { 146 int pindex = plength; 147 int nindex = index+1; 148 char ch; 149 do { 150 char ch1 = ch = iterator.setIndex(--index); 151 char ch2 = this.pattern[--pindex]; 152 if (ch1 != ch2) { 153 ch1 = Character.toUpperCase(ch1); 154 ch2 = Character.toUpperCase(ch2); 155 if (ch1 != ch2 && Character.toLowerCase(ch1) != Character.toLowerCase(ch2)) 156 break; 157 } 158 if (pindex == 0) 159 return index; 160 } while (pindex > 0); 161 index += this.shiftTable[ch % this.shiftTable.length]+1; 162 if (index < nindex) index = nindex; 163 } 164 return -1; 165 } 166 167 int matchesIgnoreCase(String text, int start, int limit) { 168 int plength = this.pattern.length; 169 if (plength == 0) return start; 170 int index = start+plength; 171 while (index <= limit) { 172 int pindex = plength; 173 int nindex = index+1; 174 char ch; 175 do { 176 char ch1 = ch = text.charAt(--index); 177 char ch2 = this.pattern[--pindex]; 178 if (ch1 != ch2) { 179 ch1 = Character.toUpperCase(ch1); 180 ch2 = Character.toUpperCase(ch2); 181 if (ch1 != ch2 && Character.toLowerCase(ch1) != Character.toLowerCase(ch2)) 182 break; 183 } 184 if (pindex == 0) 185 return index; 186 } while (pindex > 0); 187 index += this.shiftTable[ch % this.shiftTable.length]+1; 188 if (index < nindex) index = nindex; 189 } 190 return -1; 191 } 192 int matchesIgnoreCase(char[] chars, int start, int limit) { 193 int plength = this.pattern.length; 194 if (plength == 0) return start; 195 int index = start+plength; 196 while (index <= limit) { 197 int pindex = plength; 198 int nindex = index+1; 199 char ch; 200 do { 201 char ch1 = ch = chars[--index]; 202 char ch2 = this.pattern[--pindex]; 203 if (ch1 != ch2) { 204 ch1 = Character.toUpperCase(ch1); 205 ch2 = Character.toUpperCase(ch2); 206 if (ch1 != ch2 && Character.toLowerCase(ch1) != Character.toLowerCase(ch2)) 207 break; 208 } 209 if (pindex == 0) 210 return index; 211 } while (pindex > 0); 212 index += this.shiftTable[ch % this.shiftTable.length]+1; 213 if (index < nindex) index = nindex; 214 } 215 return -1; 216 } 217 218 234 } 235 236 | Popular Tags |