You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2021/02/27 12:53:45 UTC
[httpcomponents-core] branch master updated: improve Text Utils
methods
This is an automated email from the ASF dual-hosted git repository.
olegk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git
The following commit(s) were added to refs/heads/master by this push:
new be8a8a4 improve Text Utils methods
be8a8a4 is described below
commit be8a8a4aac24bd96d8cc01e8d7723034c88f9cac
Author: Arturo Bernal <ar...@gmail.com>
AuthorDate: Sat Feb 27 13:09:59 2021 +0100
improve Text Utils methods
---
.../java/org/apache/hc/core5/util/TextUtils.java | 42 +++++++++++++++++-----
1 file changed, 34 insertions(+), 8 deletions(-)
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/util/TextUtils.java b/httpcore5/src/main/java/org/apache/hc/core5/util/TextUtils.java
index 645783d..4d9ebe9 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/util/TextUtils.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/util/TextUtils.java
@@ -40,20 +40,31 @@ public final class TextUtils {
* Returns true if the parameter is null or of zero length
*/
public static boolean isEmpty(final CharSequence s) {
- if (s == null) {
- return true;
- }
- return s.length() == 0;
+ return length(s) == 0;
}
/**
- * Returns true if the parameter is null or contains only whitespace
+ * <p>Checks if a CharSequence is empty (""), null or whitespace only.</p>
+ *
+ * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
+ *
+ * <pre>
+ * TextUtils.isBlank(null) = true
+ * TextUtils.isBlank("") = true
+ * TextUtils.isBlank(" ") = true
+ * TextUtils.isBlank("abg") = false
+ * TextUtils.isBlank(" abg ") = false
+ * </pre>
+ *
+ * @param s the CharSequence to check, may be null
+ * @return {@code true} if the CharSequence is null, empty or whitespace only
*/
public static boolean isBlank(final CharSequence s) {
- if (s == null) {
+ final int strLen = length(s);
+ if (strLen == 0) {
return true;
}
- for (int i = 0; i < s.length(); i++) {
+ for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(s.charAt(i))) {
return false;
}
@@ -62,10 +73,25 @@ public final class TextUtils {
}
/**
+ * Gets a CharSequence length or {@code 0} if the CharSequence is
+ * {@code null}.
+ *
+ * @param cs
+ * a CharSequence or {@code null}
+ * @return CharSequence length or {@code 0} if the CharSequence is
+ * {@code null}.
+ * @since 5.1
+ */
+ public static int length(final CharSequence cs) {
+ return cs == null ? 0 : cs.length();
+ }
+
+ /**
* @since 4.4
*/
public static boolean containsBlanks(final CharSequence s) {
- if (s == null) {
+ final int strLen = length(s);
+ if (strLen == 0) {
return false;
}
for (int i = 0; i < s.length(); i++) {