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

(str) String.contentEquals(CharSequence) still unoptimized

    XMLWordPrintable

    Details

    • Type: Enhancement
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 7
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • CPU:
      x86
    • OS:
      windows_xp

      Description

      FULL PRODUCT VERSION :
      jdk-7-ea-bin-b15-windows-i586-05_jul_2007

      A DESCRIPTION OF THE PROBLEM :
      See comment on the bug 6355654.
      In String.contentEquals(CharSquence), if the charsequence is not an AbstractStringBuilder, has the same length and has a different content, the contents are compared twice.

      public boolean contentEquals(CharSequence cs) {
              if (count != cs.length())
                  return false;
              // Argument is a StringBuffer, StringBuilder
              if (cs instanceof AbstractStringBuilder) {
                  char v1[] = value;
                  char v2[] = ((AbstractStringBuilder)cs).getValue();
                  int i = offset;
                  int j = 0;
                  int n = count;
                  while (n-- != 0) {
                      if (v1[i++] != v2[j++])
                          return false;
                  }
      return true;
              }
              // Argument is a String
              if (cs.equals(this))
                  return true;
      /*==================================================
      Here cs could a string with a different content so there would be no
      need to compare the chars again
      ==================================================*/
              // Argument is a generic CharSequence
              char v1[] = value;
              int i = offset;
              int j = 0;
              int n = count;
              while (n-- != 0) {
                  if (v1[i++] != cs.charAt(j++))
                      return false;
              }
              return true;
          }

      For instance if cs is a String "foo" and this = "bar", cs.equal(this) return false after comparing the two contents but the characters are compared again.
      Why don't you just keep it simple? You don't need instanceof, just compare the contents using charAt and it's gonna be fine. You can still add a contentEquals(AbstractStringBuilder) if you want. Plus a method stringEquals(String) would be appreciated because we compare a string to another string most of time and without needing compareTo specification.



      REPRODUCIBILITY :
      This bug can be reproduced always.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              ndcosta Nelson Dcosta (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Dates

                Created:
                Updated:
                Imported:
                Indexed: