001    /* Copyright 2000, 2001, Compaq Computer Corporation */
002    
003    package javafe.filespace;
004    
005    
006    /** Misc. utility functions on Strings */
007    
008    public final class StringUtil {
009    
010        /** Remove leading and trailing whitespace (just spaces for now): */
011        //@ requires s != null;
012        public static String trim_whitespace(String s) {
013            while (s.length()>0 && s.charAt(0)==' ')
014                s = s.substring(1,s.length());
015    
016            while (s.length()>0 && s.charAt(s.length()-1)==' ')
017                s = s.substring(0,s.length()-1);
018    
019            return s;
020        }
021    
022            
023        /** Count the number of times a given character occurs in a String: */
024        //@ requires s != null;
025        //@ ensures \result>=0;
026        public static int countChar(String s, char c) {
027            int count = 0;
028            int start = 0;
029    
030            while ((start = s.indexOf(c, start)+1) != 0)
031                count++;
032    
033            return count;
034        }
035    
036    
037        /**
038         * Print an array of Strings on System.out, one string per
039         * line.  Prints "<null>" if list is null.
040         */
041        //@ requires list != null;
042        public static void printList(String[] list) {
043            if (list == null) {
044                System.out.println("<null>");
045                return;
046            }
047            
048            for (int i=0; i<list.length; i++) 
049                System.out.println(list[i]);
050        }
051    
052    
053        /**
054         * Parse a (possibly empty) separator-separated list into an array of
055         *  Strings:
056         */
057        //@ ensures \nonnullelements(\result);
058        public static String[] parseList(/*@ non_null @*/ String s, char separator) {
059            // Handle empty list case:
060            if (s.equals(""))
061                return new String[0];
062    
063            int items = countChar(s, separator)+1;
064            String[] list = new String[items];
065    
066            int start = 0;
067            //@ loop_invariant (\forall int j; 0<=j && j<i ==> list[j] != null);
068            for (int i = 0; i < items-1 ; i++) {
069    
070                int nextSep = s.indexOf(separator, start);
071                //@ assume nextSep != -1;
072                list[i] = s.substring(start, nextSep);
073                start = nextSep + 1;
074            }
075    
076            list[items-1] = s.substring(start);
077    
078            return list;
079        }
080    
081    
082        /** A simple test driver */
083        //@ requires \nonnullelements(args);
084        public static void main(String[] args) {
085            if (args.length != 1) {
086                System.out.println("StringUtil: usage: <cmd> <teststring>");
087                return;
088            }
089            System.out.println("Testing using '" + args[0] + "':");
090            System.out.println();
091    
092            // Test trim_whitespace:
093            System.out.println("Removing whitespace yields '" +
094                    trim_whitespace(args[0])+ "'");
095            System.out.println();
096    
097            // Test countChar:
098            System.out.println("# of commas occuring = " +
099                countChar(args[0], ','));
100            System.out.println();
101    
102            // Test countChar:
103            System.out.println("List elements are: ");
104            printList(parseList(args[0], ','));
105            System.out.println("(EOL)");
106        }
107    }