From 4a47af06c3c29f0b1ccc304d3ace37aa5cdbeeb3 Mon Sep 17 00:00:00 2001 From: Andrew John Hughes Date: Mon, 17 Mar 2008 03:04:16 +0000 Subject: 2008-03-17 Andrew John Hughes 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. --- gnu/java/lang/CPStringBuilder.java | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) (limited to 'gnu/java/lang/CPStringBuilder.java') diff --git a/gnu/java/lang/CPStringBuilder.java b/gnu/java/lang/CPStringBuilder.java index 64da224e2..978a2043f 100644 --- a/gnu/java/lang/CPStringBuilder.java +++ b/gnu/java/lang/CPStringBuilder.java @@ -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; } @@ -1012,24 +1016,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 String this StringBuilder * would create. Not to be confused with the capacity of the @@ -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); -- cgit v1.2.1