1 package org.apache.maven.surefire.util.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.StringTokenizer;
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 public class StringUtils
54 {
55 private static final byte[] HEX_CHARS = new byte[] {
56 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
57 'A', 'B', 'C', 'D', 'E', 'F' };
58
59 public static String[] split( String text, String separator )
60 {
61 int max = -1;
62 StringTokenizer tok;
63 if ( separator == null )
64 {
65
66
67 tok = new StringTokenizer( text );
68 }
69 else
70 {
71 tok = new StringTokenizer( text, separator );
72 }
73
74 int listSize = tok.countTokens();
75 if ( ( max > 0 ) && ( listSize > max ) )
76 {
77 listSize = max;
78 }
79
80 String[] list = new String[listSize];
81 int i = 0;
82 int lastTokenBegin;
83 int lastTokenEnd = 0;
84 while ( tok.hasMoreTokens() )
85 {
86 if ( ( max > 0 ) && ( i == listSize - 1 ) )
87 {
88
89
90
91 String endToken = tok.nextToken();
92 lastTokenBegin = text.indexOf( endToken, lastTokenEnd );
93 list[i] = text.substring( lastTokenBegin );
94 break;
95 }
96 else
97 {
98 list[i] = tok.nextToken();
99 lastTokenBegin = text.indexOf( list[i], lastTokenEnd );
100 lastTokenEnd = lastTokenBegin + list[i].length();
101 }
102 i++;
103 }
104 return list;
105 }
106
107
108
109
110
111
112
113
114
115 public static boolean isBlank( String str )
116 {
117 return ( ( str == null ) || ( str.trim().length() == 0 ) );
118 }
119
120
121
122
123
124
125
126
127
128
129 public static void escapeToPrintable( StringBuilder target, CharSequence str )
130 {
131 if ( target == null )
132 {
133 throw new IllegalArgumentException( "The target buffer must not be null" );
134 }
135 if ( str == null )
136 {
137 return;
138 }
139
140 for ( int i = 0; i < str.length(); i++ ) {
141 char c = str.charAt( i );
142
143
144 if ( c < 32 || c > 126 || c == '\\' || c == ',' )
145 {
146 target.append( '\\' );
147 target.append( (char) HEX_CHARS[( 0xF000 & c ) >> 12] );
148 target.append( (char) HEX_CHARS[( 0x0F00 & c ) >> 8] );
149 target.append( (char) HEX_CHARS[( 0x00F0 & c ) >> 4] );
150 target.append( (char) HEX_CHARS[( 0x000F & c )] );
151 }
152 else
153 {
154 target.append( c );
155 }
156 }
157 }
158
159
160
161
162
163
164
165 public static void unescapeString( StringBuilder target, CharSequence str )
166 {
167 if ( target == null )
168 {
169 throw new IllegalArgumentException( "The target buffer must not be null" );
170 }
171 if ( str == null )
172 {
173 return;
174 }
175
176 for ( int i = 0; i < str.length(); i++ )
177 {
178 char ch = str.charAt( i );
179
180 if ( ch == '\\' )
181 {
182 target.append( (char) (
183 digit( str.charAt( ++i ) ) << 12
184 | digit( str.charAt( ++i ) ) << 8
185 | digit( str.charAt( ++i ) ) << 4
186 | digit( str.charAt( ++i ) )
187 ) );
188 }
189 else
190 {
191 target.append( ch );
192 }
193 }
194 }
195
196 private static int digit( char ch )
197 {
198 if ( ch >= 'a' )
199 {
200 return 10 + ch - 'a';
201 }
202 else if ( ch >= 'A' )
203 {
204 return 10 + ch - 'A';
205 }
206 else
207 {
208 return ch - '0';
209 }
210 }
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228 public static int escapeBytesToPrintable( byte[] out, int outoff, byte[] input, int off, int len )
229 {
230 if ( out == null )
231 {
232 throw new IllegalArgumentException( "The output array must not be null" );
233 }
234 if ( input == null || input.length == 0 )
235 {
236 return 0;
237 }
238 int outputPos = outoff;
239 int end = off + len;
240 for ( int i = off; i < end; i++ )
241 {
242 byte b = input[i];
243
244
245 if ( b < 32 || b > 126 || b == '\\' || b == ',' )
246 {
247 int upper = ( 0xF0 & b ) >> 4;
248 int lower = ( 0x0F & b );
249 out[outputPos++] = '\\';
250 out[outputPos++] = HEX_CHARS[upper];
251 out[outputPos++] = HEX_CHARS[lower];
252 }
253 else
254 {
255 out[outputPos++] = b;
256 }
257 }
258
259 return outputPos - outoff;
260 }
261
262
263
264
265
266
267
268
269
270
271 public static int unescapeBytes( byte[] out, String str )
272 {
273 int outPos = 0;
274 if ( out == null )
275 {
276 throw new IllegalArgumentException( "The output array must not be null" );
277 }
278 if ( str == null )
279 {
280 return 0;
281 }
282 for ( int i = 0; i < str.length(); i++ )
283 {
284 char ch = str.charAt( i );
285
286 if ( ch == '\\' )
287 {
288 int upper = digit( str.charAt( ++i ) );
289 int lower = digit( str.charAt( ++i ) );
290 out[outPos++] = (byte) ( upper << 4 | lower );
291 }
292 else
293 {
294 out[outPos++] = (byte) ch;
295 }
296 }
297 return outPos;
298 }
299 }