< prev index next >

src/java.base/share/classes/java/lang/String.java

Print this page
rev 12026 : [mq]: 8058779-Faster-implementation-of-String-replace-HEAVYDUTY


2218      * @param   regex
2219      *          the regular expression to which this string is to be matched
2220      * @param   replacement
2221      *          the string to be substituted for each match
2222      *
2223      * @return  The resulting {@code String}
2224      *
2225      * @throws  PatternSyntaxException
2226      *          if the regular expression's syntax is invalid
2227      *
2228      * @see java.util.regex.Pattern
2229      *
2230      * @since 1.4
2231      * @spec JSR-51
2232      */
2233     public String replaceAll(String regex, String replacement) {
2234         return Pattern.compile(regex).matcher(this).replaceAll(replacement);
2235     }
2236 
2237     /**








2238      * Replaces each substring of this string that matches the literal target
2239      * sequence with the specified literal replacement sequence. The
2240      * replacement proceeds from the beginning of the string to the end, for
2241      * example, replacing "aa" with "b" in the string "aaa" will result in
2242      * "ba" rather than "ab".
2243      *
2244      * @param  target The sequence of char values to be replaced
2245      * @param  replacement The replacement sequence of char values
2246      * @return  The resulting string
2247      * @since 1.5
2248      */
2249     public String replace(CharSequence target, CharSequence replacement) {
2250         return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
2251                 this).replaceAll(Matcher.quoteReplacement(replacement.toString()));










































































































