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

String.offsetByCodePoints doesn't work for Strings returned by String.substring

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 6
    • 5.0
    • core-libs
    • b52
    • x86
    • linux

        FULL PRODUCT VERSION :
        java version "1.5.0_01"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_01-b08)
        Java HotSpot(TM) 64-Bit Server VM (build 1.5.0_01-b08, mixed mode)

        ADDITIONAL OS VERSION INFORMATION :
        Linux hylas 2.6.9-gentoo-r14 #1 Mon Jan 31 13:57:09 EST 2005 x86_64 AMD Athlon(tm) 64 Processor 3200+ AuthenticAMD GNU/Linux


        EXTRA RELEVANT SYSTEM CONFIGURATION :
        Tested in Netbeans 4.0

        A DESCRIPTION OF THE PROBLEM :
        If you get a String back from String.substring(), and try to run .offsetByCodePoints(0,1) on it, it will return a code point index that appears to be relative to the source string (the one you called .subtring() on.)

        This is incorrect, since the specification of String.substring() says that it returns a new String().


        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Run the sample code included.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        Since I'm running with a basic US locale, I expect the sample code to print

        i=1 j=1 k=1

        Certainly, there should be no difference between the values printed for j and k.

        ACTUAL -
        The sample code prints:

        i=1 j=4 k=1



        ERROR MESSAGES/STACK TRACES THAT OCCUR :
        No error codes or exceptions are generated.

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        package foo;

        public class BugTestClass {
            
            public static void main(String args[]) {

                String myString = "abcdef";
                int i = myString.offsetByCodePoints(0,1);
                String sub = myString.substring(3);
                int j = sub.offsetByCodePoints(0,1);
                int k = new String(sub).offsetByCodePoints(0,1);
                System.out.println("i=" + i + " j=" + j + " k=" + k);
            }
        }

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

        CUSTOMER SUBMITTED WORKAROUND :
        The workaround is to create a new String as in the example for k above, or like so:

        String source = "abcdef";
        String sub = new String( source.substring(3) );

        Now sub.offsetByCodePoints will work as expected.
        ###@###.### 2005-03-18 09:18:16 GMT

              peytoia Yuka Kamiya (Inactive)
              jssunw Jitender S (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: