1 21 package au.id.jericho.lib.html; 22 23 39 public final class ParseText implements CharSequence { 40 private final char[] text; 41 42 43 public static final int NO_BREAK=-1; 44 45 49 ParseText(final CharSequence charSequence) { 50 text=new char[charSequence.length()]; 51 for (int i=0; i<text.length; i++) text[i]=Character.toLowerCase(charSequence.charAt(i)); 52 } 53 54 58 ParseText(final OutputDocument outputDocument) { 59 this(outputDocument.toString()); 60 } 61 62 71 public boolean containsAt(final String str, final int pos) { 72 for (int i=0; i<str.length(); i++) 73 if (str.charAt(i)!=text[pos+i]) return false; 74 return true; 75 } 76 77 82 public char charAt(final int index) { 83 return text[index]; 84 } 85 86 96 public int indexOf(final char searchChar, final int fromIndex) { 97 return indexOf(searchChar,fromIndex,NO_BREAK); 98 } 99 100 117 public int indexOf(final char searchChar, final int fromIndex, final int breakAtIndex) { 118 final int actualBreakAtIndex=(breakAtIndex==NO_BREAK || breakAtIndex>text.length ? text.length : breakAtIndex); 119 for (int i=(fromIndex<0 ? 0 : fromIndex); i<actualBreakAtIndex; i++) 120 if (text[i]==searchChar) return i; 121 return -1; 122 } 123 124 134 public int lastIndexOf(final char searchChar, final int fromIndex) { 135 return lastIndexOf(searchChar,fromIndex,NO_BREAK); 136 } 137 138 155 public int lastIndexOf(final char searchChar, final int fromIndex, final int breakAtIndex) { 156 for (int i=(fromIndex>text.length ? text.length : fromIndex); i>breakAtIndex; i--) 157 if (text[i]==searchChar) return i; 158 return -1; 159 } 160 161 171 public int indexOf(final String searchString, final int fromIndex) { 172 return (searchString.length()==1) 173 ? indexOf(searchString.charAt(0),fromIndex,NO_BREAK) 174 : indexOf(searchString.toCharArray(),fromIndex,NO_BREAK); 175 } 176 177 187 public int indexOf(final char[] searchCharArray, final int fromIndex) { 188 return indexOf(searchCharArray,fromIndex,NO_BREAK); 189 } 190 191 208 public int indexOf(final String searchString, final int fromIndex, final int breakAtIndex) { 209 return (searchString.length()==1) 210 ? indexOf(searchString.charAt(0),fromIndex,breakAtIndex) 211 : indexOf(searchString.toCharArray(),fromIndex,breakAtIndex); 212 } 213 214 231 public int indexOf(final char[] searchCharArray, final int fromIndex, final int breakAtIndex) { 232 if (searchCharArray.length==0) return fromIndex; 233 final char firstChar=searchCharArray[0]; 234 final int lastPossibleBreakAtIndex=text.length-searchCharArray.length+1; 235 final int actualBreakAtIndex=(breakAtIndex==NO_BREAK || breakAtIndex>lastPossibleBreakAtIndex) ? lastPossibleBreakAtIndex : breakAtIndex; 236 outerLoop: for (int i=(fromIndex<0 ? 0 : fromIndex); i<actualBreakAtIndex; i++) { 237 if (text[i]==firstChar) { 238 for (int j=1; j<searchCharArray.length; j++) 239 if (searchCharArray[j]!=text[j+i]) continue outerLoop; 240 return i; 241 } 242 } 243 return -1; 244 } 245 246 256 public int lastIndexOf(final String searchString, final int fromIndex) { 257 return (searchString.length()==1) 258 ? lastIndexOf(searchString.charAt(0),fromIndex,NO_BREAK) 259 : lastIndexOf(searchString.toCharArray(),fromIndex,NO_BREAK); 260 } 261 262 272 public int lastIndexOf(final char[] searchCharArray, final int fromIndex) { 273 return lastIndexOf(searchCharArray,fromIndex,NO_BREAK); 274 } 275 276 293 public int lastIndexOf(final String searchString, final int fromIndex, final int breakAtIndex) { 294 return (searchString.length()==1) 295 ? lastIndexOf(searchString.charAt(0),fromIndex,breakAtIndex) 296 : lastIndexOf(searchString.toCharArray(),fromIndex,breakAtIndex); 297 } 298 299 316 public int lastIndexOf(final char[] searchCharArray, int fromIndex, final int breakAtIndex) { 317 if (searchCharArray.length==0) return fromIndex; 318 final int rightIndex=text.length-searchCharArray.length; 319 if (breakAtIndex>rightIndex) return -1; 320 if (fromIndex>rightIndex) fromIndex=rightIndex; 321 final int lastCharIndex=searchCharArray.length-1; 322 final char lastChar=searchCharArray[lastCharIndex]; 323 final int actualBreakAtPos=breakAtIndex+lastCharIndex; 324 outerLoop: for (int i=fromIndex+lastCharIndex; i>actualBreakAtPos; i--) { 325 if (text[i]==lastChar) { 326 final int startIndex=i-lastCharIndex; 327 for (int j=lastCharIndex-1; j>=0; j--) 328 if (searchCharArray[j]!=text[j+startIndex]) continue outerLoop; 329 return startIndex; 330 } 331 } 332 return -1; 333 } 334 335 339 public int length() { 340 return text.length; 341 } 342 343 353 public String substring(final int beginIndex, final int endIndex) { 354 return new String (text,beginIndex,endIndex-beginIndex); 355 } 356 357 366 public CharSequence subSequence(final int beginIndex, final int endIndex) { 367 return substring(beginIndex,endIndex); 368 } 369 370 374 public String toString() { 375 return new String (text); 376 } 377 } 378 | Popular Tags |