Proposal for SentinelInputRange

Marco Leise Marco.Leise at gmx.de
Wed Feb 27 20:13:35 PST 2013


Am Thu, 28 Feb 2013 05:01:39 +0100
schrieb "John Colvin" <john.loughran.colvin at gmail.com>:

> On Thursday, 28 February 2013 at 01:12:19 UTC, Walter Bright 
> wrote:
> > A SentinelInputRange is an InputRange with the following 
> > additions:
> >
> > 1. a compile time property 'sentinel' that is the terminating 
> > value of the range
> > 2. empty is defined as: empty = (front == sentinel)
> > 3. it is not necessary for empty to be called before front
> >
> > A C style 0-terminated string is an example of a 
> > SentinelInputRange.
> >
> > The additions to std.range would be:
> >
> > 1. isSentinelInputRange(T) which returns true if T is a 
> > SentinelInputRange
> > 2. a unittest
> > 3. documentation of this
> >
> > An addition to std.string would be a function that takes a 
> > char* and returns a SentinelInputRange.
> >
> > Motivation:
> > 1. easy conversion of C strings to ranges
> > 2. necessary for a fast implementation of a lexer
> >
> > Any takers?
> 
> Why must sentinel be known at compile time? I don't see what's in 
> the way of it being a runtime argument.

Possibly because a 0 check can be reduced to a simple bit
test on the CPU (and you need to do that often and possibly in
several source code locations when parsing long strings). A
runtime value means a memory read and comparison every time!
(For simple loops the compiler will keep the value in a
register, but that's not generally the case.)

-- 
Marco



More information about the Digitalmars-d mailing list