[fpc-devel] for-in-index loop

Mattias Gaertner nc-gaertnma at netcologne.de
Fri Jan 25 10:56:02 CET 2013


Mattias Gaertner <nc-gaertnma at netcologne.de> hat am 25. Januar 2013 um 10:52
geschrieben:
>
> Michael Schnell <mschnell at lumino.de> hat am 25. Januar 2013 um 10:35
> geschrieben:
>
> > On 01/23/2013 12:54 AM, vrt277 wrote:
> >
> > > > Hi FPC team,
> > >
> > > There is good proposed extension of for-in loop on fpc wiki: get
> > > enumerator Position if available
> > > <http://wiki.freepascal.org/for-in_loop#Proposed_extensions> . From my
> > > point
> > > of view it's essential part of iterators. E specially for data structures
> > > which store pairs of key and value associated with key.
>
> The above UTF8 example misses some points. Often you need the index and often
> you need the byte position.
> This can be remedied with an enumerator using a record:
>
> type
>   TUTF8Enummy = record
>     c: UTF8Char;
>     ByteIndex: SizeInt;
>     UTF8Index: SizeInt;
>   end;
>
>   TUTF8StringEnumerator = class
>     ...
>   public
>     constructor Create(const A: UTF8String);
>     function Current: TUTF8Enummy;
>     function MoveNext: Boolean;
>   end;
>   ...
>
> var
> s: UTF8String;
ch: TUTF8Enummy;
> begin
>   for ch in s do
>     Writeln(ch.UTF8Index, ': ', ch.c,' at byte ',ch.ByteIndex);
> end.
>
> The compiler already supports such constructs.
> It seems this is even more powerful and useful than the proposed 'index'
> extension.


Mattias



More information about the fpc-devel mailing list