001package io.ebean.enhance.ant;
002
003
004/**
005 * Utility String class that supports String manipulation functions.
006 */
007public class StringReplace {
008
009
010        /**
011         * This method takes a String and will replace all occurrences of the match
012         * String with that of the replace String.
013         * 
014         * @param source
015         *            the source string
016         * @param match
017         *            the string used to find a match
018         * @param replace
019         *            the string used to replace match with
020         * @return the source string after the search and replace
021         */
022        public static String replace(String source, String match, String replace) {
023                return replaceString(source, match, replace, 30, 0, source.length());
024        }
025
026        /**
027         * Additionally specify the additionalSize to add to the buffer. This will
028         * make the buffer bigger so that it doesn't have to grow when replacement
029         * occurs.
030         */
031        private static String replaceString(String source, String match, String replace,
032                        int additionalSize, int startPos, int endPos) {
033
034                char match0 = match.charAt(0);
035                
036                int matchLength = match.length();
037
038                if (matchLength == 1 && replace.length() == 1) {
039                        char replace0 = replace.charAt(0);
040                        return source.replace(match0, replace0);
041                }
042                if (matchLength >= replace.length()) {
043                        additionalSize = 0;
044                }
045
046
047                int sourceLength = source.length();
048                int lastMatch = endPos - matchLength;
049
050                StringBuilder sb = new StringBuilder(sourceLength + additionalSize);
051
052                if (startPos > 0) {
053                        sb.append(source.substring(0, startPos));
054                }
055
056                char sourceChar;
057                boolean isMatch;
058                int sourceMatchPos;
059
060                for (int i = startPos; i < sourceLength; i++) {
061                        sourceChar = source.charAt(i);
062                        if (i > lastMatch || sourceChar != match0) {
063                                sb.append(sourceChar);
064
065                        } else {
066                                // check to see if this is a match
067                                isMatch = true;
068                                sourceMatchPos = i;
069                                
070                                // check each following character...
071                                for (int j = 1; j < matchLength; j++) {
072                                        sourceMatchPos++;
073                                        if (source.charAt(sourceMatchPos) != match.charAt(j)) {
074                                                isMatch = false;
075                                                break;
076                                        }
077                                }
078                                if (isMatch) {
079                                        i = i + matchLength - 1;
080                                        sb.append(replace);
081                                } else {
082                                        // was not a match
083                                        sb.append(sourceChar);
084                                }
085                        }
086                }
087
088                return sb.toString();
089        }
090
091
092}