@@ -95,7 +95,7 @@ public static Integer getLastComparisons() {
9595 * @return index of first occurrence of the pattern in the text
9696 */
9797 private static int firstOccurrence (String pattern , String text , boolean caseSensitive ) {
98- shiftValues = calcShiftValues (pattern ); // build the bad symbol table
98+ shiftValues = calcShiftValues (pattern , caseSensitive ); // build the bad symbol table
9999 comparisons = 0 ; // reset comparisons
100100
101101 if (pattern .length () == 0 ) { // return failure, if pattern empty
@@ -113,7 +113,7 @@ private static int firstOccurrence(String pattern, String text, boolean caseSens
113113 char patternChar = pattern .charAt (i );
114114 char textChar = text .charAt ((textIndex + i ) - (pattern .length () - 1 ));
115115 if (!charEquals (patternChar , textChar , caseSensitive )) { // bad character, shift pattern
116- textIndex += getShiftValue (text .charAt (textIndex ));
116+ textIndex += getShiftValue (text .charAt (textIndex ), caseSensitive );
117117 break ;
118118 }
119119 i --;
@@ -153,14 +153,15 @@ private static boolean charEquals(char c1, char c2, boolean caseSensitive) {
153153 * in the table, then it is not a rightmost occurrence, so it continues.
154154 *
155155 * @param pattern basis for the bad symbol table
156+ * @param caseSensitive whether to build the table with case-sensitive keys
156157 * @return the bad symbol table
157158 */
158- private static HashMap <Character , Integer > calcShiftValues (String pattern ) {
159+ private static HashMap <Character , Integer > calcShiftValues (String pattern , boolean caseSensitive ) {
159160 patternLength = pattern .length ();
160161 HashMap <Character , Integer > table = new HashMap <>();
161162
162163 for (int i = pattern .length () - 2 ; i >= 0 ; i --) { // length - 2 is the index of the second to last character
163- char c = pattern .charAt (i );
164+ char c = caseSensitive ? pattern .charAt (i ) : Character . toLowerCase ( pattern . charAt ( i ) );
164165 int finalI = i ;
165166 table .computeIfAbsent (c , k -> pattern .length () - 1 - finalI );
166167 }
@@ -173,11 +174,14 @@ private static HashMap<Character, Integer> calcShiftValues(String pattern) {
173174 * appropriate shift value for a given character
174175 *
175176 * @param c character
177+ * @param caseSensitive whether the table was built with case-sensitive keys
176178 * @return shift value that corresponds to the character argument
177179 */
178- private static Integer getShiftValue (char c ) {
179- if (shiftValues .get (c ) != null ) {
180- return shiftValues .get (c );
180+ private static Integer getShiftValue (char c , boolean caseSensitive ) {
181+ // the table holds lower-case keys for case-insensitive searches
182+ char key = caseSensitive ? c : Character .toLowerCase (c );
183+ if (shiftValues .get (key ) != null ) {
184+ return shiftValues .get (key );
181185 } else {
182186 return patternLength ;
183187 }
0 commit comments