1
// ========================================================================
2
// Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
3
// ------------------------------------------------------------------------
4
// Licensed under the Apache License, Version 2.0 (the "License");
5
// you may not use this file except in compliance with the License.
6
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
8
// Unless required by applicable law or agreed to in writing, software
9
// distributed under the License is distributed on an "AS IS" BASIS,
10
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
// See the License for the specific language governing permissions and
12
// limitations under the License.
13
// ========================================================================
15
package org.mortbay.util;
17
import java.io.UnsupportedEncodingException;
19
// ====================================================================
20
/** Fast String Utilities.
22
* These string utilities provide both conveniance methods and
23
* performance improvements over most standard library versions. The
24
* main aim of the optimizations is to avoid object creation unless
25
* absolutely required.
27
* @author Greg Wilkins (gregw)
29
public class StringUtil
31
public static final String CRLF="\015\012";
32
public static final String __LINE_SEPARATOR=
33
System.getProperty("line.separator","\n");
35
public static final String __ISO_8859_1;
38
String iso=System.getProperty("ISO_8859_1");
42
new String(new byte[]{(byte)20},"ISO-8859-1");
45
catch(java.io.UnsupportedEncodingException e)
53
public final static String __UTF8="UTF-8";
54
public final static String __UTF8Alt="UTF8";
55
public final static String __UTF16="UTF-16";
58
private static char[] lowercases = {
59
'\000','\001','\002','\003','\004','\005','\006','\007',
60
'\010','\011','\012','\013','\014','\015','\016','\017',
61
'\020','\021','\022','\023','\024','\025','\026','\027',
62
'\030','\031','\032','\033','\034','\035','\036','\037',
63
'\040','\041','\042','\043','\044','\045','\046','\047',
64
'\050','\051','\052','\053','\054','\055','\056','\057',
65
'\060','\061','\062','\063','\064','\065','\066','\067',
66
'\070','\071','\072','\073','\074','\075','\076','\077',
67
'\100','\141','\142','\143','\144','\145','\146','\147',
68
'\150','\151','\152','\153','\154','\155','\156','\157',
69
'\160','\161','\162','\163','\164','\165','\166','\167',
70
'\170','\171','\172','\133','\134','\135','\136','\137',
71
'\140','\141','\142','\143','\144','\145','\146','\147',
72
'\150','\151','\152','\153','\154','\155','\156','\157',
73
'\160','\161','\162','\163','\164','\165','\166','\167',
74
'\170','\171','\172','\173','\174','\175','\176','\177' };
76
/* ------------------------------------------------------------ */
78
* fast lower case conversion. Only works on ascii (not unicode)
79
* @param s the string to convert
80
* @return a lower case version of s
82
public static String asciiToLowerCase(String s)
87
// look for first conversion
93
char c2=lowercases[c1];
106
c[i] = lowercases[c[i]];
109
return c==null?s:new String(c);
113
/* ------------------------------------------------------------ */
114
public static boolean startsWithIgnoreCase(String s,String w)
119
if (s==null || s.length()<w.length())
122
for (int i=0;i<w.length();i++)
139
/* ------------------------------------------------------------ */
140
public static boolean endsWithIgnoreCase(String s,String w)
154
for (int i=wl;i-->0;)
156
char c1=s.charAt(--sl);
171
/* ------------------------------------------------------------ */
173
* returns the next index of a character from the chars string
175
public static int indexFrom(String s,String chars)
177
for (int i=0;i<s.length();i++)
178
if (chars.indexOf(s.charAt(i))>=0)
183
/* ------------------------------------------------------------ */
185
* replace substrings within string.
187
public static String replace(String s, String sub, String with)
190
int i=s.indexOf(sub,c);
194
StringBuffer buf = new StringBuffer(s.length()+with.length());
200
buf.append(s.substring(c,i));
203
} while ((i=s.indexOf(sub,c))!=-1);
206
buf.append(s.substring(c,s.length()));
208
return buf.toString();
213
/* ------------------------------------------------------------ */
214
/** Remove single or double quotes.
216
public static String unquote(String s)
218
return QuotedStringTokenizer.unquote(s);
222
/* ------------------------------------------------------------ */
223
/** Append substring to StringBuffer
224
* @param buf StringBuffer to append to
225
* @param s String to append from
226
* @param offset The offset of the substring
227
* @param length The length of the substring
229
public static void append(StringBuffer buf,
236
int end=offset+length;
237
for (int i=offset; i<end;i++)
241
buf.append(s.charAt(i));
247
/* ------------------------------------------------------------ */
252
public static void append(StringBuffer buf,byte b,int base)
255
int c='0'+(bi/base)%base;
266
/* ------------------------------------------------------------ */
267
public static void append2digits(StringBuffer buf,int i)
271
buf.append((char)(i/10+'0'));
272
buf.append((char)(i%10+'0'));
276
/* ------------------------------------------------------------ */
277
/** Return a non null string.
279
* @return The string passed in or empty string if it is null.
281
public static String nonNull(String s)
288
/* ------------------------------------------------------------ */
289
public static boolean equals(String s,char[] buf, int offset, int length)
291
if (s.length()!=length)
293
for (int i=0;i<length;i++)
294
if (buf[offset+i]!=s.charAt(i))
299
/* ------------------------------------------------------------ */
300
public static String toUTF8String(byte[] b,int offset,int length)
306
Utf8StringBuffer buffer = new Utf8StringBuffer(length);
307
buffer.append(b,offset,length);
308
return buffer.toString();
311
return new String(b,offset,length,__UTF8);
313
catch (UnsupportedEncodingException e)
320
/* ------------------------------------------------------------ */
321
public static String toString(byte[] b,int offset,int length,String charset)
323
if (charset==null || StringUtil.isUTF8(charset))
324
return toUTF8String(b,offset,length);
328
return new String(b,offset,length,charset);
330
catch (UnsupportedEncodingException e)
338
/* ------------------------------------------------------------ */
339
public static boolean isUTF8(String charset)
341
return charset==__UTF8||__UTF8.equalsIgnoreCase(charset)||__UTF8Alt.equalsIgnoreCase(charset);
345
/* ------------------------------------------------------------ */
346
public static String printable(String name)
350
StringBuffer buf = new StringBuffer(name.length());
351
for (int i=0;i<name.length();i++)
353
char c=name.charAt(i);
354
if (!Character.isISOControl(c))
357
return buf.toString();