Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-1262447

java.lang.String.substring method is not compatible to specification

    XMLWordPrintable

Details

    • 1.1
    • sparc
    • solaris_2.5
    • Not verified

    Description



      Name: saf@russia Date: 08/07/96


        
      This bug was found by St.Petersburg Java SQE team (by Michael A. Gorshenev).


                            Java API Documentation
                                     1.0.2
                                   Par. 1.16

       " SUBSTRING

      public String substring(int beginIndex, int endIndex)

            Creates a new string that is a substring of this string. The substring begins at the specified
            beginIndex and extends to the character at index endIndex - 1.

            Parameters:
                  beginIndex - the beginning index, inclusive
                  endIndex - the ending index, exclusive
            Returns:
                  the specified substring.
            Throws
                  StringIndexOutOfBoundsException (I-§1.44)
                  If the beginIndex or the endIndex is out of range.
      "




      Bug in Java API 1.0.2

      java.lang.String.substring(int, int)

         method substring() must throw IndexOutOfBoundsException if beginIndex
         is larger than endIndex but it does not.
         
      =============== Here is the test fragment ===================
      String s1 = "java.lang.String.substring test";

      try {
        String s2 = s1.substring(16,10);
        System.out.println(s2);
      }
      catch (IndexOutOfBoundsException e) {
        System.out.println("Exception");
        return;
      }

      =============== Here is the test output =====================
      String

      ===== Here is the part of java.lang.String source code ======

          public String substring(int beginIndex, int endIndex) {
      if (beginIndex > endIndex) {
      int tmp = beginIndex;
      beginIndex = endIndex;
      endIndex = tmp;
      }
      if (beginIndex < 0) {
      throw new StringIndexOutOfBoundsException(beginIndex);
      }
      if (endIndex > count) {
      throw new StringIndexOutOfBoundsException(endIndex);
      }
      return ((beginIndex == 0) && (endIndex == count)) ? this :
      new String(offset + beginIndex, endIndex - beginIndex, value);
          }

      ========== Here is the part of language secification =========
      ===== (Pre-Release Version 1.0, Draft 5.2 - July 3, 1996) ====

      20.12.32 public String substring(int beginIndex, int endIndex)
      throws IndexOutOfBoundsException

      The result is a newly created String object that represents a subsequence
      of the character sequence represented by this String object; this
      subsequence begins with the character at position beginIndex and ends
      with the character at position endIndex-1. Thus, the length of the
      subsequence is endIndex-beginIndex.

      If beginIndex is negative, or endIndex is larger than the length of
      this String object, or beginIndex is larger than endIndex, then this
      method throws an IndexOutOfBoundsException.

      Examples:

      "hamburger".substring(4, 8) returns "urge"

      "smiles".substring(1, 5) returns "mile"

      ==============================================================

      Workaround:
      None
      ======================================================================

      Attachments

        Activity

          People

            jrose John Rose
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: