Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 63288 invoked from network); 6 Apr 2011 17:21:59 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 6 Apr 2011 17:21:59 -0000 Received: (qmail 79965 invoked by uid 500); 6 Apr 2011 17:21:58 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 79908 invoked by uid 500); 6 Apr 2011 17:21:58 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 79901 invoked by uid 99); 6 Apr 2011 17:21:58 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 06 Apr 2011 17:21:58 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 06 Apr 2011 17:21:57 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 0759F23889BB; Wed, 6 Apr 2011 17:21:37 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1089540 - /commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java Date: Wed, 06 Apr 2011 17:21:36 -0000 To: commits@commons.apache.org From: bayard@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110406172137.0759F23889BB@eris.apache.org> Author: bayard Date: Wed Apr 6 17:21:36 2011 New Revision: 1089540 URL: http://svn.apache.org/viewvc?rev=1089540&view=rev Log: Adding green regionMatches implementation and moving endsWith and startsWith methods to use it; thus giving them CharSequence based APIs. LANG-687 Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java?rev=1089540&r1=1089539&r2=1089540&view=diff ============================================================================== --- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java (original) +++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java Wed Apr 6 17:21:36 2011 @@ -6103,7 +6103,7 @@ public class StringUtils { //----------------------------------------------------------------------- /** - *

Check if a String starts with a specified prefix.

+ *

Check if a CharSequence starts with a specified prefix.

* *

{@code null}s are handled without exceptions. Two {@code null} * references are considered to be equal. The comparison is case sensitive.

