cmp and icmp.

Jonathan M Davis jmdavisProg at gmx.com
Thu Jul 4 16:10:00 PDT 2013


On Friday, July 05, 2013 00:36:02 Damian wrote:
> On Thursday, 4 July 2013 at 22:02:06 UTC, bearophile wrote:
> > Damian:
> >> Why don't these functions take a length parameter, like strcmp
> >> and memcmp?
> >> Is it worth making an enhancement request for such?
> > 
> > Probably slicing of their inputs, or using std.range.take() of
> > their inputs, is enough. Do you agree?
> > 
> > Bye,
> > bearophile
> 
> Ah but with slicing the inputs, you must check the length first
> so that you don't get an out of bounds error. I would prefer it
> if cmp
> did this for me, since it is going to check lengths anyway.

Except that cmp doesn't check the length. It just keeps popping elements until 
one of the two ranges is empty. Taking a length which it had to check would 
make it _less_ efficient, not more. The correct thing to do here is indeed to 
slice the range if it can be sliced or to use take if it can't be sliced. And 
if you want to slice the range and are worried about length, then just use min 
to make sure that you don't pass a length which is too large.

- Jonathan M Davis


More information about the Digitalmars-d mailing list