commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Janek Bogucki <jane...@yahoo.co.uk>
Subject StringUtils.isLine throws ArrayIndexOutOfBoundsException and fails to check index sz - 2
Date Sun, 16 Dec 2001 01:34:39 GMT
There are a couple of bugs in
org.apache.commons.util.StringUtils.isLine. (If there
is a more suitable way for me to go about reporting
stuff about the sandbox please educate me - thanks,
janek)

1.
--
org.apache.commons.util.StringUtils.isLine ( String
str ) throws a ArrayIndexOutOfBoundsException in this
case:

    String testString = "abc-\r" ;
    if ( StringUtils.isLine ( testString ) )
        System.out.println("It was a line");
    else
        System.out.println("It was not a line");


Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException
        at
org.apache.commons.util.StringUtils.isLine(StringUtils.java:1574)
        at
brandram.debug.TestPad.main(TestPad.java:47)

2.
--

The hyphen in "abc-x" is never examined in
StringUtils.isLine. In fact the character at index sz
- 2 is always skipped.

Current version of the method from StringUtils 1.18
---------------------------------------------------

    /**
     * Is a String a line, containing only letters,
digits or 
     * whitespace, and ending with an optional
newline.
     * NB: Punctuation not allowed.
     */
    static public boolean isLine(String str) {
        char ch = 0;
        char[] chrs = str.toCharArray();
        int sz = chrs.length;
        for(int i=0; i<sz-2; i++) {
            if(!Character.isLetterOrDigit(chrs[i])) {
                if(!Character.isWhitespace(chrs[i])) {
                    return false;
                }
            }
        }
        if(!Character.isLetterOrDigit(chrs[sz-1])) {
            if(!Character.isWhitespace(chrs[sz-1])) {
                if(chrs[sz-1] != '\r') {
                    return false;
                } else 
                if(chrs[sz] != '\n') {
                    return false;
                }
            }
        }
        if(!Character.isLetterOrDigit(chrs[sz])) {
            if(!Character.isWhitespace(chrs[sz])) {
                if(chrs[sz] != '\n') {
                    return false;
                }
            }
        }
        return true;
    }



__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message