PageRenderTime 187ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/bsh/StringUtil.java

#
Java | 120 lines | 59 code | 11 blank | 50 comment | 10 complexity | 5213a0bd5f70cab7fcaa133f33f63031 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. // Clean this up later...
  73. public static String methodString(String name, Class[] types)
  74. {
  75. StringBuffer sb = new StringBuffer(name + "(");
  76. if(types.length > 0)
  77. sb.append(" ");
  78. for(int i=0; i<(types.length - 1); i++)
  79. {
  80. Class c = types[i];
  81. sb.append(((c == null) ? "null" : c.getName()) + ", ");
  82. }
  83. if(types.length > 0)
  84. {
  85. Class c = types[types.length - 1];
  86. sb.append(((c == null) ? "null" : c.getName()));
  87. sb.append(" ");
  88. }
  89. sb.append(")");
  90. return sb.toString();
  91. }
  92. /**
  93. Split a filename into dirName, baseName
  94. @return String [] { dirName, baseName }
  95. public String [] splitFileName( String fileName )
  96. {
  97. String dirName, baseName;
  98. int i = fileName.lastIndexOf( File.separator );
  99. if ( i != -1 ) {
  100. dirName = fileName.substring(0, i);
  101. baseName = fileName.substring(i+1);
  102. } else
  103. baseName = fileName;
  104. return new String[] { dirName, baseName };
  105. }
  106. */
  107. }