📄 textpattern.java
字号:
while ( i < m1 ) { if ( asciiToLowerCase( a[ i ] ) == last ) { j = n - 1; k = i; while( j-- != 0 && asciiToLowerCase( a[ --k ] ) == p[ j ] ); if ( j < 0 ) return k; } if ( ( c = asciiToLowerCase( a[ ++i ] ) ) < 128 ) i += asciiShift[ c ]; else { j = shift[ c * c & m ]; k = shift[ ( c * PHI2 ) >> hs & m ]; i += j > k ? j : k; } } if ( i == m1 ) { j = n; while( j-- != 0 && asciiToLowerCase( a[ i-- ] ) == p[ j ] ); if ( j < 0 ) return i + 1; } return -1; } else { while ( i < m1 ) { if ( unicodeToLowerCase( a[ i ] ) == last ) { j = n - 1; k = i; while( j-- != 0 && unicodeToLowerCase( a[ --k ] ) == p[ j ] ); if ( j < 0 ) return k; } if ( ( c = unicodeToLowerCase( a[ ++i ] ) ) < 128 ) i += asciiShift[ c ]; else { j = shift[ c * c & m ]; k = shift[ ( c * PHI2 ) >> hs & m ]; i += j > k ? j : k; } } if ( i == m1 ) { j = n; while( j-- != 0 && unicodeToLowerCase( a[ i-- ] ) == p[ j ] ); if ( j < 0 ) return i + 1; } return -1; } } /** Returns the index of the first occurrence of this pattern in the given character sequence. * * @param s the character sequence to look in. * @return the index of the first occurrence of this pattern contained in the * given character sequence, or <code>-1</code>, if the pattern cannot be found. */ public int search( final CharSequence s ) { return search( s, 0, s.length() ); } /** Returns the index of the first occurrence of this pattern in the given character sequence starting from a given index. * * @param s the character array to look in. * @param from the index from which the search must start. * @return the index of the first occurrence of this pattern contained in the * subsequence starting from <code>from</code> (inclusive), or * <code>-1</code>, if the pattern cannot be found. */ public int search( final CharSequence s, final int from ) { return search( s, from, s.length() ); } /** Returns the index of the first occurrence of this pattern in the given character sequence between given indices. * * @param s the character array to look in. * @param from the index from which the search must start. * @param to the index at which the search must end. * @return the index of the first occurrence of this pattern contained in the * subsequence starting from <code>from</code> (inclusive) up to <code>to</code> * (exclusive) characters, or <code>-1</code>, if the pattern cannot be found. */ public int search( final CharSequence s, final int from, final int to ) { final int n = pattern.length; if ( n == 0 ) return from > to ? to : ( from < 0 ? 0 : from ); if ( n == 1 ) return indexOf( s, from, to ); final char[] p = pattern; final char last = p[ n - 1 ]; final int m1 = to - 1; final int[] shift = badCharShift; final int[] asciiShift = asciiBadCharShift; final int m = mask; final int hs = hashShift; int i = ( from < 0 ? 0 : from ) + n - 1, j, k; char c; if ( caseSensitive ) { while ( i < m1 ) { if ( s.charAt( i ) == last ) { j = n - 1; k = i; while( j-- != 0 && s.charAt( --k ) == p[ j ] ); if ( j < 0 ) return k; } if ( ( c = s.charAt( ++i ) ) < 128 ) i += asciiShift[ c ]; else { j = shift[ c * c & m ]; k = shift[ ( c * PHI2 ) >> hs & m ]; i += j > k ? j : k; } } if ( i == m1 ) { j = n; while( j-- != 0 && s.charAt( i-- ) == p[ j ] ); if ( j < 0 ) return i + 1; } return -1; } else if ( asciiCase ) { while ( i < m1 ) { if ( asciiToLowerCase( s.charAt( i ) ) == last ) { j = n - 1; k = i; while( j-- != 0 && asciiToLowerCase( s.charAt( --k ) ) == p[ j ] ); if ( j < 0 ) return k; } if ( ( c = asciiToLowerCase( s.charAt( ++i ) ) ) < 128 ) i += asciiShift[ c ]; else { j = shift[ c * c & m ]; k = shift[ ( c * PHI2 ) >> hs & m ]; i += j > k ? j : k; } } if ( i == m1 ) { j = n; while( j-- != 0 && asciiToLowerCase( s.charAt( i-- ) ) == p[ j ] ); if ( j < 0 ) return i + 1; } return -1; } else { while ( i < m1 ) { if ( unicodeToLowerCase( s.charAt( i ) ) == last ) { j = n - 1; k = i; while( j-- != 0 && unicodeToLowerCase( s.charAt( --k ) ) == p[ j ] ); if ( j < 0 ) return k; } if ( ( c = unicodeToLowerCase( s.charAt( ++i ) ) ) < 128 ) i += asciiShift[ c ]; else { j = shift[ c * c & m ]; k = shift[ ( c * PHI2 ) >> hs & m ]; i += j > k ? j : k; } } if ( i == m1 ) { j = n; while( j-- != 0 && unicodeToLowerCase( s.charAt( i-- ) ) == p[ j ] ); if ( j < 0 ) return i + 1; } return -1; } } /** Returns the index of the first occurrence of this pattern in the given byte array. * * @param a the byte array to look in. * @return the index of the first occurrence of this pattern contained in the * given byte array, or <code>-1</code>, if the pattern cannot be found. */ public int search( final byte[] a ) { return search( a, 0, a.length ); } /** Returns the index of the first occurrence of this pattern in the given byte array starting from a given index. * * @param a the byte array to look in. * @param from the index from which the search must start. * @return the index of the first occurrence of this pattern contained in the * array fragment starting from <code>from</code> (inclusive), or * <code>-1</code>, if the pattern cannot be found. */ public int search( final byte[] a, final int from ) { return search( a, from, a.length ); } /** Returns the index of the first occurrence of this pattern in the given byte array between given indices. * * @param a the byte array to look in. * @param from the index from which the search must start. * @param to the index at which the search must end. * @return the index of the first occurrence of this pattern contained in the * array fragment starting from <code>from</code> (inclusive) up to <code>to</code> * (exclusive) characters, or <code>-1</code>, if the pattern cannot be found. * TODO: this must be tested */ public int search( final byte[] a, final int from, final int to ) { final int n = pattern.length; if ( n == 0 ) return from > to ? to : ( from < 0 ? 0 : from ); if ( n == 1 ) return indexOf( a, from, to ); final char[] p = pattern; final char last = p[ n - 1 ]; final int m1 = to - 1; final int[] shift = badCharShift; final int[] asciiShift = asciiBadCharShift; final int m = mask; final int hs = hashShift; int i = ( from < 0 ? 0 : from ) + n - 1, j, k; char c; if ( caseSensitive ) { while ( i < m1 ) { if ( ( a[ i ] & 0xFF ) == last ) { j = n - 1; k = i; while( j-- != 0 && ( a[ --k ] & 0xFF )== p[ j ] ); if ( j < 0 ) return k; } if ( ( c = (char)( a[ ++i ] & 0xFF ) ) < 128 ) i += asciiShift[ c ]; else { j = shift[ c * c & m ]; k = shift[ ( c * PHI2 ) >> hs & m ]; i += j > k ? j : k; } } if ( i == m1 ) { j = n; while( j-- != 0 && ( a[ i-- ] & 0xFF ) == p[ j ] ); if ( j < 0 ) return i + 1; } return -1; } else if ( asciiCase ) { while ( i < m1 ) { if ( asciiToLowerCase( (char)( a[ i ] & 0xFF ) ) == last ) { j = n - 1; k = i; while( j-- != 0 && asciiToLowerCase( (char)( a[ --k ] & 0xFF ) ) == p[ j ] ); if ( j < 0 ) return k; } if ( ( c = asciiToLowerCase( (char)( a[ ++i ] & 0xFF ) ) ) < 128 ) i += asciiShift[ c ]; else { j = shift[ c * c & m ]; k = shift[ ( c * PHI2 ) >> hs & m ]; i += j > k ? j : k; } } if ( i == m1 ) { j = n; while( j-- != 0 && asciiToLowerCase( (char)( a[ i-- ] & 0xFF ) ) == p[ j ] ); if ( j < 0 ) return i + 1; } return -1; } else { while ( i < m1 ) { if ( unicodeToLowerCase( (char)( a[ i ] & 0xFF ) ) == last ) { j = n - 1; k = i; while( j-- != 0 && unicodeToLowerCase( (char)( a[ --k ] & 0xFF ) ) == p[ j ] ); if ( j < 0 ) return k; } if ( ( c = unicodeToLowerCase( (char)( a[ ++i ] & 0xFF ) ) ) < 128 ) i += asciiShift[ c ]; else { j = shift[ c * c & m ]; k = shift[ ( c * PHI2 ) >> hs & m ]; i += j > k ? j : k; } } if ( i == m1 ) { j = n; while( j-- != 0 && unicodeToLowerCase( (char)( a[ i-- ] & 0xFF ) ) == p[ j ] ); if ( j < 0 ) return i + 1; } return -1; } } /** Returns the index of the first occurrence of this pattern in the given character list. * * @param list the character list to look in. * @return the index of the first occurrence of this pattern contained in the * given list, or <code>-1</code>, if the pattern cannot be found. */ public int search( final CharList list ) { return search( list, 0, list.size() ); } /** Returns the index of the first occurrence of this pattern in the given character list starting from a given index. * * @param list the character list to look in. * @param from the index from which the search must start. * @return the index of the first occurrence of this pattern contained in the * sublist starting from <code>from</code> (inclusive), or * <code>-1</code>, if the pattern cannot be found. */ public int search( final CharList list, int from ) { return search( list, from, list.size() ); } /** Returns the index of the first occurrence of this pattern in the given character list between given indices. * * @param list the character list to look in. * @param from the index from which the search must start. * @param to the index at which the search must end. * @return the index of the first occurrence of this pattern contained in the * sublist starting from <code>from</code> (inclusive) up to <code>to</code> * (exclusive) characters, or <code>-1</code>, if the pattern cannot be found. */ public int search( final CharList list, final int from, final int to ) { final int n = pattern.length; if ( n == 0 ) return from > to ? to : ( from < 0 ? 0 : from ); if ( n == 1 ) return list.subList( from, to ).indexOf( pattern[ 0 ] ); final char[] p = pattern; final char last = p[ n - 1 ]; final int m1 = to - 1; final int[] shift = badCharShift; final int[] asciiShift = asciiBadCharShift; final int m = mask; final int hs = hashShift; int i = ( from < 0 ? 0 : from ) + n - 1, j, k; char c; if ( caseSensitive ) { while ( i < m1 ) { if ( list.getChar( i ) == last ) { j = n - 1; k = i; while( j-- != 0 && list.getChar( --k ) == p[ j ] ); if ( j < 0 ) return k; } if ( ( c = list.getChar( ++i ) ) < 128 ) i += asciiShift[ c ]; else { j = shift[ c * c & m ]; k = shift[ ( c * PHI2 ) >> hs & m ]; i += j > k ? j : k; } } if ( i == m1 ) { j = n; while( j-- != 0 && list.getChar( i-- ) == p[ j ] ); if ( j < 0 ) return i + 1; } return -1; } else if ( asciiCase ) { while ( i < m1 ) { if ( asciiToLowerCase( list.getChar( i ) ) == last ) { j = n - 1; k = i; while( j-- != 0 && asciiToLowerCase( list.getChar( --k ) ) == p[ j ] ); if ( j < 0 ) return k; } if ( ( c = asciiToLowerCase( list.getChar( ++i ) ) ) < 128 ) i += asciiShift[ c ]; else { j = shift[ c * c & m ]; k = shift[ ( c * PHI2 ) >> hs & m ]; i += j > k ? j : k; } } if ( i == m1 ) { j = n; while( j-- != 0 && asciiToLowerCase( list.getChar( i-- ) ) == p[ j ] ); if ( j < 0 ) return i + 1; } return -1; } else { while ( i < m1 ) { if ( unicodeToLowerCase( list.getChar( i ) ) == last ) { j = n - 1; k = i; while( j-- != 0 && unicodeToLowerCase( list.getChar( --k ) ) == p[ j ] ); if ( j < 0 ) return k; } if ( ( c = unicodeToLowerCase( list.getChar( ++i ) ) ) < 128 ) i += asciiShift[ c ]; else { j = shift[ c * c & m ]; k = shift[ ( c * PHI2 ) >> hs & m ]; i += j > k ? j : k; } } if ( i == m1 ) { j = n; while( j-- != 0 && unicodeToLowerCase( list.getChar( i-- ) ) == p[ j ] ); if ( j < 0 ) return i + 1; } return -1; } } /** Compares this text pattern to another object. * * <P>This method will return <code>true</code> iff its argument * is a <code>TextPattern</code> containing the same constant pattern with the same flags set. * * @param o an object. * @return true if the argument is a <code>TextPattern</code>s that contains the same constant pattern of this text pattern * and has the same flags set. */ final public boolean equals( final Object o ) { if ( o instanceof TextPattern ) { TextPattern p = (TextPattern)o; return caseSensitive == p.caseSensitive && asciiCase == p.asciiCase && java.util.Arrays.equals( p.pattern, pattern ); } return false; } /** Returns a hash code for this text pattern. * * <P>The hash code of a text pattern is the same as that of a * <code>String</code> with the same content (suitably lower cased, if the pattern is case insensitive). * * @return a hash code array for this object. * @see java.lang.String#hashCode() */ final public int hashCode() { final char[] a = pattern; final int l = a.length; int h; for ( int i = h = 0; i < l; i++ ) h = 31 * h + a[ i ]; return h; } final public String toString() { return new String( pattern ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -