jakarta-taglibs-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject cvs commit: jakarta-taglibs/string/src/org/apache/taglibs/string/util StringW.java
Date Thu, 14 Feb 2002 05:11:46 GMT
bayard      02/02/13 21:11:46

  Modified:    string/src/org/apache/taglibs/string/util StringW.java
  Log:
  Added protection into substring so that it doesnt complain when end
  indexes larger than the string are passed in.
  Added ability to pass -ve indexes in, meaning distance from the end of
  the string.
  
  Revision  Changes    Path
  1.7       +51 -11    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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- StringW.java	4 Feb 2002 17:07:37 -0000	1.6
  +++ StringW.java	14 Feb 2002 05:11:46 -0000	1.7
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-taglibs/string/src/org/apache/taglibs/string/util/StringW.java,v
1.6 2002/02/04 17:07:37 bayard Exp $
  - * $Revision: 1.6 $
  - * $Date: 2002/02/04 17:07:37 $
  + * $Header: /home/cvs/jakarta-taglibs/string/src/org/apache/taglibs/string/util/StringW.java,v
1.7 2002/02/14 05:11:46 bayard Exp $
  + * $Revision: 1.7 $
  + * $Date: 2002/02/14 05:11:46 $
    *
    * ====================================================================
    *
  @@ -1368,21 +1368,61 @@
       }
   
       static public String substring(String str, String start) {
  -        return substring(str, start, "-1");
  +        return substring(str, NumberW.stringToInt(start));
       }
       static public String substring(String str, int start) {
  -        return substring(str, start, -1);
  +        if(str == null) {
  +            return null;
  +        }
  +
  +        // handle negatives
  +        if(start < 0) {
  +            start = str.length() + start;    // remember start is negative
  +        }
  +
  +        if(start < 0) {
  +            start = 0;
  +        }
  +
  +        return str.substring(start);
       }
       static public String substring(String str, String start, String end) {
  -        return substring(str, NumberW.stringToInt(start), NumberW.stringToInt(end));
  +        return substring(str, NumberW.stringToInt(start), NumberW.stringToInt(end
  +));
       }
       static public String substring(String str, int start, int end) {
  -        if(end == -1) {
  -            return str.substring(start);
  -        } else {
  -            return str.substring(start, end);
  +        if(str == null) {
  +            return null;
           }
  -    }
  +
  +        // handle negatives
  +        if(end < 0) {
  +            end = str.length() + end;    // remember end is negative
  +        }
  +        if(start < 0) {
  +            start = str.length() + start;    // remember start is negative
  +        }
  +
  +        // check length next
  +        if(end > str.length()) {
  +            // check this works.
  +            end = str.length();
  +        }
  +
  +        // what if start is greater than end??
  +
  +        if(start < 0) {
  +            start = 0;
  +        }
  +
  +        // a good default?
  +        if(end < 0) {
  +            end = 0;
  +        }
  +
  +        return str.substring(start, end);
  +
  +    }    
   
       static public String random(int count) {
           return random(count, false, false);
  
  
  

--
To unsubscribe, e-mail:   <mailto:taglibs-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:taglibs-dev-help@jakarta.apache.org>


Mime
View raw message