01: /*
02: [The "BSD licence"]
03: Copyright (c) 2005-2006 Terence Parr
04: All rights reserved.
05:
06: Redistribution and use in source and binary forms, with or without
07: modification, are permitted provided that the following conditions
08: are met:
09: 1. Redistributions of source code must retain the above copyright
10: notice, this list of conditions and the following disclaimer.
11: 2. Redistributions in binary form must reproduce the above copyright
12: notice, this list of conditions and the following disclaimer in the
13: documentation and/or other materials provided with the distribution.
14: 3. The name of the author may not be used to endorse or promote products
15: derived from this software without specific prior written permission.
16:
17: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27: */
28: package org.antlr.misc;
29:
30: public class Utils {
31: public static final int INTEGER_POOL_MAX_VALUE = 1000;
32: static Integer[] ints = new Integer[INTEGER_POOL_MAX_VALUE + 1];
33:
34: /** Integer objects are immutable so share all Integers with the
35: * same value up to some max size. Use an array as a perfect hash.
36: * Return shared object for 0..INTEGER_POOL_MAX_VALUE or a new
37: * Integer object with x in it.
38: */
39: public static Integer integer(int x) {
40: if (x < 0 || x > INTEGER_POOL_MAX_VALUE) {
41: return new Integer(x);
42: }
43: if (ints[x] == null) {
44: ints[x] = new Integer(x);
45: }
46: return ints[x];
47: }
48:
49: /** Given a source string, src,
50: a string to replace, replacee,
51: and a string to replace with, replacer,
52: return a new string w/ the replacing done.
53: You can use replacer==null to remove replacee from the string.
54:
55: This should be faster than Java's String.replaceAll as that one
56: uses regex (I only want to play with strings anyway).
57: */
58: public static String replace(String src, String replacee,
59: String replacer) {
60: StringBuffer result = new StringBuffer(src.length() + 50);
61: int startIndex = 0;
62: int endIndex = src.indexOf(replacee);
63: while (endIndex != -1) {
64: result.append(src.substring(startIndex, endIndex));
65: if (replacer != null) {
66: result.append(replacer);
67: }
68: startIndex = endIndex + replacee.length();
69: endIndex = src.indexOf(replacee, startIndex);
70: }
71: result.append(src.substring(startIndex, src.length()));
72: return result.toString();
73: }
74: }
|