KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > util > StringUtil


1 // ========================================================================
2
// $Id: StringUtil.java,v 1.15 2004/10/20 12:46:24 gregwilkins Exp $
3
// Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.util;
17
18 // ====================================================================
19
/** Fast String Utilities.
20  *
21  * These string utilities provide both conveniance methods and
22  * performance improvements over most standard library versions. The
23  * main aim of the optimizations is to avoid object creation unless
24  * absolutely required.
25  *
26  * @version $Revision: 1.15 $
27  * @author Greg Wilkins (gregw)
28  */

29 public class StringUtil
30 {
31     public static final String JavaDoc __LINE_SEPARATOR=
32         System.getProperty("line.separator","\n");
33     
34     public final static String JavaDoc __ISO_8859_1;
35     static
36     {
37         String JavaDoc iso=System.getProperty("ISO_8859_1");
38         if (iso!=null)
39             __ISO_8859_1=iso;
40         else
41         {
42             try{
43                 new String JavaDoc(new byte[]{(byte)20},"ISO-8859-1");
44                 iso="ISO-8859-1";
45             }
46             catch(java.io.UnsupportedEncodingException JavaDoc e)
47             {
48                 iso="ISO8859_1";
49             }
50             __ISO_8859_1=iso;
51         }
52     }
53     
54     public final static String JavaDoc __UTF_8="UTF-8";
55     
56     private static char[] lowercases = {
57           '\000','\001','\002','\003','\004','\005','\006','\007',
58           '\010','\011','\012','\013','\014','\015','\016','\017',
59           '\020','\021','\022','\023','\024','\025','\026','\027',
60           '\030','\031','\032','\033','\034','\035','\036','\037',
61           '\040','\041','\042','\043','\044','\045','\046','\047',
62           '\050','\051','\052','\053','\054','\055','\056','\057',
63           '\060','\061','\062','\063','\064','\065','\066','\067',
64           '\070','\071','\072','\073','\074','\075','\076','\077',
65           '\100','\141','\142','\143','\144','\145','\146','\147',
66           '\150','\151','\152','\153','\154','\155','\156','\157',
67           '\160','\161','\162','\163','\164','\165','\166','\167',
68           '\170','\171','\172','\133','\134','\135','\136','\137',
69           '\140','\141','\142','\143','\144','\145','\146','\147',
70           '\150','\151','\152','\153','\154','\155','\156','\157',
71           '\160','\161','\162','\163','\164','\165','\166','\167',
72           '\170','\171','\172','\173','\174','\175','\176','\177' };
73
74     /* ------------------------------------------------------------ */
75     /**
76      * fast lower case conversion. Only works on ascii (not unicode)
77      * @param s the string to convert
78      * @return a lower case version of s
79      */

80     public static String JavaDoc asciiToLowerCase(String JavaDoc s)
81     {
82         char[] c = null;
83         int i=s.length();
84
85         // look for first conversion
86
while (i-->0)
87         {
88             char c1=s.charAt(i);
89             if (c1<=127)
90             {
91                 char c2=lowercases[c1];
92                 if (c1!=c2)
93                 {
94                     c=s.toCharArray();
95                     c[i]=c2;
96                     break;
97                 }
98             }
99         }
100
101         while (i-->0)
102         {
103             if(c[i]<=127)
104                 c[i] = lowercases[c[i]];
105         }
106         
107         return c==null?s:new String JavaDoc(c);
108     }
109
110
111     /* ------------------------------------------------------------ */
112     public static boolean startsWithIgnoreCase(String JavaDoc s,String JavaDoc w)
113     {
114         if (w==null)
115             return true;
116         
117         if (s==null || s.length()<w.length())
118             return false;
119         
120         for (int i=0;i<w.length();i++)
121         {
122             char c1=s.charAt(i);
123             char c2=w.charAt(i);
124             if (c1!=c2)
125             {
126                 if (c1<=127)
127                     c1=lowercases[c1];
128                 if (c2<=127)
129                     c2=lowercases[c2];
130                 if (c1!=c2)
131                     return false;
132             }
133         }
134         return true;
135     }
136     
137     /* ------------------------------------------------------------ */
138     public static boolean endsWithIgnoreCase(String JavaDoc s,String JavaDoc w)
139     {
140         if (w==null)
141             return true;
142         
143         int sl=s.length();
144         int wl=w.length();
145         
146         if (s==null || sl<wl)
147             return false;
148         
149         for (int i=wl;i-->0;)
150         {
151             char c1=s.charAt(--sl);
152             char c2=w.charAt(i);
153             if (c1!=c2)
154             {
155                 if (c1<=127)
156                     c1=lowercases[c1];
157                 if (c2<=127)
158                     c2=lowercases[c2];
159                 if (c1!=c2)
160                     return false;
161             }
162         }
163         return true;
164     }
165     
166     /* ------------------------------------------------------------ */
167     /**
168      * returns the next index of a character from the chars string
169      */

170     public static int indexFrom(String JavaDoc s,String JavaDoc chars)
171     {
172         for (int i=0;i<s.length();i++)
173            if (chars.indexOf(s.charAt(i))>=0)
174               return i;
175         return -1;
176     }
177     
178     /* ------------------------------------------------------------ */
179     /**
180      * replace substrings within string.
181      */

182     public static String JavaDoc replace(String JavaDoc s, String JavaDoc sub, String JavaDoc with)
183     {
184         int c=0;
185         int i=s.indexOf(sub,c);
186         if (i == -1)
187             return s;
188     
189         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(s.length()+with.length());
190
191         synchronized(buf)
192         {
193             do
194             {
195                 buf.append(s.substring(c,i));
196                 buf.append(with);
197                 c=i+sub.length();
198             } while ((i=s.indexOf(sub,c))!=-1);
199             
200             if (c<s.length())
201                 buf.append(s.substring(c,s.length()));
202             
203             return buf.toString();
204         }
205     }
206
207     /* ------------------------------------------------------------ */
208     /** Remove single or double quotes.
209      */

210     public static String JavaDoc unquote(String JavaDoc s)
211     {
212         if ((s.startsWith("\"") && s.endsWith("\"")) ||
213             (s.startsWith("'") && s.endsWith("'")))
214             s=s.substring(1,s.length()-1);
215         return s;
216     }
217
218
219     /* ------------------------------------------------------------ */
220     /** Append substring to StringBuffer
221      * @param buf StringBuffer 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(StringBuffer JavaDoc buf,
227                               String JavaDoc 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     public static void append(StringBuffer JavaDoc buf,byte b,int base)
246     {
247         int bi=0xff&b;
248         int c='0'+(bi/base)%base;
249         if (c>'9')
250             c= 'a'+(c-'0'-10);
251         buf.append((char)c);
252         c='0'+bi%base;
253         if (c>'9')
254             c= 'a'+(c-'0'-10);
255         buf.append((char)c);
256     }
257     
258     /* ------------------------------------------------------------ */
259     public static void append2digits(StringBuffer JavaDoc buf,int i)
260     {
261         if (i<100)
262         {
263             buf.append((char)(i/10+'0'));
264             buf.append((char)(i%10+'0'));
265         }
266     }
267     
268     /* ------------------------------------------------------------ */
269     /** Return a non null string.
270      * @param s String
271      * @return The string passed in or empty string if it is null.
272      */

273     public static String JavaDoc nonNull(String JavaDoc s)
274     {
275         if (s==null)
276             return "";
277         return s;
278     }
279     
280     /* ------------------------------------------------------------ */
281     public static boolean equals(String JavaDoc s,char[] buf, int offset, int length)
282     {
283         if (s.length()!=length)
284             return false;
285         for (int i=0;i<length;i++)
286             if (buf[offset+i]!=s.charAt(i))
287                 return false;
288         return true;
289     }
290     
291 }
292
Popular Tags