From 25a4549ceb59148ff58329b068c4c71dc20ff269 Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Tue, 21 Oct 2003 20:24:22 +0000 Subject: [PATCH] Tweak deleteWhitespace to avoid creating a new String if possible git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137685 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang/StringUtils.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/java/org/apache/commons/lang/StringUtils.java b/src/java/org/apache/commons/lang/StringUtils.java index bc56a886f..c7fbd8fa2 100644 --- a/src/java/org/apache/commons/lang/StringUtils.java +++ b/src/java/org/apache/commons/lang/StringUtils.java @@ -145,7 +145,7 @@ import java.util.List; * @author Gary Gregory * @author Phil Steitz * @since 1.0 - * @version $Id: StringUtils.java,v 1.109 2003/09/07 14:32:34 psteitz Exp $ + * @version $Id: StringUtils.java,v 1.110 2003/10/21 20:24:22 scolebourne Exp $ */ public class StringUtils { // Performance testing notes (JDK 1.4, Jul03, scolebourne) @@ -2439,17 +2439,21 @@ public class StringUtils { * @return the String without whitespaces, null if null String input */ public static String deleteWhitespace(String str) { - if (str == null) { - return null; + if (str == null || str.length() == 0) { + return str; } int sz = str.length(); - StringBuffer buffer = new StringBuffer(sz); + char[] chs = new char[sz]; + int count = 0; for (int i = 0; i < sz; i++) { if (!Character.isWhitespace(str.charAt(i))) { - buffer.append(str.charAt(i)); + chs[count++] = str.charAt(i); } } - return buffer.toString(); + if (count == sz) { + return str; + } + return new String(chs, 0, count); } // Replacing