001 /*****************************************************************************
002 * *
003 * This file is part of the BeanShell Java Scripting distribution. *
004 * Documentation and updates may be found at http://www.beanshell.org/ *
005 * *
006 * Sun Public License Notice: *
007 * *
008 * The contents of this file are subject to the Sun Public License Version *
009 * 1.0 (the "License"); you may not use this file except in compliance with *
010 * the License. A copy of the License is available at http://www.sun.com *
011 * *
012 * The Original Code is BeanShell. The Initial Developer of the Original *
013 * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
014 * (C) 2000. All Rights Reserved. *
015 * *
016 * GNU Public License Notice: *
017 * *
018 * Alternatively, the contents of this file may be used under the terms of *
019 * the GNU Lesser General Public License (the "LGPL"), in which case the *
020 * provisions of LGPL are applicable instead of those above. If you wish to *
021 * allow use of your version of this file only under the terms of the LGPL *
022 * and not to allow others to use your version of this file under the SPL, *
023 * indicate your decision by deleting the provisions above and replace *
024 * them with the notice and other provisions required by the LGPL. If you *
025 * do not delete the provisions above, a recipient may use your version of *
026 * this file under either the SPL or the LGPL. *
027 * *
028 * Patrick Niemeyer (pat@pat.net) *
029 * Author of Learning Java, O'Reilly & Associates *
030 * http://www.pat.net/~pat/ *
031 * *
032 *****************************************************************************/
033
034 package bsh;
035
036 import java.util.*;
037
038 public class StringUtil {
039
040 public static String [] split( String s, String delim) {
041 Vector v = new Vector();
042 StringTokenizer st = new StringTokenizer(s, delim);
043 while ( st.hasMoreTokens() )
044 v.addElement( st.nextToken() );
045 String [] sa = new String [ v.size() ];
046 v.copyInto( sa );
047 return sa;
048 }
049
050 public static String [] bubbleSort( String [] in ) {
051 Vector v = new Vector();
052 for(int i=0; i<in.length; i++)
053 v.addElement(in[i]);
054
055 int n = v.size();
056 boolean swap = true;
057 while ( swap ) {
058 swap = false;
059 for(int i=0; i<(n-1); i++)
060 if ( ((String)v.elementAt(i)).compareTo(
061 ((String)v.elementAt(i+1)) ) > 0 ) {
062 String tmp = (String)v.elementAt(i+1);
063 v.removeElementAt( i+1 );
064 v.insertElementAt( tmp, i );
065 swap = true;
066 }
067 }
068
069 String [] out = new String [ n ];
070 v.copyInto(out);
071 return out;
072 }
073
074
075 public static String maxCommonPrefix( String one, String two ) {
076 int i=0;
077 while( one.regionMatches( 0, two, 0, i ) )
078 i++;
079 return one.substring(0, i-1);
080 }
081
082 public static String methodString(String name, Class[] types)
083 {
084 StringBuffer sb = new StringBuffer(name + "(");
085 if ( types.length > 0 )
086 sb.append(" ");
087 for( int i=0; i<types.length; i++ )
088 {
089 Class c = types[i];
090 sb.append( ( (c == null) ? "null" : c.getName() )
091 + ( i < (types.length-1) ? ", " : " " ) );
092 }
093 sb.append(")");
094 return sb.toString();
095 }
096
097 /**
098 Split a filename into dirName, baseName
099 @return String [] { dirName, baseName }
100 public String [] splitFileName( String fileName )
101 {
102 String dirName, baseName;
103 int i = fileName.lastIndexOf( File.separator );
104 if ( i != -1 ) {
105 dirName = fileName.substring(0, i);
106 baseName = fileName.substring(i+1);
107 } else
108 baseName = fileName;
109
110 return new String[] { dirName, baseName };
111 }
112
113 */
114
115 /**
116 Hack - The real method is in Reflect.java which is not public.
117 */
118 public static String normalizeClassName( Class type )
119 {
120 return Reflect.normalizeClassName( type );
121 }
122 }