@@ -6117,18 +6117,18 @@ public class StringUtils { * * * @see java.lang.String#startsWith(String) - * @param str the String to check, may be null + * @param str the CharSequence to check, may be null * @param prefix the prefix to find, may be null - * @return {@code true} if the String starts with the prefix, case sensitive, or + * @return {@code true} if the CharSequence starts with the prefix, case sensitive, or * both {@code null} * @since 2.4 */ - public static boolean startsWith(String str, String prefix) { + public static boolean startsWith(CharSequence str, CharSequence prefix) { return startsWith(str, prefix, false); } /** - *

Case insensitive check if a String starts with a specified prefix.

+ *

Case insensitive check if a CharSequence starts with a specified prefix.

* *

{@code null}s are handled without exceptions. Two {@code null} * references are considered to be equal. The comparison is case insensitive.

@@ -6142,39 +6142,39 @@ public class StringUtils { * * * @see java.lang.String#startsWith(String) - * @param str the String to check, may be null + * @param str the CharSequence to check, may be null * @param prefix the prefix to find, may be null - * @return {@code true} if the String starts with the prefix, case insensitive, or + * @return {@code true} if the CharSequence starts with the prefix, case insensitive, or * both {@code null} * @since 2.4 */ - public static boolean startsWithIgnoreCase(String str, String prefix) { + public static boolean startsWithIgnoreCase(CharSequence str, CharSequence prefix) { return startsWith(str, prefix, true); } /** - *

Check if a String starts with a specified prefix (optionally case insensitive).

+ *

Check if a CharSequence starts with a specified prefix (optionally case insensitive).

* * @see java.lang.String#startsWith(String) - * @param str the String to check, may be null + * @param str the CharSequence to check, may be null * @param prefix the prefix to find, may be null * @param ignoreCase inidicates whether the compare should ignore case * (case insensitive) or not. - * @return {@code true} if the String starts with the prefix or + * @return {@code true} if the CharSequence starts with the prefix or * both {@code null} */ - private static boolean startsWith(String str, String prefix, boolean ignoreCase) { + private static boolean startsWith(CharSequence str, CharSequence prefix, boolean ignoreCase) { if (str == null || prefix == null) { return (str == null && prefix == null); } if (prefix.length() > str.length()) { return false; } - return str.regionMatches(ignoreCase, 0, prefix, 0, prefix.length()); + return regionMatchesSequence(str, ignoreCase, 0, prefix, 0, prefix.length()); } /** - *

Check if a String starts with any of an array of specified strings.

+ *

Check if a CharSequence starts with any of an array of specified strings.

* *
      * StringUtils.startsWithAny(null, null)      = false
@@ -6185,18 +6185,18 @@ public class StringUtils {
      * StringUtils.startsWithAny("abcxyz", new String[] {null, "xyz", "abc"}) = true
      * 
* - * @param string the String to check, may be null - * @param searchStrings the Strings to find, may be null or empty - * @return {@code true} if the String starts with any of the the prefixes, case insensitive, or + * @param string the CharSequence to check, may be null + * @param searchStrings the CharSequences to find, may be null or empty + * @return {@code true} if the CharSequence starts with any of the the prefixes, case insensitive, or * both {@code null} * @since 2.5 */ - public static boolean startsWithAny(String string, String... searchStrings) { + public static boolean startsWithAny(CharSequence string, CharSequence... searchStrings) { if (isEmpty(string) || ArrayUtils.isEmpty(searchStrings)) { return false; } for (int i = 0; i < searchStrings.length; i++) { - String searchString = searchStrings[i]; + CharSequence searchString = searchStrings[i]; if (StringUtils.startsWith(string, searchString)) { return true; } @@ -6208,7 +6208,7 @@ public class StringUtils { //----------------------------------------------------------------------- /** - *

Check if a String ends with a specified suffix.

+ *

Check if a CharSequence ends with a specified suffix.

* *

{@code null}s are handled without exceptions. Two {@code null} * references are considered to be equal. The comparison is case sensitive.

@@ -6223,18 +6223,18 @@ public class StringUtils { * * * @see java.lang.String#endsWith(String) - * @param str the String to check, may be null + * @param str the CharSequence to check, may be null * @param suffix the suffix to find, may be null - * @return {@code true} if the String ends with the suffix, case sensitive, or + * @return {@code true} if the CharSequence ends with the suffix, case sensitive, or * both {@code null} * @since 2.4 */ - public static boolean endsWith(String str, String suffix) { + public static boolean endsWith(CharSequence str, CharSequence suffix) { return endsWith(str, suffix, false); } /** - *

Case insensitive check if a String ends with a specified suffix.

+ *

Case insensitive check if a CharSequence ends with a specified suffix.

* *

{@code null}s are handled without exceptions. Two {@code null} * references are considered to be equal. The comparison is case insensitive.

@@ -6249,28 +6249,28 @@ public class StringUtils { * * * @see java.lang.String#endsWith(String) - * @param str the String to check, may be null + * @param str the CharSequence to check, may be null * @param suffix the suffix to find, may be null - * @return {@code true} if the String ends with the suffix, case insensitive, or + * @return {@code true} if the CharSequence ends with the suffix, case insensitive, or * both {@code null} * @since 2.4 */ - public static boolean endsWithIgnoreCase(String str, String suffix) { + public static boolean endsWithIgnoreCase(CharSequence str, CharSequence suffix) { return endsWith(str, suffix, true); } /** - *

Check if a String ends with a specified suffix (optionally case insensitive).

+ *

Check if a CharSequence ends with a specified suffix (optionally case insensitive).

* * @see java.lang.String#endsWith(String) - * @param str the String to check, may be null + * @param str the CharSequence to check, may be null * @param suffix the suffix to find, may be null * @param ignoreCase inidicates whether the compare should ignore case * (case insensitive) or not. - * @return {@code true} if the String starts with the prefix or + * @return {@code true} if the CharSequence starts with the prefix or * both {@code null} */ - private static boolean endsWith(String str, String suffix, boolean ignoreCase) { + private static boolean endsWith(CharSequence str, CharSequence suffix, boolean ignoreCase) { if (str == null || suffix == null) { return str == null && suffix == null; } @@ -6278,7 +6278,7 @@ public class StringUtils { return false; } int strOffset = str.length() - suffix.length(); - return str.regionMatches(ignoreCase, strOffset, suffix, 0, suffix.length()); + return regionMatchesSequence(str, ignoreCase, strOffset, suffix, 0, suffix.length()); } /** @@ -6330,7 +6330,7 @@ public class StringUtils { } /** - *

Check if a String ends with any of an array of specified strings.

+ *

Check if a CharSequence ends with any of an array of specified strings.

* *
      * StringUtils.endsWithAny(null, null)      = false
@@ -6341,18 +6341,18 @@ public class StringUtils {
      * StringUtils.endsWithAny("abcxyz", new String[] {null, "xyz", "abc"}) = true
      * 
* - * @param string the String to check, may be null - * @param searchStrings the Strings to find, may be null or empty - * @return {@code true} if the String ends with any of the the prefixes, case insensitive, or + * @param string the CharSequence to check, may be null + * @param searchStrings the CharSequences to find, may be null or empty + * @return {@code true} if the CharSequence ends with any of the the prefixes, case insensitive, or * both {@code null} * @since 3.1 */ - public static boolean endsWithAny(String string, String... searchStrings) { + public static boolean endsWithAny(CharSequence string, CharSequence... searchStrings) { if (isEmpty(string) || ArrayUtils.isEmpty(searchStrings)) { return false; } for (int i = 0; i < searchStrings.length; i++) { - String searchString = searchStrings[i]; + CharSequence searchString = searchStrings[i]; if (StringUtils.endsWith(string, searchString)) { return true; } @@ -6491,4 +6491,15 @@ public class StringUtils { } } + static boolean regionMatchesSequence(CharSequence cs, boolean ignoreCase, int thisStart, + CharSequence substring, int start, int length) + { + if (cs instanceof String && substring instanceof String) { + return ((String) cs).regionMatches(ignoreCase, thisStart, ((String)substring), start, length); + } else { + // TODO: Implement rather than convert to String + return cs.toString().regionMatches(ignoreCase, thisStart, substring.toString(), start, length); + } + } + }