-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathStrings.java
More file actions
397 lines (358 loc) · 12.8 KB
/
Copy pathStrings.java
File metadata and controls
397 lines (358 loc) · 12.8 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
*
* Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product
* that is described in this document. In particular, and without limitation, these intellectual property
* rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or
* more additional patents or pending patent applications in the U.S. and in other countries.
*
* U.S. Government Rights - Commercial software. Government users are subject to the Sun
* Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its
* supplements.
*
* Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or
* registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks
* are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the
* U.S. and other countries.
*
* UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open
* Company, Ltd.
*/
package com.sun.max.lang;
import java.io.*;
import java.util.*;
/**
* Additional String-related operations.
*
* @author Bernd Mathiske
*/
public final class Strings {
private Strings() {
}
/**
* @param stream
* the input stream to be read in its entirety and then closed
* @return the contents of the input stream as a String, with line breaks
* @throws IOException
* as usual
*/
public static String fromInputStream(InputStream stream) throws IOException {
final BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
final StringBuffer result = new StringBuffer();
final String lineSeparator = System.getProperty("line.separator");
while (true) {
final String line = reader.readLine();
if (line == null) {
stream.close();
return result.toString();
}
result.append(line);
result.append(lineSeparator);
}
}
public static String firstCharToLowerCase(String s) {
if (s == null || s.length() == 0) {
return s;
}
return s.substring(0, 1).toLowerCase() + s.substring(1);
}
public static String firstCharToUpperCase(String s) {
if (s == null || s.length() == 0) {
return s;
}
return s.substring(0, 1).toUpperCase() + s.substring(1);
}
public static String times(char ch, int nTimes) {
if (nTimes <= 0) {
return "";
}
final char[] chars = new char[nTimes];
for (int i = 0; i < nTimes; i++) {
chars[i] = ch;
}
return new String(chars);
}
private static final char[] SPACES;
static {
SPACES = new char[200];
java.util.Arrays.fill(SPACES, ' ');
}
public static String spaces(int nSpaces) {
if (nSpaces <= 0) {
return "";
}
if (nSpaces <= SPACES.length) {
return new String(SPACES, 0, nSpaces);
}
return times(' ', nSpaces);
}
/**
* @return The String {@code s} padded out to {@code length}, if needed, by appending space characters
*/
public static String padLengthWithSpaces(String s, int length) {
if (s.length() >= length) {
return s;
}
return s + spaces(length - s.length());
}
/**
* @return The string {@code s} padded out to {@code length}, if needed, by prepending space characters
*/
public static String padLengthWithSpaces(int length, String s) {
if (s.length() >= length) {
return s;
}
return spaces(length - s.length()) + s;
}
private static final char[] ZEROES;
static {
ZEROES = new char[200];
java.util.Arrays.fill(ZEROES, '0');
}
public static String zeroes(int nZeroes) {
if (nZeroes <= 0) {
return "";
}
if (nZeroes <= ZEROES.length) {
return new String(ZEROES, 0, nZeroes);
}
return times(' ', nZeroes);
}
/**
* @return The String {@code s} padded out to {@code length}, if needed, by appending zero characters
*/
public static String padLengthWithZeroes(String s, int length) {
if (s.length() >= length) {
return s;
}
return s + zeroes(length - s.length());
}
/**
* @return The string {@code s} padded out to {@code length}, if needed, by prepending zero characters
*/
public static String padLengthWithZeroes(int length, String s) {
if (s.length() >= length) {
return s;
}
return zeroes(length - s.length()) + s;
}
/**
* Finds the index of the first non-escaped instance of {@code c} in {@code s} starting at {@code fromIndex}.
* The search takes into account that the escape char (i.e. {@code '\'}) may itself be escaped.
*
* @return -1 if the char could not be found
*/
public static int indexOfNonEscapedChar(char c, String s, int fromIndex) {
int index = s.indexOf(c, fromIndex);
while (index != -1) {
if (index > 0 && (s.charAt(index - 1) != '\\') || (index > 1 && s.charAt(index - 2) == '\\')) {
return index;
}
index = s.indexOf(c, index + 1);
}
return -1;
}
/**
* Parses a command line into a string array appropriate for calling {@link Runtime#exec(String[])}.
* The given command line is tokenized around {@link Character#isWhitespace(char) whitespaces}
* except for sequences of characters enclosed in non-escaped double quotes (after the double
* quotes are removed).
*/
public static String[] splitCommand(String command) {
final List<String> parts = new ArrayList<String>();
boolean escapedChar = false;
boolean insideQuotes = false;
final char[] buffer = new char[command.length()];
int pos = 0;
for (int index = 0; index < command.length(); ++index) {
final char ch = command.charAt(index);
if (escapedChar) {
escapedChar = false;
} else {
if (ch == '\\') {
escapedChar = true;
} else {
if (insideQuotes) {
if (ch == '"') {
insideQuotes = false;
continue;
}
} else {
if (ch == '"') {
insideQuotes = true;
continue;
} else if (Character.isWhitespace(ch)) {
if (pos != 0) {
parts.add(new String(buffer, 0, pos));
pos = 0;
}
continue;
}
}
}
}
buffer[pos++] = ch;
}
if (insideQuotes) {
throw new IllegalArgumentException("unclosed quotes");
}
if (escapedChar) {
throw new IllegalArgumentException("command line cannot end with escape char '\\'");
}
if (pos != 0) {
parts.add(new String(buffer, 0, pos));
}
return parts.toArray(new String[parts.size()]);
}
public static String truncate(String s, int maxLength) {
if (maxLength < 0) {
throw new IllegalArgumentException();
}
if (s.length() <= maxLength) {
return s;
}
return s.substring(0, maxLength) + "...";
}
/**
* Capitalizes the first character in a given string.
*
* @param string the string to process
* @param lowercaseTail if true, the remaining characters in {@code string} are converted to lower case
*/
public static String capitalizeFirst(String string, boolean lowercaseTail) {
final String tail = string.substring(1);
return Character.toUpperCase(string.charAt(0)) + (lowercaseTail ? tail.toLowerCase() : tail);
}
/**
* Chops the last {@code count} from a given string.
*
* @param s the string to chop
* @param count the number of characters to chop from the end of {@code s}
* @return the chopped string
* @throws IndexOutOfBoundsException if {@code count < 0} or {@code count > s.length()}
*/
public static String chopSuffix(String s, int count) {
return s.substring(0, s.length() - count);
}
/**
* Chops the last {@code suffix.length()} from a given string. Calling this method is
* equivalent to {@link #chopSuffix(String, int) chop(s, suffix.length())}.
*/
public static String chopSuffix(String s, String suffix) {
return chopSuffix(s, suffix.length());
}
/**
* Prepends {@code n} space characters to every line in String {@code lines},
* including a possibly non-empty line following the final newline.
* Returns {@code lines} if {@code spaces <= 0}
*/
public static String indent(String lines, int spaces) {
return indent(lines, spaces(spaces));
}
/**
* Prepends the String {@code indentation} to every line in String {@code lines},
* including a possibly non-empty line following the final newline.
*/
public static String indent(String lines, String indentation) {
if (lines.length() == 0) {
return lines;
}
final String newLine = "\n";
if (lines.endsWith(newLine)) {
return indentation + (lines.substring(0, lines.length() - 1)).replace(newLine, newLine + indentation) + newLine;
}
return indentation + lines.replace(newLine, newLine + indentation);
}
public static String formatParagraphs(String s, int leftJust, int pindent, int width) {
final int len = s.length();
int indent = pindent;
indent += leftJust;
int consumed = indent + leftJust;
final String indstr = space(indent);
final String ljstr = space(leftJust);
final StringBuffer buf = new StringBuffer(s.length() + 50);
buf.append(indstr);
int lastSp = -1;
for (int cntr = 0; cntr < len; cntr++) {
final char c = s.charAt(cntr);
if (c == '\n') {
buf.append('\n');
consumed = indent;
buf.append(indstr);
continue;
} else if (Character.isWhitespace(c)) {
lastSp = buf.length();
}
buf.append(c);
consumed++;
if (consumed > width) {
if (lastSp >= 0) {
buf.setCharAt(lastSp, '\n');
buf.insert(lastSp + 1, ljstr);
consumed = buf.length() - lastSp + leftJust - 1;
}
}
}
return buf.toString();
}
protected static final String[] spacers = {
"", // 0
" ", // 1
" ", // 2
" ", // 3
" ", // 4
" ", // 5
" ", // 6
" ", // 7
" ", // 8
" ", // 9
" ", // 10
};
public static void appendFract(StringBuffer buf, double val, int digits) {
int cntr = 0;
for (int radix = 10; cntr < digits; radix = radix * 10, cntr++) {
if (cntr == 0) {
buf.append('.');
}
final int digit = (int) (val * radix) % 10;
buf.append((char) (digit + '0'));
}
}
public static String fixedDouble(double fval, int places) {
if (Double.isInfinite(fval)) {
return "(inf)";
}
if (Double.isNaN(fval)) {
return "(NaN)";
}
final StringBuffer buf = new StringBuffer(places + 5);
// append the whole part
final long val = (long) fval;
buf.append(val);
// append the fractional part
final double fract = fval >= 0 ? fval - val : val - fval;
appendFract(buf, fract, places);
return buf.toString();
}
public static String space(int len) {
if (len <= 0) {
return "";
}
if (len < spacers.length) {
return spacers[len];
}
return times(' ', len);
}
public static void space(StringBuffer buf, int len) {
int i = 0;
while (i++ < len) {
buf.append(' ');
}
}
public static String concat(String first, String second, String separator) {
if (!first.isEmpty()) {
return first + separator + second;
}
return second;
}
}