| Index: editor/tools/plugins/com.google.dart.tools.core/src/com/google/dart/tools/core/utilities/general/StringUtilities.java
|
| diff --git a/editor/tools/plugins/com.google.dart.tools.core/src/com/google/dart/tools/core/utilities/general/StringUtilities.java b/editor/tools/plugins/com.google.dart.tools.core/src/com/google/dart/tools/core/utilities/general/StringUtilities.java
|
| index c375aba9ebcda905dcddd77a59c56b521e468673..37f1c19086f27dbf64b284d2a97930ea26f7c291 100644
|
| --- a/editor/tools/plugins/com.google.dart.tools.core/src/com/google/dart/tools/core/utilities/general/StringUtilities.java
|
| +++ b/editor/tools/plugins/com.google.dart.tools.core/src/com/google/dart/tools/core/utilities/general/StringUtilities.java
|
| @@ -16,6 +16,46 @@ package com.google.dart.tools.core.utilities.general;
|
| public final class StringUtilities {
|
|
|
| /**
|
| + * The empty String <code>""</code>.
|
| + */
|
| + public static final String EMPTY = "";
|
| +
|
| + /**
|
| + * <p>
|
| + * Counts how many times the substring appears in the larger String.
|
| + * </p>
|
| + * <p>
|
| + * A <code>null</code> or empty ("") String input returns <code>0</code>.
|
| + * </p>
|
| + *
|
| + * <pre>
|
| + * StringUtils.countMatches(null, *) = 0
|
| + * StringUtils.countMatches("", *) = 0
|
| + * StringUtils.countMatches("abba", null) = 0
|
| + * StringUtils.countMatches("abba", "") = 0
|
| + * StringUtils.countMatches("abba", "a") = 2
|
| + * StringUtils.countMatches("abba", "ab") = 1
|
| + * StringUtils.countMatches("abba", "xxx") = 0
|
| + * </pre>
|
| + *
|
| + * @param str the String to check, may be null
|
| + * @param sub the substring to count, may be null
|
| + * @return the number of occurrences, 0 if either String is <code>null</code>
|
| + */
|
| + public static int countMatches(String str, String sub) {
|
| + if (isEmpty(str) || isEmpty(sub)) {
|
| + return 0;
|
| + }
|
| + int count = 0;
|
| + int idx = 0;
|
| + while ((idx = str.indexOf(sub, idx)) != -1) {
|
| + count++;
|
| + idx += sub.length();
|
| + }
|
| + return count;
|
| + }
|
| +
|
| + /**
|
| * This method is equivalent to {@link String#endsWith(String)} with the exception that
|
| * {@link String#equalsIgnoreCase(String)} is used, instead of {@link String#equals(Object)}.
|
| *
|
| @@ -43,6 +83,265 @@ public final class StringUtilities {
|
| return strSuffix.equalsIgnoreCase(suffix);
|
| }
|
|
|
| + /**
|
| + * <p>
|
| + * Checks if a String is whitespace, empty ("") or null.
|
| + * </p>
|
| + *
|
| + * <pre>
|
| + * StringUtils.isBlank(null) = true
|
| + * StringUtils.isBlank("") = true
|
| + * StringUtils.isBlank(" ") = true
|
| + * StringUtils.isBlank("bob") = false
|
| + * StringUtils.isBlank(" bob ") = false
|
| + * </pre>
|
| + *
|
| + * @param str the String to check, may be null
|
| + * @return <code>true</code> if the String is null, empty or whitespace
|
| + */
|
| + public static boolean isBlank(String str) {
|
| + int strLen;
|
| + if (str == null || (strLen = str.length()) == 0) {
|
| + return true;
|
| + }
|
| + for (int i = 0; i < strLen; i++) {
|
| + if (Character.isWhitespace(str.charAt(i)) == false) {
|
| + return false;
|
| + }
|
| + }
|
| + return true;
|
| + }
|
| +
|
| + /**
|
| + * <p>
|
| + * Checks if a String is empty ("") or null.
|
| + * </p>
|
| + *
|
| + * <pre>
|
| + * StringUtils.isEmpty(null) = true
|
| + * StringUtils.isEmpty("") = true
|
| + * StringUtils.isEmpty(" ") = false
|
| + * StringUtils.isEmpty("bob") = false
|
| + * StringUtils.isEmpty(" bob ") = false
|
| + * </pre>
|
| + *
|
| + * @param str the String to check, may be null
|
| + * @return <code>true</code> if the String is empty or null
|
| + */
|
| + public static boolean isEmpty(String str) {
|
| + return str == null || str.length() == 0;
|
| + }
|
| +
|
| + /**
|
| + * <p>
|
| + * Checks if a String is not empty (""), not null and not whitespace only.
|
| + * </p>
|
| + *
|
| + * <pre>
|
| + * StringUtils.isNotBlank(null) = false
|
| + * StringUtils.isNotBlank("") = false
|
| + * StringUtils.isNotBlank(" ") = false
|
| + * StringUtils.isNotBlank("bob") = true
|
| + * StringUtils.isNotBlank(" bob ") = true
|
| + * </pre>
|
| + *
|
| + * @param str the String to check, may be null
|
| + * @return <code>true</code> if the String is not empty and not null and not whitespace
|
| + */
|
| + public static boolean isNotBlank(String str) {
|
| + return !isBlank(str);
|
| + }
|
| +
|
| + /**
|
| + * <p>
|
| + * Checks if a String is not empty ("") and not null.
|
| + * </p>
|
| + *
|
| + * <pre>
|
| + * StringUtils.isNotEmpty(null) = false
|
| + * StringUtils.isNotEmpty("") = false
|
| + * StringUtils.isNotEmpty(" ") = true
|
| + * StringUtils.isNotEmpty("bob") = true
|
| + * StringUtils.isNotEmpty(" bob ") = true
|
| + * </pre>
|
| + *
|
| + * @param str the String to check, may be null
|
| + * @return <code>true</code> if the String is not empty and not null
|
| + */
|
| + public static boolean isNotEmpty(String str) {
|
| + return !isEmpty(str);
|
| + }
|
| +
|
| + /**
|
| + * <p>
|
| + * Gets the substring after the first occurrence of a separator. The separator is not returned.
|
| + * </p>
|
| + * <p>
|
| + * A <code>null</code> string input will return <code>null</code>. An empty ("") string input will
|
| + * return the empty string. A <code>null</code> separator will return the empty string if the
|
| + * input string is not <code>null</code>.
|
| + * </p>
|
| + * <p>
|
| + * If nothing is found, the empty string is returned.
|
| + * </p>
|
| + *
|
| + * <pre>
|
| + * StringUtils.substringAfter(null, *) = null
|
| + * StringUtils.substringAfter("", *) = ""
|
| + * StringUtils.substringAfter(*, null) = ""
|
| + * StringUtils.substringAfter("abc", "a") = "bc"
|
| + * StringUtils.substringAfter("abcba", "b") = "cba"
|
| + * StringUtils.substringAfter("abc", "c") = ""
|
| + * StringUtils.substringAfter("abc", "d") = ""
|
| + * StringUtils.substringAfter("abc", "") = "abc"
|
| + * </pre>
|
| + *
|
| + * @param str the String to get a substring from, may be null
|
| + * @param separator the String to search for, may be null
|
| + * @return the substring after the first occurrence of the separator, <code>null</code> if null
|
| + * String input
|
| + * @since 2.0
|
| + */
|
| + public static String substringAfter(String str, String separator) {
|
| + if (isEmpty(str)) {
|
| + return str;
|
| + }
|
| + if (separator == null) {
|
| + return EMPTY;
|
| + }
|
| + int pos = str.indexOf(separator);
|
| + if (pos == -1) {
|
| + return EMPTY;
|
| + }
|
| + return str.substring(pos + separator.length());
|
| + }
|
| +
|
| + /**
|
| + * <p>
|
| + * Gets the substring after the last occurrence of a separator. The separator is not returned.
|
| + * </p>
|
| + * <p>
|
| + * A <code>null</code> string input will return <code>null</code>. An empty ("") string input will
|
| + * return the empty string. An empty or <code>null</code> separator will return the empty string
|
| + * if the input string is not <code>null</code>.
|
| + * </p>
|
| + * <p>
|
| + * If nothing is found, the empty string is returned.
|
| + * </p>
|
| + *
|
| + * <pre>
|
| + * StringUtils.substringAfterLast(null, *) = null
|
| + * StringUtils.substringAfterLast("", *) = ""
|
| + * StringUtils.substringAfterLast(*, "") = ""
|
| + * StringUtils.substringAfterLast(*, null) = ""
|
| + * StringUtils.substringAfterLast("abc", "a") = "bc"
|
| + * StringUtils.substringAfterLast("abcba", "b") = "a"
|
| + * StringUtils.substringAfterLast("abc", "c") = ""
|
| + * StringUtils.substringAfterLast("a", "a") = ""
|
| + * StringUtils.substringAfterLast("a", "z") = ""
|
| + * </pre>
|
| + *
|
| + * @param str the String to get a substring from, may be null
|
| + * @param separator the String to search for, may be null
|
| + * @return the substring after the last occurrence of the separator, <code>null</code> if null
|
| + * String input
|
| + */
|
| + public static String substringAfterLast(String str, String separator) {
|
| + if (isEmpty(str)) {
|
| + return str;
|
| + }
|
| + if (isEmpty(separator)) {
|
| + return EMPTY;
|
| + }
|
| + int pos = str.lastIndexOf(separator);
|
| + if (pos == -1 || pos == str.length() - separator.length()) {
|
| + return EMPTY;
|
| + }
|
| + return str.substring(pos + separator.length());
|
| + }
|
| +
|
| + /**
|
| + * <p>
|
| + * Gets the substring before the first occurrence of a separator. The separator is not returned.
|
| + * </p>
|
| + * <p>
|
| + * A <code>null</code> string input will return <code>null</code>. An empty ("") string input will
|
| + * return the empty string. A <code>null</code> separator will return the input string.
|
| + * </p>
|
| + * <p>
|
| + * If nothing is found, the string input is returned.
|
| + * </p>
|
| + *
|
| + * <pre>
|
| + * StringUtils.substringBefore(null, *) = null
|
| + * StringUtils.substringBefore("", *) = ""
|
| + * StringUtils.substringBefore("abc", "a") = ""
|
| + * StringUtils.substringBefore("abcba", "b") = "a"
|
| + * StringUtils.substringBefore("abc", "c") = "ab"
|
| + * StringUtils.substringBefore("abc", "d") = "abc"
|
| + * StringUtils.substringBefore("abc", "") = ""
|
| + * StringUtils.substringBefore("abc", null) = "abc"
|
| + * </pre>
|
| + *
|
| + * @param str the String to get a substring from, may be null
|
| + * @param separator the String to search for, may be null
|
| + * @return the substring before the first occurrence of the separator, <code>null</code> if null
|
| + * String input
|
| + * @since 2.0
|
| + */
|
| + public static String substringBefore(String str, String separator) {
|
| + if (isEmpty(str) || separator == null) {
|
| + return str;
|
| + }
|
| + if (separator.length() == 0) {
|
| + return EMPTY;
|
| + }
|
| + int pos = str.indexOf(separator);
|
| + if (pos == -1) {
|
| + return str;
|
| + }
|
| + return str.substring(0, pos);
|
| + }
|
| +
|
| + /**
|
| + * <p>
|
| + * Gets the substring before the last occurrence of a separator. The separator is not returned.
|
| + * </p>
|
| + * <p>
|
| + * A <code>null</code> string input will return <code>null</code>. An empty ("") string input will
|
| + * return the empty string. An empty or <code>null</code> separator will return the input string.
|
| + * </p>
|
| + * <p>
|
| + * If nothing is found, the string input is returned.
|
| + * </p>
|
| + *
|
| + * <pre>
|
| + * StringUtils.substringBeforeLast(null, *) = null
|
| + * StringUtils.substringBeforeLast("", *) = ""
|
| + * StringUtils.substringBeforeLast("abcba", "b") = "abc"
|
| + * StringUtils.substringBeforeLast("abc", "c") = "ab"
|
| + * StringUtils.substringBeforeLast("a", "a") = ""
|
| + * StringUtils.substringBeforeLast("a", "z") = "a"
|
| + * StringUtils.substringBeforeLast("a", null) = "a"
|
| + * StringUtils.substringBeforeLast("a", "") = "a"
|
| + * </pre>
|
| + *
|
| + * @param str the String to get a substring from, may be null
|
| + * @param separator the String to search for, may be null
|
| + * @return the substring before the last occurrence of the separator, <code>null</code> if null
|
| + * String input
|
| + */
|
| + public static String substringBeforeLast(String str, String separator) {
|
| + if (isEmpty(str) || isEmpty(separator)) {
|
| + return str;
|
| + }
|
| + int pos = str.lastIndexOf(separator);
|
| + if (pos == -1) {
|
| + return str;
|
| + }
|
| + return str.substring(0, pos);
|
| + }
|
| +
|
| private StringUtilities() {
|
|
|
| }
|
|
|