You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by ge...@apache.org on 2002/02/20 12:35:11 UTC

cvs commit: jakarta-velocity/src/java/org/apache/velocity/util StringUtils.java

geirm       02/02/20 03:35:11

  Modified:    src/java/org/apache/velocity/util StringUtils.java
  Log:
  Fix for bug # 5920.
  
  I basically rewrote it, and would really appreciate someone who uses this
  to look over the changes.  I think it's right, but I added some
  semantics which were undefined, namely it returns the input if the
  input string is null, the count is 0, or the EOL is null.
  
  Revision  Changes    Path
  1.16      +33 -22    jakarta-velocity/src/java/org/apache/velocity/util/StringUtils.java
  
  Index: StringUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/util/StringUtils.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- StringUtils.java	22 Oct 2001 03:53:27 -0000	1.15
  +++ StringUtils.java	20 Feb 2002 11:35:11 -0000	1.16
  @@ -65,6 +65,8 @@
   import java.util.Hashtable;
   import java.util.List;
   import java.util.StringTokenizer;
  +import java.util.Map;
  +
   
   /**
    * This class provides some methods for dynamically
  @@ -75,7 +77,7 @@
    *
    *  @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
    *  @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
  - *  @version $Id: StringUtils.java,v 1.15 2001/10/22 03:53:27 jon Exp $
  + *  @version $Id: StringUtils.java,v 1.16 2002/02/20 11:35:11 geirm Exp $
    */
   public class StringUtils
   {
  @@ -290,34 +292,43 @@
        * @return String with processed answer.
        */
       public static String chop(String s, int i, String eol)
  -    {        
  -        char[] sa = s.toCharArray();
  -        int length = sa.length;
  +    {
  +        if ( i == 0 || s == null || eol == null )
  +        {
  +           return s;
  +        }
   
  -        if ( eol.length() == 2 ) 
  +        int length = s.length();
  +
  +        /*
  +         * if it is a 2 char EOL and the string ends with
  +         * it, nip it off.  The EOL in this case is treated like 1 character
  +         */
  +        if ( eol.length() == 2 && s.endsWith(eol ))
           {
  -            char eol1 = eol.charAt(0);
  -            char eol2 = eol.charAt(1);
  -            for (; i>0; i--)
  -            {
  -                if ( sa[length-1] == eol2 && sa[length-2] == eol1 ) 
  -                {
  -                    length -= 2;
  -                }
  -                else 
  -                {
  -                    length--;
  -                }
  -            }
  +            length -= 2;
  +            i -= 1;
           }
  -        else
  +
  +        if ( i > 0)
           {
               length -= i;
           }
   
  -        return new String(sa, 0, length);
  +        if ( length < 0)
  +        {
  +            length = 0;
  +        }
  +
  +        return s.substring( 0, length);
       }
  -    
  +
  +    public static StringBuffer stringSubstitution( String argStr,
  +                                                   Hashtable vars )
  +    {
  +        return stringSubstitution( argStr, (Map) vars );
  +    }
  +
       /**
        * Perform a series of substitutions. The substitions
        * are performed by replacing $variable in the target
  @@ -329,7 +340,7 @@
        * @return String target string with replacements.
        */
       public static StringBuffer stringSubstitution(String argStr,
  -            Hashtable vars)
  +            Map vars)
       {
           StringBuffer argBuf = new StringBuffer();
   
  
  
  

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