String Prefix Predicate

"Nordlöw" via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 18 13:50:54 PDT 2014


On Monday, 18 August 2014 at 12:42:25 UTC, monarch_dodra wrote:
> 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.

Ok, sorry about that. My mistake. And thanks for correcting me on 
this matter.

> 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");
> }

Ok, so decoding is needed only when whole and part have different 
encodings,

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

Ok.

>
>> 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.

Actual ranges of...characters and strings? Could you gives some 
examples? I'm curious.

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

Ok, got it.


More information about the Digitalmars-d-learn mailing list