1 36 package org.columba.ristretto.parser; 37 38 import java.util.ArrayList ; 39 import java.util.Arrays ; 40 import java.util.List ; 41 42 43 49 public class CharSequenceSearcher { 50 private int[] badCharacterSkip; 51 private int[] goodSuffixSkip; 52 53 private char[] pattern; 54 private int patternLength; 55 56 57 62 public CharSequenceSearcher(String pattern) { 63 this(pattern.toCharArray()); 64 } 65 66 71 public CharSequenceSearcher(char[] pattern) { 72 badCharacterSkip = new int[256]; 73 74 setPattern(pattern); 75 } 76 77 83 public void setPattern(char[] pattern) { 84 this.pattern = pattern; 85 patternLength = pattern.length; 86 goodSuffixSkip = new int[patternLength + 1]; 87 88 initGoodSuffixSkipArray(); 90 initBadCharSkipArray(); 91 } 92 93 private void initGoodSuffixSkipArray() { 94 int i,j,p; 95 int[] f = new int[patternLength + 1]; 96 97 j = patternLength + 1; 98 f[patternLength] = j; 99 100 for (i = patternLength; i > 0; i--) { 101 while (j <= patternLength && pattern[i - 1] != pattern[j - 1]) { 102 if (goodSuffixSkip[j] == 0) { 103 goodSuffixSkip[j] = j - i; 104 } 105 j = f[j]; 106 } 107 f[i - 1] = --j; 108 } 109 110 p = f[0]; 111 for (j = 0; j <= patternLength; ++j) { 112 if (goodSuffixSkip[j] == 0) { 113 goodSuffixSkip[j] = p; 114 } 115 if (j == p) { 116 p = f[p]; 117 } 118 } 119 } 120 121 private void initBadCharSkipArray() { 122 Arrays.fill(badCharacterSkip, patternLength); 123 124 for (int j = 0; j < patternLength - 1; j++) { 125 badCharacterSkip[pattern[j]] = patternLength - j - 1; 126 } 127 } 128 129 137 public List match(CharSequence text) { 138 int i,j; 139 int textLength = text.length(); 140 List result = new ArrayList (); 141 142 i = 0; 143 while (i <= textLength - patternLength) { 144 for (j = patternLength - 1; j >= 0 145 && pattern[j] == text.charAt(i + j); --j) { 146 } 147 148 if (j < 0) { 149 result.add(new Integer (i)); 150 151 i += goodSuffixSkip[0]; 152 } else { 153 i += Math.max( 154 goodSuffixSkip[j + 1], 155 badCharacterSkip[text.charAt(i + j)] - patternLength + j + 1); 156 } 157 } 158 159 return result; 160 } 161 }
| Popular Tags
|