KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > util > StringTools


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1997-1999 Raja Vallee-Rai
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27 package soot.util;
28
29 import java.text.*;
30
31 /** Utility methods for string manipulations commonly used in Soot. */
32 public class StringTools
33 {
34
35     /** Returns fromString, but with non-isalpha() characters printed as
36      * <code>'\\unnnn'</code>. Used by SootClass to generate output. */

37     public static java.lang.String JavaDoc getEscapedStringOf(String JavaDoc fromString)
38     {
39        char[] fromStringArray;
40        int cr, lf, ch;
41         StringBuffer JavaDoc whole = new StringBuffer JavaDoc();
42         StringBuffer JavaDoc mini = new StringBuffer JavaDoc();
43
44        fromStringArray = fromString.toCharArray();
45
46        cr = lineSeparator.charAt(0);
47        lf = -1;
48
49        if (lineSeparator.length() == 2)
50            lf = lineSeparator.charAt(1);
51
52        for (int i = 0; i < fromStringArray.length; i++)
53        {
54            ch = (int) fromStringArray[i];
55            if (((ch >= 32 && ch <= 126) || ch == cr || ch == lf) && ch != '\\')
56            {
57                whole.append((char) ch);
58
59                continue;
60            }
61            
62            mini.setLength(0);
63            mini.append(Integer.toHexString(ch));
64
65            while (mini.length() < 4)
66                mini.insert(0, "0");
67
68            mini.insert(0, "\\u");
69            whole.append(mini.toString());
70        }
71
72        return whole.toString();
73     }
74
75     /** Convenience field storing the system line separator. */
76     public final static String JavaDoc lineSeparator = System.getProperty("line.separator");;
77
78     /** Returns fromString, but with certain characters printed as
79      * if they were in a Java string literal.
80      * Used by StringConstant.toString() */

81     public static java.lang.String JavaDoc getQuotedStringOf(String JavaDoc fromString)
82     {
83         StringBuffer JavaDoc toStringBuffer;
84         char[] fromStringArray;
85
86         toStringBuffer = new java.lang.StringBuffer JavaDoc();
87         fromStringArray = fromString.toCharArray();
88
89         toStringBuffer.append("\"");
90
91         for (int i = 0; i < fromStringArray.length; i++)
92         {
93             char ch = fromStringArray[i];
94             {
95               if (ch == '\\')
96                 { toStringBuffer.append("\\\\"); continue;}
97               if (ch == '\'')
98                 { toStringBuffer.append("\\\'"); continue; }
99               if (ch == '\"')
100                 { toStringBuffer.append("\\\""); continue; }
101               if (ch == '\n')
102                 { toStringBuffer.append("\\n"); continue; }
103               if (ch == '\t')
104                 { toStringBuffer.append("\\t"); continue; }
105               else if((int) ch >= 32 && (int) ch <= 126)
106                 {toStringBuffer.append(ch); continue;}
107             }
108             
109             toStringBuffer.append(getUnicodeStringFromChar(ch));
110         }
111
112         toStringBuffer.append("\"");
113         return toStringBuffer.toString();
114     }
115
116     /** Returns a String containing the escaped <code>\\unnnn</code>
117      * representation for <code>ch</code>. */

118     public static String JavaDoc getUnicodeStringFromChar(char ch)
119     {
120         String JavaDoc s = Integer.toHexString((int) ch);
121         String JavaDoc padding = null;
122         
123         switch(s.length()) {
124         case 1:
125             padding = "000";
126             break;
127         case 2:
128             padding = "00";
129             break;
130         case 3:
131             padding = "0";
132             break;
133         case 4:
134             padding = "";
135             break;
136         }
137         
138         return "\\u" + padding + s;
139     }
140   
141     /** Returns a String de-escaping the <code>\\unnnn</code>
142      * representation for any escaped characters in the string. */

143     public static String JavaDoc getUnEscapedStringOf(String JavaDoc str)
144     {
145         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
146         CharacterIterator iter = new StringCharacterIterator(str);
147         
148         for(char ch = iter.first(); ch != CharacterIterator.DONE; ch = iter.next())
149         {
150             if (ch != '\\')
151                 buf.append(ch);
152             else
153             { // enter escaped mode
154
ch = iter.next();
155                 char format;
156
157                 if(ch == '\\')
158                     buf.append(ch);
159                 else if ( (format = getCFormatChar(ch)) != '\0')
160                     buf.append(format);
161                 else if(ch == 'u')
162                 { //enter unicode mode
163
StringBuffer JavaDoc mini = new StringBuffer JavaDoc(4);
164                     for(int i = 0; i <4; i++)
165                         mini.append(iter.next());
166                     
167                     ch = (char) Integer.parseInt(mini.toString(), 16);
168                     buf.append(ch);
169                 } else {
170                     throw new RuntimeException JavaDoc("Unexpected char: " + ch);
171                 }
172             }
173         }
174         return buf.toString();
175     }
176
177     /** Returns the canonical C-string representation of c. */
178     public static char getCFormatChar(char c)
179     {
180         char res;
181
182         switch(c) {
183         case 'n':
184             res = '\n';
185             break;
186         case 't':
187             res = '\t';
188             break;
189         case 'r':
190             res = '\r';
191             break;
192         case 'b':
193             res = '\b';
194             break;
195         case '\"':
196             res = '\"';
197             break;
198         case '\'':
199             res = '\'';
200             break;
201             
202         default:
203             res = '\0';
204             break;
205         }
206         return res;
207     }
208
209     /**
210      * Replaces all occurrences of the given substring with the
211      * given replacement string.
212      *
213      * @param orig The string in which all occurrences of the substring
214      * are to be replaced.
215      *
216      * @param toBeReplaced The substring which is to be replaced.
217      *
218      * @param replacment The string which is to replace
219      * <code>toBeReplaced</code>.
220      *
221      * @return The resulting {@link String}.
222      *
223      * <p>This method provides a facility similar to the
224      * String.replaceAll() method available in Java 1.4, except that
225      * it can only replace substrings rather than regular expressions.
226      * So if Soot ever abandons compatibility with pre-1.4
227      * definitions of the Java API, it can also abandon replaceAllIn().
228      */

229      public static String JavaDoc replaceAll(String JavaDoc orig, String JavaDoc toBeReplaced,
230                      String JavaDoc replacement) {
231      int quarryLength = toBeReplaced.length();
232      if (quarryLength <= 0) {
233          return orig;
234      }
235
236      int index = orig.indexOf(toBeReplaced);
237      if (index < 0) {
238          return orig;
239      } else {
240          int from = 0;
241          StringBuffer JavaDoc sb;
242          if (quarryLength < replacement.length()) {
243          sb = new StringBuffer JavaDoc(orig.length());
244          } else {
245          sb = new StringBuffer JavaDoc(orig.length() * 2);
246          }
247          
248          do {
249          sb.append(orig.substring(from, index));
250          sb.append(replacement);
251          from = index + quarryLength;
252          index = orig.indexOf(toBeReplaced, from);
253          } while (index >= 0);
254
255          sb.append(orig.substring(from));
256          return sb.toString();
257      }
258      }
259 }
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
Popular Tags