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

(str) String.indexOf(String, int) is inconsistent with String.lastIndexOf(String, int)

    XMLWordPrintable

Details

    • Bug
    • Resolution: Not an Issue
    • P4
    • None
    • 6
    • core-libs
    • x86
    • windows_xp

    Description

      FULL PRODUCT VERSION :
      multiple, at least including JDKs 1.4 - 1.6

      java version "1.5.0_06"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]
      (irrelevant)

      A DESCRIPTION OF THE PROBLEM :
      String.indexOf(String, int) is not consistent with lastIndexOf(String, int) for null String and negative int.

      This was corrected for the versions of indexOf which take characters in J2SE 1.4 -- see "Spec update for String.indexOf/lastIndexOf" on http://java.sun.com/j2se/1.4/jcp/j2se-1_4-beta2-mr_docs-spec/
      but no change was made to the versions which take strings. So calling

      String.indexOf("", -1) returns 0
      String.lastIndexOf("", -1) returns -1

      The code at fault exists in JDK 1.5 and 1.6 (attached below).

      ALSO, although the JavaDoc for indexOf(char, int) and lastIndexOf(char, int) was updated in 1.4 to clarify the requirements and handling of fromIndex, the update was only to the methods that take char and not to those that take Strings, thus leaving those methods poorly documented.

      ---------

          static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,
                                 char[] target, int targetOffset, int targetCount,
                                 int fromIndex) {
              /*
      * Check arguments; return immediately where possible. For
      * consistency, don't check for null str.
      */
              int rightIndex = sourceCount - targetCount;
      if (fromIndex < 0) {
      return -1;
      }

      --------

          static int indexOf(char[] source, int sourceOffset, int sourceCount,
                             char[] target, int targetOffset, int targetCount,
                             int fromIndex) {
      if (fromIndex >= sourceCount) {
                  return (targetCount == 0 ? sourceCount : -1);
      }
           if (fromIndex < 0) {
           fromIndex = 0;
           }
      if (targetCount == 0) {
      return fromIndex;
      }


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      public class Test
      {
          public static void main(String args[])
          {
              System.out.println("Hello".indexOf("",-1) + "\t" + "Hello".lastIndexOf("",-1));
          }
      }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Consistency. -1 should be returned by both methods, thus indexOf should be corrected. The JavaDoc for both methods should be updated following the example of the versions of the same methods which take chars instead of Strings.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class Test
      {
          public static void main(String args[])
          {
              System.out.println("Hello".indexOf("",-1) + "\t" + "Hello".lastIndexOf("",-1));
          }
      }

      ---------- END SOURCE ----------

      Attachments

        Activity

          People

            jgish Jim Gish (Inactive)
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: