commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jon Skeet" <jon.sk...@peramon.com>
Subject [Lang] Faster version of StringUtils.replace?
Date Mon, 07 Oct 2002 11:13:34 GMT
I've just been looking at the implementation of StringUtils.replace, and
I'd like to donate my own implementation, if folks are interested in it.
It's a bit faster (at least, it was in my small test program), at the
potential cost of a bit more memory. In its original form it doesn't
have *quite* the same interface, but it should be very easy to fix that,
and I'd be happy to do that if people would like it.

Here's the code:

    /**
     * Returns a String with all occurrences of <code>from</code>
     * within <code>orig</code> replaced with <code>to</code>. If
     * <code>orig</code> contains no occurrences of
     * <code>from</code>, or if <code>from</code> is equal to
     * <code>to</code>, <code>orig</code> itself is returned rather
     * than a copy being made. None of the parameters should be
     * null.
     * 
     * @param orig the original String. Must not be null.
     * @param from the String to replace within <code>orig</code>.
     * Must not be null.
     * @param to the String to replace <code>from</code> with. Must
     * not be null.
     * 
     * @return a version of <code>orig</code> with all occurrences
     * of <code>from</code> being replaced with <code>to</code>.
     * 
     * @throws IllegalArgumentException if <code>from</code> is empty
     */
    public static String replace (String orig, String from, String to)
    {
        int fromLength = from.length();
        
        if (fromLength==0)
            throw new IllegalArgumentException 
            ("String to be replaced must not be empty");
        
        int start = orig.indexOf (from);
        if (start==-1)
            return orig;
        
        boolean greaterLength = (to.length() >= fromLength);
        
        StringBuffer buffer;
        // If the "to" parameter is longer than (or
        // as long as) "from", the final length will
        // be at least as large
        if (greaterLength)
        {
            if (from.equals (to))
                return orig;
            buffer = new StringBuffer(orig.length());
        }
        else
        {
            buffer = new StringBuffer();
        }
        
        char [] origChars = orig.toCharArray();
        
        int copyFrom=0;
        while (start != -1)
        {
            buffer.append (origChars, copyFrom, start-copyFrom);
            buffer.append (to);
            copyFrom=start+fromLength;
            start = orig.indexOf (from, copyFrom);
        }
        buffer.append (origChars, copyFrom, origChars.length-copyFrom);
        
        return buffer.toString();
    }

Let me know (on or off list) if you're interested in using the code.
There may well be further tweaks which could be useful and which I'd be
happy to investigate if the code is considered.

Thanks,
Jon Skeet

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


Mime
View raw message