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 Tue, 10 Sep 2002 00:39:40 GMT
bayard      2002/09/09 17:39:40

  Modified:    string/src/org/apache/taglibs/string/util StringW.java
  Log:
  Patched the truncate method from Tim Kettering with his latest fix.
  "I was incorrectly checking on the lower, instead of upper limit
  before entering the block that would do a substring, so if a string was
  longer than the lower limit, but not long enough for the upper and there
  were no spaces after the lower limit, the code would attempt to substring to
  the upper, subsequently throwing an out of range exception. "
  
  Submitted by:	Tim Kettering <timster@mac.com>
  
  Revision  Changes    Path
  1.14      +37 -39    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.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- StringW.java	15 Aug 2002 07:23:03 -0000	1.13
  +++ StringW.java	10 Sep 2002 00:39:40 -0000	1.14
  @@ -279,40 +279,38 @@
        *
        * @author timster@mac.com
        */
  -    public static String truncateNicely(String str, int lower, int upper, String appendToEnd)
  -    {
  -        // strip markup from the string
  -        str = XmlW.removeXml(str);
  -
  -        // now determine if the string fits within the lower limit
  -        // if it does, go straight to return, do not pass 'go' and collect $200
  -        if(str.length() >= lower) {
  -            // the magic location int
  -            int loc;
  -        
  -            // first we determine where the next space appears after lower
  -            loc = str.indexOf(' ', lower);
  -            
  -            // if the location is -1, we'll set it to the upper limit, 
  -            // but if upper limit is also -1, we skip any work and 
  -            // return the entire string
  -            if( !(upper == -1 && loc == -1) ) {
  -                // a quick check to adjust the upper if its set wrong
  -                if(upper < lower) {
  -                    upper = lower;
  -                }
  -            
  -                if(loc == -1) {
  -                    str = str.substring(0, upper);
  -                } else {
  -                    str = str.substring(0, loc);
  -                }
  -                
  -                // the string was truncated, so we append the appendToEnd String
  -                str = str + appendToEnd;
  -            }
  -        }
  -        
  -        return str;
  -    }
  +	public static String truncateNicely(String str, int lower, int upper, String appendToEnd)
  +	{
  +		// strip markup from the string
  +		str = XmlW.removeXml(str);
  +		
  +		// quickly adjust the upper if it is set lower than 'lower'
  +		if(upper < lower) {
  +			upper = lower;
  +		}		
  +		
  +		// now determine if the string fits within the upper limit
  +		// if it does, go straight to return, do not pass 'go' and collect $200
  +		if(str.length() > upper) {
  +			// the magic location int
  +			int loc;
  +		
  +			// first we determine where the next space appears after lower
  +			loc = str.lastIndexOf(' ', upper);
  +			
  +			// now we'll see if the location is greater than the lower limit
  +			if(loc >= lower) {
  +				// yes it was, so we'll cut it off here
  +				str = str.substring(0, loc);
  +			} else {
  +				// no it wasnt, so we'll cut it off at the upper limit
  +				str = str.substring(0, upper);
  +			}
  +	
  +			// the string was truncated, so we append the appendToEnd String				
  +			str = str + appendToEnd;
  +		}
  +		
  +		return str;
  +	}
   }
  
  
  

--
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