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

Implementation of BreakIterator.preceding(int) is not correct.

XMLWordPrintable

    • 5.0
    • 5.0
    • rc
    • generic
    • generic

      Name: nl37777 Date: 09/29/98


      Version^[$B!'^[(J1.2fcs_K

      In the following sample, an IllegalArgumentException is thrown.
      So, it is not possible to look for the previous boundary from last position of the text.

          import java.text.BreakIterator;
          
          public class PrecedingTest {
              public static void main(String args[]) {
                  BreakIterator boundary = BreakIterator.getCharacterInstance();
                  boundary.setText("abc");
                  int last = boundary.last();
                  System.out.println("last " + last);
                  int beforeLast = boundary.preceding(last);
                  // Exception java.lang.IllegalArgumentException:
                  // preceding() offset out of bounds
                  System.out.println("beforeLast " + beforeLast);
              }
          }


      The related source files.

      src/share/classes/java/text/SimpleTextBoundary.java
      src/share/classes/java/text/BreakIterator.java



      In SimpleTextBoundary, the range of check of the parameter is same in following(int) and preceding(int).
      But, it is not correct.

        <Current parameter check>
          following(int offset):
              offset < text.getBeginIndex() || offset >= text.getEndIndex()
          preceding(int offset):
              offset < text.getBeginIndex() || offset >= text.getEndIndex()

        <The sample of revision>
          following(int offset):
              offset < text.getBeginIndex() || offset >= text.getEndIndex()
          preceding(int offset):
              offset <= text.getBeginIndex() || offset > text.getEndIndex()



      In addition, the implementation of BreakIterator.preceding(int offset) is not correct.

        <Current BreakIterator's source>
          public int preceding(int offset) {
              int pos = following(offset); // <- here
              while (pos >= offset && pos != DONE)
                  pos = previous();
              return pos;
          }

        <The sample of revision>
              int pos = following(offset - 1);
      (Review ID: 39425)
      ======================================================================
      ###@###.### 11/2/04 18:38 GMT

            xlu Xiaobin Lu (Inactive)
            nlindenbsunw Norbert Lindenberg (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: