This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: gcj minor bug


Hello, for those interested.

> This is a well known gcj bug.  The code in StringTokenizer is actually
> fine, it is the compiler that has the problem.  I believe this bug is
> fixed in cvs.
Have you ever tried it out? I did a cvs check out, and the code in the
cvs breaks on 2 points:
1) nullpointer exceptions are raised because you can pass in null values
into the constructor
2) the delimiter algo is totally screwed up. I rewrote the thing after I
kept getting stupid bugs with ant 

the code is attached do with it what you want, if you need another
rewrite just mail me. i've included a very quick test case (wrong.java)
which all turn out fine.

> We try to make it easy.  For simple patches, just send the patch, a
> ChangeLog entry, and an explanation to <java-patches@gcc.gnu.org>.
well there must be someone on this list who has write access???

the code can probably be made more efficient, but at least it's not
buggy.
public class wrong {
	public static void main(String args[]) {
		wrong w = new wrong();
		w.test1();
		w.test2();
		w.test3();
		w.test4();
		w.test5();
		w.test6();	
		w.test7();
		w.test8();
	}

	void test8() {
		String s = "   aaa           aaaaaa aaaaaaa aaaaaA";
		StringTokenizer t = new StringTokenizer(s,"a",true);
		System.out.println("\nHas more tokens?  "+t.hasMoreTokens());
		System.out.println("Number of tokens? "+t.countTokens());
		for (;t.hasMoreTokens();System.out.print(t.nextToken()+"/"));
	}
	void test7() {
		String s = "   aaa           aaaaaa aaaaaaa aaaaaA";
		StringTokenizer t = new StringTokenizer(s);
		System.out.println("\nHas more tokens? "+t.hasMoreTokens());
		System.out.println("Number of tokens?"+t.countTokens());
		for (;t.hasMoreTokens();System.out.print(t.nextToken()+"/"));
	}
	void test6() {
		String s = "   aaa,aaaaaa,aaaaaaa,aaaaaA";
		StringTokenizer t = new StringTokenizer(s,",");
		System.out.println("\nHas more tokens? "+t.hasMoreTokens());
		System.out.println("Number of tokens?"+t.countTokens());
		for (;t.hasMoreTokens();System.out.print(t.nextToken()+"/"));
	}
	
	void test1() {
		String s = "aaa,aaaaaa,aaaaaaa,aaaaaA";
		StringTokenizer t = new StringTokenizer(s,",");
		System.out.println("\nHas more tokens? "+t.hasMoreTokens());
		System.out.println("Number of tokens?"+t.countTokens());
		for (;t.hasMoreTokens();System.out.print(t.nextToken()+"/"));
	}
	void test2() {
		String s = "aaaaaaaaaaaaaaaaaaaaaA";
		StringTokenizer t = new StringTokenizer(s,"a",true);
		System.out.println("\nHas more tokens? "+t.hasMoreTokens());
		System.out.println("Number of tokens?"+t.countTokens());
		for (;t.hasMoreTokens();System.out.println(t.nextToken()+"/"));
	}
	void test3() {
		String s = "aaaaaaaaaaaaaaa";
		StringTokenizer t = new StringTokenizer(s,"a");
		System.out.println("\nHas more tokens? "+t.hasMoreTokens());
		System.out.println("Number of tokens?"+t.countTokens());
		for (;t.hasMoreTokens();System.out.print(t.nextToken()+"/"));
	}

	void test4() {
		String s = "aaaaaaaaaaaaaaa";
		StringTokenizer t = new StringTokenizer(s);
		System.out.println("\nHas more tokens? "+t.hasMoreTokens());
		System.out.println("Number of tokens?"+t.countTokens());
		for (;t.hasMoreTokens();System.out.print(t.nextToken()+"/"));
	}
	void test5() {
		String s = null;
		StringTokenizer t = new StringTokenizer(s);
		System.out.println("\nHas more tokens? "+t.hasMoreTokens());
		System.out.println("Number of tokens?"+t.countTokens());
		for (;t.hasMoreTokens();System.out.print(t.nextToken()+"/"));
	}
}
/**
 *
 * @author  bhun
 */
public class StringTokenizer implements java.util.Enumeration {
    /** Default delimiters.
     */
    private final static String DEFAULT = " \t\n\r\f";
    
    /** Buffer for inputstring.
     */
    private StringBuffer input;
    
    /** Delimiters.
     */
    private String delimiters = DEFAULT;
    
    /** By default delimiters are not tokens.
     */
    private boolean delimiterIsToken = false;
    
    /** Tokenize string according to delimiters " \t\n\r\f".
     */
    public StringTokenizer(String a) {
        input = new StringBuffer(a==null?"":a);
    }
    
    /* Specify custom delimiters to tokenize a string with.
     * @param a String to tokenize
     * @param b Delimiters
     */
    public StringTokenizer(String a, String b) {
        this(a);
        delimiters = b;
    }
    
    /** Specify custom delimiters and wheter they are considered tokens
     * @param a string to tokenize
     * @param b delimiters
     * @param c true is delimiters are tokens
     */
    public StringTokenizer(String a, String b, boolean c) {
        this(a,b);
        delimiterIsToken=c;
    }
    /** count number of tokens than can be returned till the end of string.
     * @return number of tokens left.
     */
    public int countTokens() {
        int i = 0; // start at index 0
        int n = 0; // zero tokens at start
        if (input.length() > 0) {
            while (i<input.length()) {
                if (delimiters.indexOf(input.charAt(i))==-1) {
                    n++;
                    while (i<input.length()&&delimiters.indexOf(input.charAt(i))==-1) i++;
                } else {
                    if (delimiterIsToken) n++; i++;
                }
            }
        }
        return n;
    }
    
    /** @return true more tokens can be returned, false if not
     */
    public boolean hasMoreElements() {
        boolean hasMoreElements = false;
        if (input.length() > 0) {
            if (!delimiterIsToken) {
                found: for (int i = 0 ; i<input.length();i++) {
                    if (delimiters.indexOf(input.charAt(i))==-1) {
                        hasMoreElements = true;
                        break found;
                    }
                }
            } else {
                hasMoreElements = true;
            }
        }
        return hasMoreElements;
    }
    
    /** @return true more tokens can be returned, false if not
     */
    public boolean hasMoreTokens() {
        return hasMoreElements();
    }
    
    /** @return next token
     */
    public Object nextElement() {
        return nextToken();
    }
    
    /** @return next token
     */
    public String nextToken() {
        int n = 0;
        String rs = null;
        if (input.length() > 0) {
            if (delimiters.indexOf(input.charAt(0))==-1) {
                delimit: for (n = 1; n < input.length(); n++) {
                    if (delimiters.indexOf(input.charAt(n))!=-1) break delimit;
                }
                rs = input.substring(0,n);
                input.delete(0,n);
            } else {
                rs = input.substring(0,1);
                input.deleteCharAt(0);
            }
        } else {
            throw new java.util.NoSuchElementException("No more tokens left.");
        }
        return rs;
    }
    
    /** Specify new set of delimiters and return next token.
     * @param spec Set of delimiters.
     * @return next token according to new specification.
     */
    public String nextToken(String spec) {
        delimiters = spec;
        return nextToken();
    }
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]