You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-dev@jakarta.apache.org by ba...@apache.org on 2002/06/07 22:54:30 UTC

cvs commit: jakarta-taglibs/string/src/org/apache/taglibs/string/util StringW.java

bayard      2002/06/07 13:54:29

  Modified:    string/src/org/apache/taglibs/string/util StringW.java
  Log:
  Fixed an error in the stripStart and stripEnd methods. They would happily
  run off the end of the String if the String was mostly made up of the
  delimiter.
  
  Added a stripAll method which allows all elements in an array to be stripped.
  
  Revision  Changes    Path
  1.10      +44 -12    jakarta-taglibs/string/src/org/apache/taglibs/string/util/StringW.java
  
  Index: StringW.java
  ===================================================================
  RCS file: /home/cvs/jakarta-taglibs/string/src/org/apache/taglibs/string/util/StringW.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- StringW.java	23 Apr 2002 13:36:37 -0000	1.9
  +++ StringW.java	7 Jun 2002 20:54:29 -0000	1.10
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-taglibs/string/src/org/apache/taglibs/string/util/StringW.java,v 1.9 2002/04/23 13:36:37 bayard Exp $
  - * $Revision: 1.9 $
  - * $Date: 2002/04/23 13:36:37 $
  + * $Header: /home/cvs/jakarta-taglibs/string/src/org/apache/taglibs/string/util/StringW.java,v 1.10 2002/06/07 20:54:29 bayard Exp $
  + * $Revision: 1.10 $
  + * $Date: 2002/06/07 20:54:29 $
    *
    * ====================================================================
    *
  @@ -805,6 +805,30 @@
       }
   
       /**
  +     * Strip whitespace from the front and back of every string
  +     * in the array.
  +     */
  +    static public String[] stripAll(String[] strs) {
  +        return stripAll(strs, null);
  +    }
  + 
  +    /**
  +     * Strip the specified delimiter from the front and back of
  +     * every String in the array.
  +     */
  +    static public String[] stripAll(String[] strs, String delimiter) {
  +        if( (strs == null) || (strs.length == 0) ) {
  +            return strs;
  +        }
  +        int sz = strs.length;
  +        String[] newArr = new String[sz];
  +        for(int i=0; i<sz; i++) {
  +            newArr[i] = strip(strs[i], delimiter);
  +        }
  +        return newArr;
  +    }     
  +
  +    /**
        * Swaps the case of String. Properly looks after 
        * making sure the start of words are Titlecase and not 
        * Uppercase.
  @@ -878,38 +902,46 @@
        * Strip any of a supplied substring from the end of a String..
        */
       static public String stripEnd(String str, String ch) {
  +        if(str == null) {
  +            return null;
  +        }
           int end = str.length();
  -
  + 
           if(ch == null) {
  -            while( Character.isWhitespace( str.charAt(end-1) ) ) {
  -                end--;
  +            while( (end != 0) && Character.isWhitespace( str.charAt(end-1) ) ) {                end--;
               }
           } else {
               char chr = ch.charAt(0);
  -            while( str.charAt(end-1) == chr ) {
  +            while( (end != 0) && (str.charAt(end-1) == chr) ) {
                   end--;
               }
           }
  -        return str.substring(0, end);
  +        return str.substring(0, end); 
       }
   
       /**
        * Strip any of a supplied substring from the start of a String..
        */
       static public String stripStart(String str, String ch) {
  +        if(str == null) {
  +            return null;
  +        }
  + 
           int start = 0;
  -
  + 
  +        int sz = str.length();
  + 
           if(ch == null) {
  -            while( Character.isWhitespace( str.charAt(start) ) ) {
  +            while( (start != sz) && Character.isWhitespace( str.charAt(start) )) {
                   start++;
               }
           } else {
               char chr = ch.charAt(0);
  -            while( str.charAt(start) == chr ) {
  +            while( (start != sz) && (str.charAt(start) == chr ) ) {
                   start++;
               }
           }
  -        return str.substring(start);
  +        return str.substring(start);     
       }
   
       /**
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>