commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang StringUtils.java
Date Tue, 21 Oct 2003 20:24:22 GMT
scolebourne    2003/10/21 13:24:22

  Modified:    lang/src/java/org/apache/commons/lang StringUtils.java
  Log:
  Tweak deleteWhitespace to avoid creating a new String if possible
  
  Revision  Changes    Path
  1.110     +10 -6     jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java
  
  Index: StringUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java,v
  retrieving revision 1.109
  retrieving revision 1.110
  diff -u -r1.109 -r1.110
  --- StringUtils.java	7 Sep 2003 14:32:34 -0000	1.109
  +++ StringUtils.java	21 Oct 2003 20:24:22 -0000	1.110
  @@ -2439,17 +2439,21 @@
        * @return the String without whitespaces, <code>null</code> 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
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message