Refactoring StringBuilder so that it may work outside the java.lang namespace in CPStringBuilder seems to have caused the code for indexOf to be broken. This fixes it.
ChangeLog: 2008-03-17 Andrew John Hughes <[EMAIL PROTECTED]> PR classpath/21869: * gnu/java/lang/CPStringBuilder.java: (indexOf(String,int)): Use regionMatches from String. (lastIndexOf(String,int)): Likewise. (regionMatches(int,String)): Removed broken code. (substring(int,int)): Rearrange index computation so it is only computed if valid. -- Andrew :) Support Free Java! Contribute to GNU Classpath and the OpenJDK http://www.gnu.org/software/classpath http://openjdk.java.net PGP Key: 94EFD9D8 (http://subkeys.pgp.net) Fingerprint = F8EF F1EA 401E 2E60 15FA 7927 142C 2591 94EF D9D8
Index: gnu/java/lang/CPStringBuilder.java =================================================================== RCS file: /sources/classpath/classpath/gnu/java/lang/CPStringBuilder.java,v retrieving revision 1.3 diff -u -3 -p -u -r1.3 CPStringBuilder.java --- gnu/java/lang/CPStringBuilder.java 17 Mar 2008 00:14:35 -0000 1.3 +++ gnu/java/lang/CPStringBuilder.java 17 Mar 2008 02:58:01 -0000 @@ -829,9 +829,11 @@ public final class CPStringBuilder { if (fromIndex < 0) fromIndex = 0; - int limit = count - str.length(); - for ( ; fromIndex <= limit; fromIndex++) - if (regionMatches(fromIndex, str)) + int olength = str.length(); + int limit = count - olength; + String s = VMCPStringBuilder.toString(value, 0, count); + for (; fromIndex <= limit; ++fromIndex) + if (s.regionMatches(fromIndex, str, 0, olength)) return fromIndex; return -1; } @@ -865,8 +867,10 @@ public final class CPStringBuilder public int lastIndexOf(String str, int fromIndex) { fromIndex = Math.min(fromIndex, count - str.length()); + String s = VMCPStringBuilder.toString(value, 0, count); + int olength = str.length(); for ( ; fromIndex >= 0; fromIndex--) - if (regionMatches(fromIndex, str)) + if (s.regionMatches(fromIndex, str, 0, olength)) return fromIndex; return -1; } @@ -1013,24 +1017,6 @@ public final class CPStringBuilder } /** - * Predicate which determines if a substring of this matches another String - * starting at a specified offset for each String and continuing for a - * specified length. This is more efficient than creating a String to call - * indexOf on. - * - * @param toffset index to start comparison at for this String - * @param other non-null String to compare to region of this - * @return true if regions match, false otherwise - * @see #indexOf(String, int) - * @see #lastIndexOf(String, int) - * @see String#regionMatches(boolean, int, String, int, int) - */ - private boolean regionMatches(int toffset, String other) - { - return new String().regionMatches(toffset, other, 0, other.length()); - } - - /** * Get the length of the <code>String</code> this <code>StringBuilder</code> * would create. Not to be confused with the <em>capacity</em> of the * <code>StringBuilder</code>. @@ -1074,9 +1060,9 @@ public final class CPStringBuilder */ public String substring(int beginIndex, int endIndex) { - int len = endIndex - beginIndex; if (beginIndex < 0 || endIndex > count || endIndex < beginIndex) throw new StringIndexOutOfBoundsException(); + int len = endIndex - beginIndex; if (len == 0) return ""; return VMCPStringBuilder.toString(value, beginIndex, len);
signature.asc
Description: Digital signature