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

(str) add support for iterating over the codepoints in a string

    XMLWordPrintable

Details

    • Enhancement
    • Resolution: Duplicate
    • P4
    • None
    • 5.0
    • core-libs
    • generic
    • generic

    Description

      [adapted from Josh and Neal's "cooking with Tiger" talk]

      Placing the enclosed adapter method in java.lang.String enables clients to
      iterate through the code points in a String very easily:

          void f(String s) {
      for (int ch : s.codePoints())
      ;// do something with the codepoint ch here.
          }

      Here is the proposed addition to java.lang.String:

      public Iterable<Integer> codePoints() {
          return new Iterable<Integer>() {
              public Iterator<Integer> iterator() {
                  return new Iterator<Integer>() {
                      int nextIndex = 0;
                      public boolean hasNext() {
                          return nextIndex < length();
                      }
                      public Integer next() {
                          int result = codePointAt(nextIndex);
                          nextIndex += Character.charCount(result);
                          return result;
                      }
                      public void remove() {
                          throw new UnsupportedOperationException();
                      }
                  };
              }
          };
      }

      Attachments

        Issue Links

          Activity

            People

              darcy Joe Darcy
              gafter Neal Gafter
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: