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 @SuppressWarnings( "checkstyle:magicnumber" )
130 public static void escapeToPrintable( StringBuilder target, CharSequence str )
131 {
132 if ( target == null )
133 {
134 throw new IllegalArgumentException( "The target buffer must not be null" );
135 }
136 if ( str == null )
137 {
138 return;
139 }
140
141 for ( int i = 0; i < str.length(); i++ )
142 {
143 char c = str.charAt( i );
144
145
146 if ( c < 32 || c > 126 || c == '\\' || c == ',' )
147 {
148 target.append( '\\' );
149 target.append( (char) HEX_CHARS[( 0xF000 & c ) >> 12] );
150 target.append( (char) HEX_CHARS[( 0x0F00 & c ) >> 8] );
151 target.append( (char) HEX_CHARS[( 0x00F0 & c ) >> 4] );
152 target.append( (char) HEX_CHARS[( 0x000F & c )] );
153 }
154 else
155 {
156 target.append( c );
157 }
158 }
159 }
160
161
162
163
164
165
166
167 public static void unescapeString( StringBuilder target, CharSequence str )
168 {
169 if ( target == null )
170 {
171 throw new IllegalArgumentException( "The target buffer must not be null" );
172 }
173 if ( str == null )
174 {
175 return;
176 }
177
178 for ( int i = 0; i < str.length(); i++ )
179 {
180 char ch = str.charAt( i );
181
182 if ( ch == '\\' )
183 {
184 target.append( (char) (
185 digit( str.charAt( ++i ) ) << 12
186 | digit( str.charAt( ++i ) ) << 8
187 | digit( str.charAt( ++i ) ) << 4
188 | digit( str.charAt( ++i ) )
189 ) );
190 }
191 else
192 {
193 target.append( ch );
194 }
195 }
196 }
197
198 private static int digit( char ch )
199 {
200 if ( ch >= 'a' )
201 {
202 return 10 + ch - 'a';
203 }
204 else if ( ch >= 'A' )
205 {
206 return 10 + ch - 'A';
207 }
208 else
209 {
210 return ch - '0';
211 }
212 }
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230 @SuppressWarnings( "checkstyle:magicnumber" )
231 public static int escapeBytesToPrintable( byte[] out, int outoff, byte[] input, int off, int len )
232 {
233 if ( out == null )
234 {
235 throw new IllegalArgumentException( "The output array must not be null" );
236 }
237 if ( input == null || input.length == 0 )
238 {
239 return 0;
240 }
241 int outputPos = outoff;
242 int end = off + len;
243 for ( int i = off; i < end; i++ )
244 {
245 byte b = input[i];
246
247
248 if ( b < 32 || b > 126 || b == '\\' || b == ',' )
249 {
250 int upper = ( 0xF0 & b ) >> 4;
251 int lower = ( 0x0F & b );
252 out[outputPos++] = '\\';
253 out[outputPos++] = HEX_CHARS[upper];
254 out[outputPos++] = HEX_CHARS[lower];
255 }
256 else
257 {
258 out[outputPos++] = b;
259 }
260 }
261
262 return outputPos - outoff;
263 }
264
265
266
267
268
269
270
271
272
273
274 public static int unescapeBytes( byte[] out, String str )
275 {
276 int outPos = 0;
277 if ( out == null )
278 {
279 throw new IllegalArgumentException( "The output array must not be null" );
280 }
281 if ( str == null )
282 {
283 return 0;
284 }
285 for ( int i = 0; i < str.length(); i++ )
286 {
287 char ch = str.charAt( i );
288
289 if ( ch == '\\' )
290 {
291 int upper = digit( str.charAt( ++i ) );
292 int lower = digit( str.charAt( ++i ) );
293 out[outPos++] = (byte) ( upper << 4 | lower );
294 }
295 else
296 {
297 out[outPos++] = (byte) ch;
298 }
299 }
300 return outPos;
301 }
302 }