Changeset 66ee7b5 for core/java


Ignore:
Timestamp:
Mar 14, 2018 1:33:43 PM (2 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
6eb09bd
Parents:
9b3082b
Message:

Util: Add replace() method for StringBuilders?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/data/DataHelper.java

    r9b3082b r66ee7b5  
    616616    /**
    617617     *  Lower-case hex without leading zeros.
    618      *  Use toString(byte[] to get leading zeros
     618     *  Use toString(byte[]) to get leading zeros
    619619     *  @param data may be null (returns "00")
    620620     */
     
    20562056        }
    20572057    }
     2058
     2059    /**
     2060      * Replace all instances of "from" with "to" in the StringBuilder buf.
     2061      * Same as String.replace(), but in-memory with no object churn,
     2062      * as long as "to" is equal size or smaller than "from", or buf has capacity.
     2063      * Use for large Strings or for multiple replacements in a row.
     2064      *
     2065      * @param buf contains the string to be searched
     2066      * @param from the string to be replaced
     2067      * @param to the replacement string
     2068      * @since 0.9.34
     2069      */
     2070    public static void replace(StringBuilder buf, String from, String to) {
     2071        int oidx = 0;
     2072        while (oidx < buf.length()) {
     2073            int idx = buf.indexOf(from, oidx);
     2074            if (idx < 0)
     2075                break;
     2076            buf.replace(idx, idx + from.length(), to);
     2077            oidx = idx + to.length();
     2078        }
     2079    }
    20582080}
Note: See TracChangeset for help on using the changeset viewer.