dlr         01/08/25 22:54:41

  Modified:    util/src/java/org/apache/commons/util StringUtils.java
  Log:
  replaceString() routines renamed to replace() and optimized.
  
  Revision  Changes    Path
  1.11      +44 -49    
jakarta-commons-sandbox/util/src/java/org/apache/commons/util/StringUtils.java
  
  Index: StringUtils.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons-sandbox/util/src/java/org/apache/commons/util/StringUtils.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -u -r1.10 -r1.11
  --- StringUtils.java  2001/08/26 04:48:22     1.10
  +++ StringUtils.java  2001/08/26 05:54:41     1.11
  @@ -71,13 +71,17 @@
   
   
   /**
  - * This is where common String manipulation routines should go.
  + * <p>Common <code>String</code> manipulation routines.</p>
    *
  + * <p>Originally from <a
  + * href="http://jakarta.apache.org/turbine/";>Turbine</a> and the
  + * GenerationJavaCore library.</p>
  + *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Jon S. Stevens</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Daniel Rall</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Greg Coladonato</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Bayard</a>
  - * @version $Id: StringUtils.java,v 1.10 2001/08/26 04:48:22 dlr Exp $
  + * @version $Id: StringUtils.java,v 1.11 2001/08/26 05:54:41 dlr Exp $
    */
   public class StringUtils
   {
  @@ -176,7 +180,6 @@
           return objectKey;
       }
   
  -
       /**
        * Remove Underscores from a string and replaces first
        * Letters with Capitals.  foo_bar becomes FooBar
  @@ -427,19 +430,6 @@
           return (wrappedLine.toString());
       }
   
  -
  -// from GenerationJavaCore library
  -/*
  - * A set of String library static methods. While extending String or 
  - * StringBuffer would have been the nicest solution, that is not 
  - * possible, so a simple set of static methods seems the most workable.
  - *
  - * Method ideas have so far been taken from the PHP4, Ruby and .NET languages.
  - *
  - * @author [EMAIL PROTECTED]
  - * @version 0.4 20010812
  - */
  -
       /**
        * Uncapitalise a string. That is, convert the first character into 
        * lower-case.
  @@ -467,55 +457,60 @@
       /**
        * Replace a string with another string inside a larger string, once.
        *
  -     * @param text String to do search and replace in
  -     * @param repl String to search for
  -     * @param with String to replace with
  -     *
  -     * @return String with once value replaced
  +     * @see #replace(String text, String repl, String with, int max)
        */
  -    static public String replaceStringOnce(String text, String repl, String with) {
  -        return replaceString(text, repl, with, 1);
  +    public static String replaceOnce(String text, String repl, String with)
  +    {
  +        return replace(text, repl, with, 1);
       }
   
       /**
  -     * Replace a string with another string inside a larger string, for
  -     * all of the search string.
  -     *
  -     * @param text String to do search and replace in
  -     * @param repl String to search for
  -     * @param with String to replace with
  -     *
  -     * @return String with all values replaced
  +     * @see #replace(String text, String repl, String with, int max)
        */
  -    static public String replaceString(String text, String repl, String with) {
  -        return replaceString(text, repl, with, -1);
  +    public static String replace(String text, String repl, String with)
  +    {
  +        return replace(text, repl, with, -1);
       }
  -    static public String replaceString(String text, String repl, String with, 
String n) {
  -        return replaceString(text, repl, with, NumberUtils.stringToInt(n,-1));
  +
  +    /**
  +     * @see #replace(String text, String repl, String with, int max)
  +     */
  +    public static String replace(String text, String repl, String with,
  +                                 String max)
  +    {
  +        return replace(text, repl, with, NumberUtils.stringToInt(max, -1));
       }
   
       /**
        * Replace a string with another string inside a larger string, for
        * the first n values of the search string.
        *
  -     * @param text String to do search and replace in
  +     * @param text Text to search and replace in.
        * @param repl String to search for
        * @param with String to replace with
  -     * @param n    int    values to replace
  -     *
  -     * @return String with n values replacEd
  +     * @param max Maximum number of values to replace, or
  +     * <code>-1</code> if no maximum.
  +     * @return The text with any replacements processed.
        */
  -    static public String replaceString(String text, String repl, String with, int 
n) {
  -        int idx = 0;
  -        while( (idx = text.indexOf(repl)) != -1) {
  -            text = text.substring(0,idx) + with + text.substring(idx+repl.length() 
);
  -            idx += with.length();    // jump beyond replacement
  -            n--;
  -            if(n == 0) {
  +    public static String replace(String text, String repl, String with,
  +                                 int max)
  +    {
  +        StringBuffer buf = new StringBuffer(text.length());
  +        int start = 0, end = 0;
  +        while ( (end = text.indexOf(repl, start)) != -1 )
  +        {
  +            //System.out.println("end=" + end);
  +            buf.append(text.substring(start, end)).append(with);
  +            start = end + repl.length();
  +            //System.out.println("new start=" + start);
  +
  +            if (--max == 0)
  +            {
                   break;
               }
           }
  -        return text;
  +        buf.append(text.substring(start));
  +        return buf.toString();
       }
   
       static public String overlayString(String text, String overlay, String start, 
String end) {
  @@ -1726,9 +1721,9 @@
           while(keys.hasNext()) {
               String key = keys.next().toString();
               String value = map.get(key).toString();
  -            text = replaceString(text, "${"+key+"}", value);
  +            text = replace(text, "${"+key+"}", value);
               if(key.indexOf(" ") == -1) {
  -                text = replaceString(text, "$"+key, value);
  +                text = replace(text, "$"+key, value);
               }
           }
           return text;
  
  
  

Reply via email to