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: *****************************************************************************/package bsh;
033:
034: import java.util.*;
035:
036: public class StringUtil {
037:
038: public static String[] split(String s, String delim) {
039: Vector v = new Vector();
040: StringTokenizer st = new StringTokenizer(s, delim);
041: while (st.hasMoreTokens())
042: v.addElement(st.nextToken());
043: String[] sa = new String[v.size()];
044: v.copyInto(sa);
045: return sa;
046: }
047:
048: public static String[] bubbleSort(String[] in) {
049: Vector v = new Vector();
050: for (int i = 0; i < in.length; i++)
051: v.addElement(in[i]);
052:
053: int n = v.size();
054: boolean swap = true;
055: while (swap) {
056: swap = false;
057: for (int i = 0; i < (n - 1); i++)
058: if (((String) v.elementAt(i)).compareTo(((String) v
059: .elementAt(i + 1))) > 0) {
060: String tmp = (String) v.elementAt(i + 1);
061: v.removeElementAt(i + 1);
062: v.insertElementAt(tmp, i);
063: swap = true;
064: }
065: }
066:
067: String[] out = new String[n];
068: v.copyInto(out);
069: return out;
070: }
071:
072: public static String maxCommonPrefix(String one, String two) {
073: int i = 0;
074: while (one.regionMatches(0, two, 0, i))
075: i++;
076: return one.substring(0, i - 1);
077: }
078:
079: public static String methodString(String name, Class[] types) {
080: StringBuffer sb = new StringBuffer(name + "(");
081: if (types.length > 0)
082: sb.append(" ");
083: for (int i = 0; i < types.length; i++) {
084: Class c = types[i];
085: sb.append(((c == null) ? "null" : c.getName())
086: + (i < (types.length - 1) ? ", " : " "));
087: }
088: sb.append(")");
089: return sb.toString();
090: }
091:
092: /**
093: Split a filename into dirName, baseName
094: @return String [] { dirName, baseName }
095: public String [] splitFileName( String fileName )
096: {
097: String dirName, baseName;
098: int i = fileName.lastIndexOf( File.separator );
099: if ( i != -1 ) {
100: dirName = fileName.substring(0, i);
101: baseName = fileName.substring(i+1);
102: } else
103: baseName = fileName;
104:
105: return new String[] { dirName, baseName };
106: }
107:
108: */
109:
110: /**
111: Hack - The real method is in Reflect.java which is not public.
112: */
113: public static String normalizeClassName(Class type) {
114: return Reflect.normalizeClassName(type);
115: }
116: }
|