forked from soot-oss/soot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringTools.java
More file actions
180 lines (168 loc) · 5.49 KB
/
StringTools.java
File metadata and controls
180 lines (168 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package soot.util;
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 1997 - 1999 Raja Vallee-Rai
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
/** Utility methods for string manipulations commonly used in Soot. */
public class StringTools {
/** Convenience field storing the system line separator. */
public final static String lineSeparator = System.getProperty("line.separator");
/**
* Returns fromString, but with non-isalpha() characters printed as <code>'\\unnnn'</code>. Used by SootClass to generate
* output.
*/
public static String getEscapedStringOf(String fromString) {
StringBuilder whole = new StringBuilder();
assert (!lineSeparator.isEmpty());
final int cr = lineSeparator.charAt(0);
final int lf = (lineSeparator.length() == 2) ? lineSeparator.charAt(1) : -1;
for (char ch : fromString.toCharArray()) {
int asInt = ch;
if (asInt != '\\' && ((asInt >= 32 && asInt <= 126) || asInt == cr || asInt == lf)) {
whole.append(ch);
} else {
whole.append(getUnicodeStringFromChar(ch));
}
}
return whole.toString();
}
/**
* Returns fromString, but with certain characters printed as if they were in a Java string literal. Used by
* StringConstant.toString()
*/
public static String getQuotedStringOf(String fromString) {
final int fromStringLen = fromString.length();
// We definitely need fromStringLen + 2, but let's have some additional space
StringBuilder toStringBuffer = new StringBuilder(fromStringLen + 20);
toStringBuffer.append("\"");
for (int i = 0; i < fromStringLen; i++) {
char ch = fromString.charAt(i);
switch (ch) {
case '\\':
toStringBuffer.append("\\\\");
break;
case '\'':
toStringBuffer.append("\\\'");
break;
case '\"':
toStringBuffer.append("\\\"");
break;
case '\n':
toStringBuffer.append("\\n");
break;
case '\t':
toStringBuffer.append("\\t");
break;
case '\r':
/*
* 04.04.2006 mbatch added handling of \r, as compilers throw error if unicode
*/
toStringBuffer.append("\\r");
break;
case '\f':
/*
* 10.04.2006 Nomait A Naeem added handling of \f, as compilers throw error if unicode
*/
toStringBuffer.append("\\f");
break;
default:
if (ch >= 32 && ch <= 126) {
toStringBuffer.append(ch);
} else {
toStringBuffer.append(getUnicodeStringFromChar(ch));
}
break;
}
}
toStringBuffer.append("\"");
return toStringBuffer.toString();
}
/**
* Returns a String containing the escaped <code>\\unnnn</code> representation for <code>ch</code>.
*/
public static String getUnicodeStringFromChar(char ch) {
String s = Integer.toHexString(ch);
switch (s.length()) {
case 1:
return "\\u" + "000" + s;
case 2:
return "\\u" + "00" + s;
case 3:
return "\\u" + "0" + s;
case 4:
return "\\u" + "" + s;
default:
// hex value of a char never exceeds 4 characters since char is 2 bytes
throw new AssertionError("invalid hex string '" + s + "' from char '" + ch + "'");
}
}
/**
* Returns a String de-escaping the <code>\\unnnn</code> representation for any escaped characters in the string.
*/
public static String getUnEscapedStringOf(String str) {
StringBuilder buf = new StringBuilder();
CharacterIterator iter = new StringCharacterIterator(str);
for (char ch = iter.first(); ch != CharacterIterator.DONE; ch = iter.next()) {
if (ch != '\\') {
buf.append(ch);
} else { // enter escaped mode
ch = iter.next();
char format;
if (ch == '\\') {
buf.append(ch);
} else if ((format = getCFormatChar(ch)) != '\0') {
buf.append(format);
} else if (ch == 'u') { // enter unicode mode
StringBuilder mini = new StringBuilder(4);
for (int i = 0; i < 4; i++) {
mini.append(iter.next());
}
buf.append((char) Integer.parseInt(mini.toString(), 16));
} else {
throw new RuntimeException("Unexpected char: " + ch);
}
}
}
return buf.toString();
}
/** Returns the canonical C-string representation of c. */
public static char getCFormatChar(char c) {
switch (c) {
case 'n':
return '\n';
case 't':
return '\t';
case 'r':
return '\r';
case 'b':
return '\b';
case 'f':
return '\f';
case '\"':
return '\"';
case '\'':
return '\'';
default:
return '\0';
}
}
}