commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Volker Glave (JIRA)" <>
Subject [jira] Created: (LANG-643) Javadoc StringUtils.left() claims to throw on negative len, but doesn't
Date Fri, 13 Aug 2010 14:54:17 GMT
Javadoc StringUtils.left() claims to throw on negative len, but doesn't

                 Key: LANG-643
             Project: Commons Lang
          Issue Type: Bug
          Components: lang.*
    Affects Versions: 2.5, 3.0
            Reporter: Volker Glave
            Priority: Minor

The Javadoc comment for StringUtils.left() claims to throw an exception "if len is negative"
(and that input argument len "must be zero or positive"):,%20int),%20int)

{quote}public static String left(String str, int len)

    Gets the leftmost len characters of a String.

    If len characters are not available, or the String is null, the String will be returned
without an exception. {color:red}An exception is thrown if len is negative.{color}

     StringUtils.left(null, *)    = null
     StringUtils.left(*, -ve)     = ""
     StringUtils.left("", *)      = ""
     StringUtils.left("abc", 0)   = ""
     StringUtils.left("abc", 2)   = "ab"
     StringUtils.left("abc", 4)   = "abc"

        str - the String to get the leftmost characters from, may be null
        len - the length of the required String, {color:red}must be zero or positive{color}
        the leftmost characters, null if null String input{quote}

But it  doesn't. (Luckily and preferably anyway :-).)
Instead an empty string will be returned. (Which is good.) As is seen from the implementation
code ...

        if (len < 0) {
            return EMPTY;

..., and by example, too:

{code}$ cat
import org.apache.commons.lang.StringUtils;

public final class StringUtilsTest {
    public static void main(final String[] args) {
        final String result = StringUtils.left("foobar", -42);
        System.out.println(">" + result + "<");

$ javac -classpath commons-lang.jar

$ java -classpath .:commons-lang.jar StringUtilsTest

The Javadoc comment should by updated.

Same issue for right() and mid(), by the way.

Volker Glave

This message is automatically generated by JIRA.
You can reply to this email to add a comment to the issue online.

View raw message