Mercurial Hosting > luan
view src/org/eclipse/jetty/util/StringUtil.java @ 1027:6d17a257b03f
remove Buffer.asImmutableBuffer()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 02 Nov 2016 23:58:37 -0600 |
parents | 6939226e0ac4 |
children |
line wrap: on
line source
// // ======================================================================== // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. // ------------------------------------------------------------------------ // All rights reserved. This program and the accompanying materials // are made available under the terms of the Eclipse Public License v1.0 // and Apache License v2.0 which accompanies this distribution. // // The Eclipse Public License is available at // http://www.eclipse.org/legal/epl-v10.html // // The Apache License v2.0 is available at // http://www.opensource.org/licenses/apache2.0.php // // You may elect to redistribute this code under either of these licenses. // ======================================================================== // package org.eclipse.jetty.util; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** Fast String Utilities. * * These string utilities provide both conveniance methods and * performance improvements over most standard library versions. The * main aim of the optimizations is to avoid object creation unless * absolutely required. * * */ public final class StringUtil { private static final Logger LOG = LoggerFactory.getLogger(StringUtil.class); public static final String ALL_INTERFACES="0.0.0.0"; public static final String __ISO_8859_1="ISO-8859-1"; public final static String __UTF8="UTF-8"; public final static String __UTF16="UTF-16"; public final static Charset __UTF8_CHARSET; public final static Charset __ISO_8859_1_CHARSET; static { __UTF8_CHARSET=Charset.forName(__UTF8); __ISO_8859_1_CHARSET=Charset.forName(__ISO_8859_1); } private static char[] lowercases = { '\000','\001','\002','\003','\004','\005','\006','\007', '\010','\011','\012','\013','\014','\015','\016','\017', '\020','\021','\022','\023','\024','\025','\026','\027', '\030','\031','\032','\033','\034','\035','\036','\037', '\040','\041','\042','\043','\044','\045','\046','\047', '\050','\051','\052','\053','\054','\055','\056','\057', '\060','\061','\062','\063','\064','\065','\066','\067', '\070','\071','\072','\073','\074','\075','\076','\077', '\100','\141','\142','\143','\144','\145','\146','\147', '\150','\151','\152','\153','\154','\155','\156','\157', '\160','\161','\162','\163','\164','\165','\166','\167', '\170','\171','\172','\133','\134','\135','\136','\137', '\140','\141','\142','\143','\144','\145','\146','\147', '\150','\151','\152','\153','\154','\155','\156','\157', '\160','\161','\162','\163','\164','\165','\166','\167', '\170','\171','\172','\173','\174','\175','\176','\177' }; /* ------------------------------------------------------------ */ /** * fast lower case conversion. Only works on ascii (not unicode) * @param s the string to convert * @return a lower case version of s */ public static String asciiToLowerCase(String s) { char[] c = null; int i=s.length(); // look for first conversion while (i-->0) { char c1=s.charAt(i); if (c1<=127) { char c2=lowercases[c1]; if (c1!=c2) { c=s.toCharArray(); c[i]=c2; break; } } } while (i-->0) { if(c[i]<=127) c[i] = lowercases[c[i]]; } return c==null?s:new String(c); } /* ------------------------------------------------------------ */ /** * replace substrings within string. */ public static String replace(String s, String sub, String with) { int c=0; int i=s.indexOf(sub,c); if (i == -1) return s; StringBuilder buf = new StringBuilder(s.length()+with.length()); do { buf.append(s.substring(c,i)); buf.append(with); c=i+sub.length(); } while ((i=s.indexOf(sub,c))!=-1); if (c<s.length()) buf.append(s.substring(c,s.length())); return buf.toString(); } /* ------------------------------------------------------------ */ /** * append hex digit * */ public static void append(StringBuilder buf,byte b,int base) { int bi=0xff&b; int c='0'+(bi/base)%base; if (c>'9') c= 'a'+(c-'0'-10); buf.append((char)c); c='0'+bi%base; if (c>'9') c= 'a'+(c-'0'-10); buf.append((char)c); } public static void append2digits(StringBuilder buf,int i) { if (i<100) { buf.append((char)(i/10+'0')); buf.append((char)(i%10+'0')); } } public static String toString(byte[] b,int offset,int length,String charset) { try { return new String(b,offset,length,charset); } catch (UnsupportedEncodingException e) { throw new IllegalArgumentException(e); } } public static boolean isUTF8(String charset) { return __UTF8.equalsIgnoreCase(charset)||"UTF8".equalsIgnoreCase(charset); } public static byte[] getBytes(String s) { try { return s.getBytes(__ISO_8859_1); } catch(Exception e) { LOG.warn("",e); return s.getBytes(); } } }