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 package org.melati.poem.util;
47
48
49
50
51 public final class StringUtils {
52
53
54
55
56
57
58
59
60
61
62 public static String[] split(String s, char c) {
63 int n = 0;
64 for (int i = s.indexOf(c); i >= 0; i = s.indexOf(c, i + 1))
65 ++n;
66
67 String[] them = new String[n + 1];
68
69 for (int i = 0, m = 0;; ++m) {
70 int j = s.indexOf(c, i);
71 if (j == -1) {
72 them[m] = s.substring(i);
73 break;
74 } else {
75 them[m] = s.substring(i, j);
76 i = j + 1;
77 }
78 }
79
80 return them;
81 }
82
83
84
85
86
87
88 public static void appendEscaped(StringBuffer b, String s, char character) {
89 appendEscaped(b, s, character, '\\');
90 }
91
92
93
94
95
96
97
98
99
100 public static void appendEscaped(StringBuffer b,
101 String s, char character, char escapeChar) {
102 int l = s.length();
103 for (int i = 0; i < l; ++i) {
104 char c = s.charAt(i);
105 if (c == escapeChar || c == character) {
106
107
108 for (int j = 0; j < i; ++j)
109 b.append(s.charAt(j));
110 b.append(escapeChar);
111 b.append(c);
112
113
114
115 for (++i; i < l; ++i) {
116 c = s.charAt(i);
117 if (c == escapeChar || c == character)
118 b.append(escapeChar);
119 b.append(c);
120 }
121 return;
122 }
123 }
124
125 b.append(s);
126 }
127
128
129
130
131
132
133
134
135
136 public static void appendQuoted(StringBuffer b, String s, char q) {
137 b.append(q);
138 appendEscaped(b, s, q);
139 b.append(q);
140 }
141
142
143
144
145
146
147
148
149 public static String quoted(String i, char q) {
150 StringBuffer b = new StringBuffer();
151 appendQuoted(b, i, q);
152 return b.toString();
153 }
154
155
156
157
158
159
160
161
162 public static String capitalised(String name) {
163 char suffix[] = name.toCharArray();
164 suffix[0] = Character.toUpperCase(suffix[0]);
165 return new String(suffix);
166 }
167
168
169
170
171
172
173
174 public static String uncapitalised(String name) {
175 char suffix[] = name.toCharArray();
176 suffix[0] = Character.toLowerCase(suffix[0]);
177 return new String(suffix);
178 }
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193 public static String tr(String s, String from, String to) {
194 StringBuffer sNew = null;
195
196 for (int i = 0; i < s.length(); ++i) {
197 int t = from.indexOf(s.charAt(i));
198 if (t != -1) {
199 if (sNew == null)
200 sNew = new StringBuffer(s);
201 sNew.setCharAt(i, to.charAt(t));
202 }
203 }
204
205 return sNew == null ? s : sNew.toString();
206 }
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222 public static String tr(String s, char from, char to) {
223 StringBuffer sNew = null;
224
225 for (int i = 0; i < s.length(); ++i) {
226 if (s.charAt(i) == from) {
227 if (sNew == null)
228 sNew = new StringBuffer(s);
229 sNew.setCharAt(i, to);
230 }
231 }
232
233 return sNew == null ? s : sNew.toString();
234 }
235
236
237
238
239
240
241
242
243 public static String concatenated(String sep, String[] xs) {
244 if (sep == null) sep = "";
245 if (xs.length == 0)
246 return "";
247 else {
248 int l = sep.length() * (xs.length - 1) + xs[0].length();
249 for (int i = 1; i < xs.length; ++i)
250 l += xs[i].length();
251
252 StringBuffer c = new StringBuffer(l);
253
254 c.append(xs[0]);
255 for (int i = 1; i < xs.length; ++i) {
256 c.append(sep);
257 c.append(xs[i]);
258 }
259
260 return c.toString();
261 }
262 }
263
264 private static String allowableChars =
265 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz0123456789";
266
267
268
269
270
271
272
273 public static String randomString(int length) {
274 String result = "";
275 int j = allowableChars.length();
276 for (int a = 0; a < length; a++) {
277 int index = new Double(Math.random() * j).intValue();
278 result += allowableChars.charAt(index);
279 }
280 return result;
281 }
282
283
284
285
286
287
288
289 public static String nulled(String s) {
290 if (s != null && s.equals(""))
291 return null;
292 return s;
293 }
294
295
296
297
298
299
300 public static String unNulled(String in) {
301 if (in == null)
302 return "";
303 return in;
304 }
305
306 private static final char[] hexDigits =
307 { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
308 'A', 'B', 'C', 'D', 'E', 'F' };
309
310
311
312
313
314
315 public static String hexEncoding(byte[] bytes) {
316 StringBuffer it = new StringBuffer(bytes.length * 2);
317
318 for (int i = 0; i < bytes.length; ++i) {
319 int b = bytes[i];
320 it.append(hexDigits[b >> 4 & 0xF]);
321 it.append(hexDigits[b & 0xF]);
322 }
323
324 return it.toString();
325 }
326
327
328
329
330
331
332
333 public static byte hexDecoding(char c) {
334 if ('0' <= c && c <= '9')
335 return (byte) (c - '0');
336 else if ('A' <= c && c <= 'F')
337 return (byte) (0xA + c - 'A');
338 else if ('a' <= c && c <= 'f')
339 return (byte) (0xa + c - 'a');
340 else
341 throw new IllegalArgumentException("Invalid hex digit in string");
342 }
343
344
345
346
347
348
349 public static byte[] hexDecoding(String digits) {
350
351 int l = digits.length() / 2;
352 if (l * 2 != digits.length())
353 throw new IllegalArgumentException(
354 "Hex string has odd number of digits");
355
356 byte[] it = new byte[l];
357
358 for (int i = 0; i < l; ++i)
359 it[i] =
360 (byte) (hexDecoding(digits.charAt(i * 2)) << 4 |
361 hexDecoding(digits.charAt(i * 2 + 1)));
362
363 return it;
364 }
365
366
367
368
369
370
371 public static boolean isQuoted(String in) {
372 if (in == null)
373 return false;
374 if (in.length() < 2)
375 return false;
376 if ((in.startsWith("'") || in.startsWith("\""))
377 &&
378 (in.endsWith("'") || in.endsWith("\""))) {
379 return true;
380 }
381 return false;
382 }
383 }