comparison src/org/eclipse/jetty/util/StringUtil.java @ 1012:8d0bdd357e6e

simplify StringUtil
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 24 Oct 2016 00:47:24 -0600
parents 3242aff51053
children 6939226e0ac4
comparison
equal deleted inserted replaced
1011:4e7208df7741 1012:8d0bdd357e6e
31 * main aim of the optimizations is to avoid object creation unless 31 * main aim of the optimizations is to avoid object creation unless
32 * absolutely required. 32 * absolutely required.
33 * 33 *
34 * 34 *
35 */ 35 */
36 public class StringUtil 36 public final class StringUtil
37 { 37 {
38 private static final Logger LOG = LoggerFactory.getLogger(StringUtil.class); 38 private static final Logger LOG = LoggerFactory.getLogger(StringUtil.class);
39 39
40 public static final String ALL_INTERFACES="0.0.0.0"; 40 public static final String ALL_INTERFACES="0.0.0.0";
41 public static final String CRLF="\015\012";
42 41
43 public static final String __ISO_8859_1="ISO-8859-1"; 42 public static final String __ISO_8859_1="ISO-8859-1";
44 public final static String __UTF8="UTF-8"; 43 public final static String __UTF8="UTF-8";
45 public final static String __UTF8Alt="UTF8";
46 public final static String __UTF16="UTF-16"; 44 public final static String __UTF16="UTF-16";
47 45
48 public final static Charset __UTF8_CHARSET; 46 public final static Charset __UTF8_CHARSET;
49 public final static Charset __ISO_8859_1_CHARSET; 47 public final static Charset __ISO_8859_1_CHARSET;
50 48
106 } 104 }
107 105
108 return c==null?s:new String(c); 106 return c==null?s:new String(c);
109 } 107 }
110 108
111
112 /* ------------------------------------------------------------ */
113 public static boolean startsWithIgnoreCase(String s,String w)
114 {
115 if (w==null)
116 return true;
117
118 if (s==null || s.length()<w.length())
119 return false;
120
121 for (int i=0;i<w.length();i++)
122 {
123 char c1=s.charAt(i);
124 char c2=w.charAt(i);
125 if (c1!=c2)
126 {
127 if (c1<=127)
128 c1=lowercases[c1];
129 if (c2<=127)
130 c2=lowercases[c2];
131 if (c1!=c2)
132 return false;
133 }
134 }
135 return true;
136 }
137
138 /* ------------------------------------------------------------ */
139 public static boolean endsWithIgnoreCase(String s,String w)
140 {
141 if (w==null)
142 return true;
143
144 if (s==null)
145 return false;
146
147 int sl=s.length();
148 int wl=w.length();
149
150 if (sl<wl)
151 return false;
152
153 for (int i=wl;i-->0;)
154 {
155 char c1=s.charAt(--sl);
156 char c2=w.charAt(i);
157 if (c1!=c2)
158 {
159 if (c1<=127)
160 c1=lowercases[c1];
161 if (c2<=127)
162 c2=lowercases[c2];
163 if (c1!=c2)
164 return false;
165 }
166 }
167 return true;
168 }
169
170 /* ------------------------------------------------------------ */
171 /**
172 * returns the next index of a character from the chars string
173 */
174 public static int indexFrom(String s,String chars)
175 {
176 for (int i=0;i<s.length();i++)
177 if (chars.indexOf(s.charAt(i))>=0)
178 return i;
179 return -1;
180 }
181
182 /* ------------------------------------------------------------ */ 109 /* ------------------------------------------------------------ */
183 /** 110 /**
184 * replace substrings within string. 111 * replace substrings within string.
185 */ 112 */
186 public static String replace(String s, String sub, String with) 113 public static String replace(String s, String sub, String with)
206 133
207 } 134 }
208 135
209 136
210 /* ------------------------------------------------------------ */ 137 /* ------------------------------------------------------------ */
211 /** Remove single or double quotes.
212 */
213 public static String unquote(String s)
214 {
215 return QuotedStringTokenizer.unquote(s);
216 }
217
218
219 /* ------------------------------------------------------------ */
220 /** Append substring to StringBuilder
221 * @param buf StringBuilder to append to
222 * @param s String to append from
223 * @param offset The offset of the substring
224 * @param length The length of the substring
225 */
226 public static void append(StringBuilder buf,
227 String s,
228 int offset,
229 int length)
230 {
231 synchronized(buf)
232 {
233 int end=offset+length;
234 for (int i=offset; i<end;i++)
235 {
236 if (i>=s.length())
237 break;
238 buf.append(s.charAt(i));
239 }
240 }
241 }
242
243
244 /* ------------------------------------------------------------ */
245 /** 138 /**
246 * append hex digit 139 * append hex digit
247 * 140 *
248 */ 141 */
249 public static void append(StringBuilder buf,byte b,int base) 142 public static void append(StringBuilder buf,byte b,int base)
256 c='0'+bi%base; 149 c='0'+bi%base;
257 if (c>'9') 150 if (c>'9')
258 c= 'a'+(c-'0'-10); 151 c= 'a'+(c-'0'-10);
259 buf.append((char)c); 152 buf.append((char)c);
260 } 153 }
261
262 /* ------------------------------------------------------------ */
263 public static void append2digits(StringBuffer buf,int i)
264 {
265 if (i<100)
266 {
267 buf.append((char)(i/10+'0'));
268 buf.append((char)(i%10+'0'));
269 }
270 }
271 154
272 /* ------------------------------------------------------------ */
273 public static void append2digits(StringBuilder buf,int i) 155 public static void append2digits(StringBuilder buf,int i)
274 { 156 {
275 if (i<100) 157 if (i<100)
276 { 158 {
277 buf.append((char)(i/10+'0')); 159 buf.append((char)(i/10+'0'));
278 buf.append((char)(i%10+'0')); 160 buf.append((char)(i%10+'0'));
279 } 161 }
280 } 162 }
281 163
282 /* ------------------------------------------------------------ */
283 /** Return a non null string.
284 * @param s String
285 * @return The string passed in or empty string if it is null.
286 */
287 public static String nonNull(String s)
288 {
289 if (s==null)
290 return "";
291 return s;
292 }
293
294 /* ------------------------------------------------------------ */
295 public static boolean equals(String s,char[] buf, int offset, int length)
296 {
297 if (s.length()!=length)
298 return false;
299 for (int i=0;i<length;i++)
300 if (buf[offset+i]!=s.charAt(i))
301 return false;
302 return true;
303 }
304
305 /* ------------------------------------------------------------ */
306 public static String toUTF8String(byte[] b,int offset,int length)
307 {
308 try
309 {
310 return new String(b,offset,length,__UTF8);
311 }
312 catch (UnsupportedEncodingException e)
313 {
314 throw new IllegalArgumentException(e);
315 }
316 }
317
318 /* ------------------------------------------------------------ */
319 public static String toString(byte[] b,int offset,int length,String charset) 164 public static String toString(byte[] b,int offset,int length,String charset)
320 { 165 {
321 try 166 try
322 { 167 {
323 return new String(b,offset,length,charset); 168 return new String(b,offset,length,charset);
326 { 171 {
327 throw new IllegalArgumentException(e); 172 throw new IllegalArgumentException(e);
328 } 173 }
329 } 174 }
330 175
331
332 /* ------------------------------------------------------------ */
333 public static boolean isUTF8(String charset) 176 public static boolean isUTF8(String charset)
334 { 177 {
335 return __UTF8.equalsIgnoreCase(charset)||__UTF8Alt.equalsIgnoreCase(charset); 178 return __UTF8.equalsIgnoreCase(charset)||__UTF8Alt.equalsIgnoreCase(charset);
336 } 179 }
337 180
338 181
339 /* ------------------------------------------------------------ */
340 public static String printable(String name)
341 {
342 if (name==null)
343 return null;
344 StringBuilder buf = new StringBuilder(name.length());
345 for (int i=0;i<name.length();i++)
346 {
347 char c=name.charAt(i);
348 if (!Character.isISOControl(c))
349 buf.append(c);
350 }
351 return buf.toString();
352 }
353
354 /* ------------------------------------------------------------ */
355 public static String printable(byte[] b)
356 {
357 StringBuilder buf = new StringBuilder();
358 for (int i=0;i<b.length;i++)
359 {
360 char c=(char)b[i];
361 if (Character.isWhitespace(c)|| c>' ' && c<0x7f)
362 buf.append(c);
363 else
364 {
365 buf.append("0x");
366 TypeUtil.toHex(b[i],buf);
367 }
368 }
369 return buf.toString();
370 }
371
372 public static byte[] getBytes(String s) 182 public static byte[] getBytes(String s)
373 { 183 {
374 try 184 try
375 { 185 {
376 return s.getBytes(__ISO_8859_1); 186 return s.getBytes(__ISO_8859_1);
379 { 189 {
380 LOG.warn("",e); 190 LOG.warn("",e);
381 return s.getBytes(); 191 return s.getBytes();
382 } 192 }
383 } 193 }
384
385 public static byte[] getBytes(String s,String charset)
386 {
387 try
388 {
389 return s.getBytes(charset);
390 }
391 catch(Exception e)
392 {
393 LOG.warn("",e);
394 return s.getBytes();
395 }
396 }
397
398
399
400 /**
401 * Converts a binary SID to a string SID
402 *
403 * http://en.wikipedia.org/wiki/Security_Identifier
404 *
405 * S-1-IdentifierAuthority-SubAuthority1-SubAuthority2-...-SubAuthorityn
406 */
407 public static String sidBytesToString(byte[] sidBytes)
408 {
409 StringBuilder sidString = new StringBuilder();
410
411 // Identify this as a SID
412 sidString.append("S-");
413
414 // Add SID revision level (expect 1 but may change someday)
415 sidString.append(Byte.toString(sidBytes[0])).append('-');
416
417 StringBuilder tmpBuilder = new StringBuilder();
418
419 // crunch the six bytes of issuing authority value
420 for (int i = 2; i <= 7; ++i)
421 {
422 tmpBuilder.append(Integer.toHexString(sidBytes[i] & 0xFF));
423 }
424
425 sidString.append(Long.parseLong(tmpBuilder.toString(), 16)); // '-' is in the subauth loop
426
427 // the number of subAuthorities we need to attach
428 int subAuthorityCount = sidBytes[1];
429 194
430 // attach each of the subAuthorities
431 for (int i = 0; i < subAuthorityCount; ++i)
432 {
433 int offset = i * 4;
434 tmpBuilder.setLength(0);
435 // these need to be zero padded hex and little endian
436 tmpBuilder.append(String.format("%02X%02X%02X%02X",
437 (sidBytes[11 + offset] & 0xFF),
438 (sidBytes[10 + offset] & 0xFF),
439 (sidBytes[9 + offset] & 0xFF),
440 (sidBytes[8 + offset] & 0xFF)));
441 sidString.append('-').append(Long.parseLong(tmpBuilder.toString(), 16));
442 }
443
444 return sidString.toString();
445 }
446
447 /**
448 * Converts a string SID to a binary SID
449 *
450 * http://en.wikipedia.org/wiki/Security_Identifier
451 *
452 * S-1-IdentifierAuthority-SubAuthority1-SubAuthority2-...-SubAuthorityn
453 */
454 public static byte[] sidStringToBytes( String sidString )
455 {
456 String[] sidTokens = sidString.split("-");
457
458 int subAuthorityCount = sidTokens.length - 3; // S-Rev-IdAuth-
459
460 int byteCount = 0;
461 byte[] sidBytes = new byte[1 + 1 + 6 + (4 * subAuthorityCount)];
462
463 // the revision byte
464 sidBytes[byteCount++] = (byte)Integer.parseInt(sidTokens[1]);
465
466 // the # of sub authorities byte
467 sidBytes[byteCount++] = (byte)subAuthorityCount;
468
469 // the certAuthority
470 String hexStr = Long.toHexString(Long.parseLong(sidTokens[2]));
471
472 while( hexStr.length() < 12) // pad to 12 characters
473 {
474 hexStr = "0" + hexStr;
475 }
476
477 // place the certAuthority 6 bytes
478 for ( int i = 0 ; i < hexStr.length(); i = i + 2)
479 {
480 sidBytes[byteCount++] = (byte)Integer.parseInt(hexStr.substring(i, i + 2),16);
481 }
482
483
484 for ( int i = 3; i < sidTokens.length ; ++i)
485 {
486 hexStr = Long.toHexString(Long.parseLong(sidTokens[i]));
487
488 while( hexStr.length() < 8) // pad to 8 characters
489 {
490 hexStr = "0" + hexStr;
491 }
492
493 // place the inverted sub authorities, 4 bytes each
494 for ( int j = hexStr.length(); j > 0; j = j - 2)
495 {
496 sidBytes[byteCount++] = (byte)Integer.parseInt(hexStr.substring(j-2, j),16);
497 }
498 }
499
500 return sidBytes;
501 }
502 } 195 }