restartPattern:
for (;;) {
//System.out.println("RESET " + start);
UTF8Reader clobReader = getCharacterStreamAtPos(start, synchronization);
if (clobReader == null)
return -1;
// start of any match of the complete pattern.
int patternIndex = 0;
char[] tmpPattern = null;
boolean needPattern = true;
// how many characters of the patter segment we have matched
int matchCount = 0;
long currentPosition = start;
int clobOffset = -1;
int read = -1;
// absolute position of a possible match
long matchPosition = -1;
// absolute position of the next possible match
long nextBestMatchPosition = -1;
//System.out.println("restartPattern: " + start);
search:
for (;;)
{
//System.out.println("search: " + needPattern + " -- " + clobOffset);
if (needPattern) {
String tmpPatternS;
if ((patternLength - patternIndex) > 256)
tmpPatternS = searchStr.substring(patternIndex, 256);
else
tmpPatternS = searchStr;
tmpPattern = tmpPatternS.toCharArray();
needPattern = false;
matchCount = 0;
}
if (clobOffset == -1) {
read = clobReader.read(tmpClob, 0, tmpClob.length);
//System.out.println("MORE DATA " + read);
if (read == -1)
return -1;
if (read == 0)
continue search;
clobOffset = 0;
}
// find matches within our two temp arrays.
compareArrays:
for (; clobOffset < read; clobOffset++) {
//System.out.println("compareArrays " + clobOffset);
char clobC = tmpClob[clobOffset];
if (clobC == tmpPattern[matchCount])
{
if (matchPosition == -1) {
matchPosition = currentPosition + clobOffset;
}
matchCount++;
// have we matched the entire pattern segment
if (matchCount == tmpPattern.length)
{
// move onto the next segment.
patternIndex += tmpPattern.length;
if (patternIndex == patternLength) {
// complete match !!
clobReader.close();
//System.out.println("COMPLETE@" + matchPosition);
return matchPosition;
}
needPattern = true;
continue search;
}
if (clobC == tmpPattern[0]) {
// save the next best start position.
// must be the first character of the actual pattern
if (patternIndex == 0) {
// must not be just a repeat of the match of the first character
if (matchCount != 1) {
// must not have a previous next best.
if (nextBestMatchPosition == -1) {
nextBestMatchPosition = currentPosition + clobOffset;
}
}
}
}
continue compareArrays;
}
else
{
// not a match
//
//
if (matchPosition != -1) {
// failed after we matched some amount of the pattern
matchPosition = -1;
// See if we found a next best match
if (nextBestMatchPosition == -1)
{
// NO - just continue on, re-starting at this character
if (patternIndex != 0) {
needPattern = true;
continue search;
}
}
else if (nextBestMatchPosition >= currentPosition)
{
// restart in the current array
clobOffset = (int) (nextBestMatchPosition - currentPosition);
nextBestMatchPosition = -1;
if (patternIndex != 0) {
needPattern = true;
continue search;
}
}
else
{
clobReader.close();
start = nextBestMatchPosition;
continue restartPattern;
}
clobOffset--; // since the continue will increment it