2252     }
2253 
2254     /**
2255      * Splits this string around matches of the given
2256      * <a href="../util/regex/Pattern.html#sum">regular expression</a>.
2257      *
2258      * <p> The array returned by this method contains each substring of this
2259      * string that is terminated by another substring that matches the given
2260      * expression or is terminated by the end of the string.  The substrings in
2261      * the array are in the order in which they occur in this string.  If the
2262      * expression does not match any part of the input then the resulting array
2263      * has just one element, namely this string.
2264      *
2265      * <p> When there is a positive-width match at the beginning of this
2266      * string then an empty leading substring is included at the beginning
2267      * of the resulting array. A zero-width match at the beginning however
2268      * never produces such empty leading substring.
2269      *
2270      * <p> The {@code limit} parameter controls the number of times the
2271      * pattern is applied and therefore affects the length of the resulting




2218      * @param   regex
2219      *          the regular expression to which this string is to be matched
2220      * @param   replacement
2221      *          the string to be substituted for each match
2222      *
2223      * @return  The resulting {@code String}
2224      *
2225      * @throws  PatternSyntaxException
2226      *          if the regular expression's syntax is invalid
2227      *
2228      * @see java.util.regex.Pattern
2229      *
2230      * @since 1.4
2231      * @spec JSR-51
2232      */
2233     public String replaceAll(String regex, String replacement) {
2234         return Pattern.compile(regex).matcher(this).replaceAll(replacement);
2235     }
2236 
2237     /**
2238      * The maximum size of array to allocate.
2239      * Some VMs reserve some header words in an array.
2240      * Attempts to allocate larger arrays may result in
2241      * OutOfMemoryError: Requested array size exceeds VM limit
2242      */
2243     private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
2244 
2245     /**
2246      * Replaces each substring of this string that matches the literal target
2247      * sequence with the specified literal replacement sequence. The
2248      * replacement proceeds from the beginning of the string to the end, for
2249      * example, replacing "aa" with "b" in the string "aaa" will result in
2250      * "ba" rather than "ab".
2251      *
2252      * @param  target The sequence of char values to be replaced
2253      * @param  replacement The replacement sequence of char values
2254      * @return  The resulting string
2255      * @since 1.5
2256      */
2257     public String replace(CharSequence target, CharSequence replacement) {
2258         String starget = target.toString();
2259         int targLen = starget.length();
2260         String srepl = replacement.toString();
2261         int replLen = srepl.length();
2262 
2263         // special case: replacing empty substrings
2264         if (targLen == 0) {
2265             return splitAndJoin(srepl);
2266         }
2267 
2268         int i = indexOf(starget);
2269         // special case: nothing to replace
2270         if (i < 0) {
2271             return this;
2272         }
2273 
2274         // find and store indices of substrings to replace
2275         int p = 0, j;
2276         int[] pos = new int[16];
2277         pos[0] = i;
2278         i += targLen;
2279         while ((j = indexOf(starget, i)) > 0) {
2280             if (++p == pos.length) {
2281                 int cap = p << 1;
2282                 // overflow-conscious code
2283                 if (p >= MAX_ARRAY_SIZE - p) {
2284                     if (p == MAX_ARRAY_SIZE)
2285                         throw new OutOfMemoryError();
2286                     cap = MAX_ARRAY_SIZE;
2287                 }
2288                 pos = Arrays.copyOf(pos, cap);
2289             }
2290             pos[p] = j;
2291             i = j + targLen;
2292         }
2293 
2294         final char[] value = this.value;
2295         final char[] replValue = srepl.value;
2296         int thisLen = value.length;
2297         int deltaLen = replLen - targLen;
2298         // overflow-conscious code
2299         int resultLen = thisLen + (p + 1) * deltaLen;
2300         if (MAX_ARRAY_SIZE / (p + 1) < deltaLen ||
2301                 MAX_ARRAY_SIZE - resultLen < 0) {
2302             throw new OutOfMemoryError();
2303         }
2304         if (resultLen <= 0) {
2305             return "";
2306         }
2307 
2308         char[] result = new char[resultLen];
2309         int posFrom = 0, posTo = 0;
2310         for (int q = 0; q <= p; ++q) {
2311             int nextPos = pos[q];
2312             int lenInc = nextPos - posFrom;
2313             if (lenInc > 0) {
2314                 System.arraycopy(value, posFrom, result, posTo, lenInc);
2315                 posTo += lenInc;
2316             }
2317             if (replLen > 0) {
2318                 System.arraycopy(replValue, 0, result, posTo, replLen);
2319                 posTo += replLen;
2320             }
2321             posFrom = nextPos + targLen;
2322         }
2323         System.arraycopy(value, posFrom, result, posTo,
2324                 thisLen - posFrom);
2325 
2326         return new String(result, true);
2327     }
2328 
2329     /**
2330      * Returns this string with every empty substring
2331      * replaced by the given delimiter.
2332      * If this string is empty, the delimiter is returned.
2333      * If the delimiter is empty, unchanged this string
2334      * is returned.
2335      */
2336     private String splitAndJoin(String delimiter) {
2337         final char[] value = this.value;
2338         int thisLen = value.length;
2339         if (thisLen == 0) {
2340             return delimiter;
2341         }
2342 
2343         final char[] delimValue = delimiter.value;
2344         int delimLen = delimValue.length;
2345         if (delimLen == 0) {
2346             return this;
2347         }
2348 
2349         // overflow-conscious code
2350         int resultLen = thisLen + (thisLen + 1) * delimLen;
2351         if (MAX_ARRAY_SIZE / (thisLen + 1) < delimLen ||
2352                 MAX_ARRAY_SIZE - resultLen < 0) {
2353             throw new OutOfMemoryError();
2354         }
2355 
2356         char[] result = new char[resultLen];
2357         int posFrom = 0, posTo = 0;
2358         while (posFrom < thisLen) {
2359             System.arraycopy(delimValue, 0, result, posTo, delimLen);
2360             posTo += delimLen;
2361             result[posTo++] = value[posFrom++];
2362         }
2363         System.arraycopy(delimValue, 0, result, posTo, delimLen);
2364 
2365         return new String(result, true);
2366     }
2367 
2368     /**
2369      * Splits this string around matches of the given
2370      * <a href="../util/regex/Pattern.html#sum">regular expression</a>.
2371      *
2372      * <p> The array returned by this method contains each substring of this
2373      * string that is terminated by another substring that matches the given
2374      * expression or is terminated by the end of the string.  The substrings in
2375      * the array are in the order in which they occur in this string.  If the
2376      * expression does not match any part of the input then the resulting array
2377      * has just one element, namely this string.
2378      *
2379      * <p> When there is a positive-width match at the beginning of this
2380      * string then an empty leading substring is included at the beginning
2381      * of the resulting array. A zero-width match at the beginning however
2382      * never produces such empty leading substring.
2383      *
2384      * <p> The {@code limit} parameter controls the number of times the
2385      * pattern is applied and therefore affects the length of the resulting


< prev index next >