String Prefix Predicate

monarch_dodra via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 18 05:42:24 PDT 2014


On Monday, 18 August 2014 at 11:28:25 UTC, Nordlöw wrote:
> On Saturday, 16 August 2014 at 20:59:47 UTC, monarch_dodra 
> wrote:
>> I don't get it? If you use "byDchar", you are *explicitly* 
>> decoding. How is that any better? If anything, you are 
>> *preventing* the (many) opportunities phobos has to *avoid* 
>> decoding when it can...
>
> byDchar and alikes are lazy ranges, ie they don't allocate.

Lazy does NOT mean does not allocate. You are making a terrible 
mistake if you assume that.

Furthermore decoding does NOT allocate either. At worst, it can 
throw an exception, but that's exceptional.

> They also don't throw exceptions which is preferably in some 
> cases.

Even then, "startsWith(string1, string2)" will *NOT* decode. It 
will do a binary comparison of the codeunits. A fast one at that, 
since you'll use SIMD vector comparison. Because of this, it 
won't throw any exceptions either. This compiles just fine:
void main() nothrow
{
     bool b = "foobar".startsWith("foo");
}


In contrast, with:
whole.byDchar().startsWith(part.byDchar())
You *will* decode. *THAT* will be painfully slow.

> Read the details at
> https://github.com/D-Programming-Language/phobos/pull/2043

If you are using a string, the only thing helpful in there is 
`byCodeunit`. The rest is only useful if you have actual ranges.

If you are using phobos, you should really trust the 
implementation that decoding will only happen on a "as needed" 
basis.


More information about the Digitalmars-d-learn mailing list