📄 mutablestring.java
字号:
hashLength = -1; array = CharArrays.trim( array, l ); } else hashLength = l; } return this; } /** Replaces the characters with indices ranging from <code>start</code> (inclusive) * to <code>end</code> (exclusive) with the given mutable string. * * @param start The starting index (inclusive). * @param end The ending index (exclusive). * @param s The mutable string to be copied. * @return this mutable string. * @throws IndexOutOfBoundsException if <code>start</code> is negative, * greater than <code>length()</code>, or greater than <code>end</code>. */ final public MutableString replace( final int start, int end, final MutableString s ) { final int length = length(); if ( end > length ) end = length; if ( start > end ) throw new StringIndexOutOfBoundsException(); final int l = s.length(); final int newLength = length + l - end + start; if ( l == 0 && newLength == length ) return this; if ( newLength >= length ) { expand( newLength ); System.arraycopy( array, end, array, start + l, length - end ); System.arraycopy( s.array, 0, array, start, l ); hashLength = hashLength < 0 ? -1 : newLength; } else { System.arraycopy( array, end, array, start + l, length - end ); System.arraycopy( s.array, 0, array, start, l ); if ( hashLength < 0 ) { setCapacity( newLength ); hashLength = -1; } else hashLength = newLength; } return this; } /** Replaces the characters with indices ranging from <code>start</code> (inclusive) * to <code>end</code> (exclusive) with the given <code>String</code>. * * @param start The starting index (inclusive). * @param end The ending index (exclusive). * @param s The <code>String</code> to be copied. * @return this mutable string. * @throws IndexOutOfBoundsException if <code>start</code> is negative, * greater than <code>length()</code>, or greater than <code>end</code>. */ final public MutableString replace( final int start, int end, final String s ) { final int length = length(); if ( end > length ) end = length; if ( start > end ) throw new StringIndexOutOfBoundsException(); final int l = s.length(); final int newLength = length + l - end + start; if ( l == 0 && newLength == length ) return this; if ( newLength >= length ) { expand( newLength ); System.arraycopy( array, end, array, start + l, length - end ); s.getChars( 0, l, array, start ); hashLength = hashLength < 0 ? -1 : newLength; } else { System.arraycopy( array, end, array, start + l, length - end ); s.getChars( 0, l, array, start ); if ( hashLength < 0 ) { setCapacity( newLength ); hashLength = -1; } else hashLength = newLength; } return this; } /** Replaces the characters with indices ranging from <code>start</code> (inclusive) * to <code>end</code> (exclusive) with the given <code>CharSequence</code>. * * @param start The starting index (inclusive). * @param end The ending index (exclusive). * @param s The <code>CharSequence</code> to be copied. * @return this mutable string. * @throws IndexOutOfBoundsException if <code>start</code> is negative, * greater than <code>length()</code>, or greater than <code>end</code>. */ final public MutableString replace( final int start, int end, final CharSequence s ) { final int length = length(); if ( end > length ) end = length; if ( start > end ) throw new StringIndexOutOfBoundsException(); final int l = s.length(); final int newLength = length + l - end + start; if ( l == 0 && newLength == length ) return this; if ( newLength >= length ) { expand( newLength ); System.arraycopy( array, end, array, start + l, length - end ); getChars( s, 0, l, array, start ); hashLength = hashLength < 0 ? -1 : newLength; } else { System.arraycopy( array, end, array, start + l, length - end ); getChars( s, 0, l, array, start ); if ( hashLength < 0 ) { setCapacity( newLength ); hashLength = -1; } else hashLength = newLength; } return this; } /** Replaces the characters with indices ranging from <code>start</code> (inclusive) * to <code>end</code> (exclusive) with the given character. * * @param start The starting index (inclusive). * @param end The ending index (exclusive). * @param c The character to be copied. * @return this mutable string. * @throws IndexOutOfBoundsException if <code>start</code> is negative, * greater than <code>length()</code>, or greater than <code>end</code>. */ final public MutableString replace( final int start, int end, final char c ) { final int length = length(); if ( end > length ) end = length; if ( start > end ) throw new StringIndexOutOfBoundsException(); final int newLength = length + 1 - end + start; if ( newLength >= length ) { expand( newLength ); // TODO: optimise for the case end == start + 1 System.arraycopy( array, end, array, start + 1, length - end ); array[ start ] = c; hashLength = hashLength < 0 ? -1 : newLength; } else { System.arraycopy( array, end, array, start + 1, length - end ); array[ start ] = c; if ( hashLength < 0 ) { setCapacity( newLength ); hashLength = -1; } else hashLength = newLength; } return this; } /** Replaces the content of this mutable string with the given mutable string. * * @param s the mutable string whose content will replace the present one. * @return this mutable string. */ final public MutableString replace( final MutableString s ) { return replace( 0, Integer.MAX_VALUE, s ); } /** Replaces the content of this mutable string with the given string. * * @param s the string whose content will replace the present one. * @return this mutable string. */ final public MutableString replace( final String s ) { return replace( 0, Integer.MAX_VALUE, s ); } /** Replaces the content of this mutable string with the given character sequence. * * @param s the character sequence whose content will replace the present one. * @return this mutable string. */ final public MutableString replace( final CharSequence s ) { return replace( 0, Integer.MAX_VALUE, s ); } /** Replaces the content of this mutable string with the given character. * * @param c the character whose content will replace the present content. * @return this mutable string. */ final public MutableString replace( final char c ) { return replace( 0, Integer.MAX_VALUE, c ); } /** Replaces each occurrence of a set of characters with a corresponding mutable string. * * <P>Each occurrences of the character <code>c[i]</code> in this mutable * string will be replaced by <code>s[i]</code>. Note that * <code>c</code> and <code>s</code> must have the same length, and that * <code>c</code> must not contain duplicates. Moreover, each replacement * string must be nonempty. * * <P>This method uses a Bloom filter to avoid repeated linear scans of the * character array for {@link #length()} times; however, this optimisation * will be most effective with arrays of less than twenty characters, * and, in fact, will very slightly slow down replacements with more than one hundred characters. * * <P>This method will try <em>at most</em> one reallocation. * * @param c an array of characters to be replaced. * @param s an array of replacement mutable strings. * @return this mutable string. * @throws IllegalArgumentException if one of the replacement strings is empty. */ final public MutableString replace( final char[] c, final MutableString[] s ) { final int n = c.length; if ( n == 0 ) return this; final int length = length(); char[] a = array; int i, j, k, l, newLength = length, bloomFilter = 0; k = n; while( k-- != 0 ) { bloomFilter |= 1 << ( c[ k ] & 0x1F ); if ( s[ k ].length() == 0 ) throw new IllegalArgumentException( "You cannot use the empty string as a replacement" ); } i = length; boolean found = false; while ( i-- != 0 ) { if ( ( bloomFilter & ( 1 << ( a[ i ] & 0x1F ) ) ) != 0 ) { k = n; while ( k-- != 0 ) if ( a[ i ] == c[ k ] ) break; if ( k >= 0 ) { newLength += s[ k ].length() - 1; found = true; } } } if ( ! found ) return this; expand( newLength ); a = array; i = newLength; // index in the new string j = length; // index in the old string while( j-- != 0 ) { if ( ( bloomFilter & ( 1 << ( a[ j ] & 0x1F ) ) ) != 0 ) { k = n; while ( k-- != 0 ) if ( a[ j ] == c[ k ] ) break; if ( k >= 0 ) { l = s[ k ].length(); System.arraycopy( s[ k ].array, 0, array, i -= l, l ); continue; } } a[ --i ] = a[ j ]; } hashLength = hashLength < 0 ? -1 : newLength; return this; } /** Replaces each occurrence of a set of characters with a corresponding string. * * <P>Each occurrences of the character <code>c[i]</code> in this mutable * string will be replaced by <code>s[i]</code>. Note that * <code>c</code> and <code>s</code> must have the same length, and that * <code>c</code> must not contain duplicates. Moreover, each replacement * string must be nonempty. * * <P>This method uses a Bloom filter to avoid repeated linear scans of the * character array for {@link #length()} times; however, this optimisation * will be most effective with arrays of less than twenty characters, * and, in fact, will very slightly slow down replacements with more than one hundred characters. * * <P>This method will try <em>at most</em> one reallocation. * * @param c an array of characters to be replaced. * @param s an array of replacement strings. * @return this mutable string. * @throws IllegalArgumentException if one of the replacement strings is empty. */ final public MutableString replace( final char[] c, final String[] s ) { final int n = c.length; if ( n == 0 ) return this; final int length = length(); char[] a = array; int i, j, k, l, newLength = length, bloomFilter = 0; k = n; while( k-- != 0 ) { bloomFilter |= 1 << ( c[ k ] & 0x1F ); if ( s[ k ].length() == 0 ) throw new IllegalArgumentException( "You cannot use the empty string as a replacement" ); } i = length; boolean found = false; while ( i-- != 0 ) { if ( ( bloomFilter & ( 1 << ( a[ i ] & 0x1F ) ) ) != 0 ) { k = n; while ( k-- != 0 ) if ( a[ i ] == c[ k ] ) break; if ( k >= 0 ) { newLength += s[ k ].length() - 1; found = true; } } } if ( ! found ) return this; expand( newLength ); a = array; i = newLength; // index in the new string j = length; // index in the old string while( j-- != 0 ) { if ( ( bloomFilter & ( 1 << ( a[ j ] & 0x1F ) ) ) != 0 ) { k = n; while ( k-- != 0 ) if ( a[ j ] == c[ k ] ) break; if ( k >= 0 ) { l = s[ k ].length(); getChars( s[ k ], 0, l, array, i -= l ); continue; } } a[ --i ] = a[ j ]; } hashLength = hashLength < 0 ? -1 : newLength; return this; } /** Replaces each occurrence of a set of characters with a corresponding character sequence. * * <P>Each occurrences of the character <code>c[i]</code> in this mutable * string will be replaced by <code>s[i]</code>. Note that * <code>c</code> and <code>s</code> must have the same length, and that * <code>c</code> must not conta
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -