/jEdit/tags/jedit-4-2-pre11/bsh/StringUtil.java

# · Java · 122 lines · 58 code · 12 blank · 52 comment · 8 complexity · d00010b696a0aa0a69fb82d163b0a7c1 MD5 · raw file

  1. /*****************************************************************************
  2. * *
  3. * This file is part of the BeanShell Java Scripting distribution. *
  4. * Documentation and updates may be found at http://www.beanshell.org/ *
  5. * *
  6. * Sun Public License Notice: *
  7. * *
  8. * The contents of this file are subject to the Sun Public License Version *
  9. * 1.0 (the "License"); you may not use this file except in compliance with *
  10. * the License. A copy of the License is available at http://www.sun.com *
  11. * *
  12. * The Original Code is BeanShell. The Initial Developer of the Original *
  13. * Code is Pat Niemeyer. Portions created by Pat Niemeyer are Copyright *
  14. * (C) 2000. All Rights Reserved. *
  15. * *
  16. * GNU Public License Notice: *
  17. * *
  18. * Alternatively, the contents of this file may be used under the terms of *
  19. * the GNU Lesser General Public License (the "LGPL"), in which case the *
  20. * provisions of LGPL are applicable instead of those above. If you wish to *
  21. * allow use of your version of this file only under the terms of the LGPL *
  22. * and not to allow others to use your version of this file under the SPL, *
  23. * indicate your decision by deleting the provisions above and replace *
  24. * them with the notice and other provisions required by the LGPL. If you *
  25. * do not delete the provisions above, a recipient may use your version of *
  26. * this file under either the SPL or the LGPL. *
  27. * *
  28. * Patrick Niemeyer (pat@pat.net) *
  29. * Author of Learning Java, O'Reilly & Associates *
  30. * http://www.pat.net/~pat/ *
  31. * *
  32. *****************************************************************************/
  33. package bsh;
  34. import java.util.*;
  35. public class StringUtil {
  36. public static String [] split( String s, String delim) {
  37. Vector v = new Vector();
  38. StringTokenizer st = new StringTokenizer(s, delim);
  39. while ( st.hasMoreTokens() )
  40. v.addElement( st.nextToken() );
  41. String [] sa = new String [ v.size() ];
  42. v.copyInto( sa );
  43. return sa;
  44. }
  45. public static String [] bubbleSort( String [] in ) {
  46. Vector v = new Vector();
  47. for(int i=0; i<in.length; i++)
  48. v.addElement(in[i]);
  49. int n = v.size();
  50. boolean swap = true;
  51. while ( swap ) {
  52. swap = false;
  53. for(int i=0; i<(n-1); i++)
  54. if ( ((String)v.elementAt(i)).compareTo(
  55. ((String)v.elementAt(i+1)) ) > 0 ) {
  56. String tmp = (String)v.elementAt(i+1);
  57. v.removeElementAt( i+1 );
  58. v.insertElementAt( tmp, i );
  59. swap = true;
  60. }
  61. }
  62. String [] out = new String [ n ];
  63. v.copyInto(out);
  64. return out;
  65. }
  66. public static String maxCommonPrefix( String one, String two ) {
  67. int i=0;
  68. while( one.regionMatches( 0, two, 0, i ) )
  69. i++;
  70. return one.substring(0, i-1);
  71. }
  72. public static String methodString(String name, Class[] types)
  73. {
  74. StringBuffer sb = new StringBuffer(name + "(");
  75. if ( types.length > 0 )
  76. sb.append(" ");
  77. for( int i=0; i<types.length; i++ )
  78. {
  79. Class c = types[i];
  80. sb.append( ( (c == null) ? "null" : c.getName() )
  81. + ( i < (types.length-1) ? ", " : " " ) );
  82. }
  83. sb.append(")");
  84. return sb.toString();
  85. }
  86. /**
  87. Split a filename into dirName, baseName
  88. @return String [] { dirName, baseName }
  89. public String [] splitFileName( String fileName )
  90. {
  91. String dirName, baseName;
  92. int i = fileName.lastIndexOf( File.separator );
  93. if ( i != -1 ) {
  94. dirName = fileName.substring(0, i);
  95. baseName = fileName.substring(i+1);
  96. } else
  97. baseName = fileName;
  98. return new String[] { dirName, baseName };
  99. }
  100. */
  101. /**
  102. Hack - The real method is in Reflect.java which is not public.
  103. */
  104. public static String normalizeClassName( Class type )
  105. {
  106. return Reflect.normalizeClassName( type );
  107. }
  108. }