Wouldn't this be nice?
    Joseph D. Darcy 
    Joe.Darcy at Sun.COM
       
    Thu Nov 12 15:50:24 PST 2009
    
    
  
Neal Gafter wrote:
> I wrote this back in
> 2004<http://www.denverjug.org/meetings/files/200408_Tiger.pdf>
> :
>
> *Iterable<Integer> codePoints(final String s) {
> *
> *return new Iterable<Integer>() {
> *
> *public Iterator<Integer> iterator() {
> *
> *return new Iterator<Integer>() {
> *
> *int nextIndex = 0;
> public boolean hasNext() {
> *
> *return nextIndex < s.length();
> *
> *}
> public Integer next() {
> *
> *int result = s.codePointAt(nextIndex);
> nextIndex += Character.charCount(result);
> return result;
> *
> *}
> public void remove()
> *
> *{ throw new UnsupportedOperationException(); }
> *
> *};
> *
> *}
> *
> *};
> *
> *}
> *
>
> Doing this for CharSequence is only a bit more complicated.
>
> If this is likely to be used frequently, direct compiler-generated would
> certainly be better.  Do we have a feel for how often this feature would be
> used?
>
>   
Yes, when I had to implement the functionality of iterating over the 
code points of a string 
(http://blogs.sun.com/darcy/entry/iterating_over_codepoints), I didn't 
find writing the canonical loop intuitive:
String s = ...
for(int cp, i = 0; i < s.length(); i += Character.charCount(cp)) {
    cp = s.codePointAt(i);
    // Process cp...
}
I would certainly be in favor of having a platform library method to do 
this, 5003547 "add support for iterating over the codepoints in a 
string" (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5003547).
-Joe
    
    
More information about the coin-dev
mailing list