[dmd-internals] Rare and pernicious bug in string append

Andrei Alexandrescu andrei at erdani.com
Tue Mar 16 09:19:12 PDT 2010


Pasted below.

/**
If $(D startsWith(r1, r2)), consume the corresponding elements off $(D
r1) and return $(D true). Otherwise, leave $(D r1) unchanged and
return $(D false).
  */
bool startsWithConsume(alias pred = "a == b", R1, R2)(ref R1 r1, R2 r2)
if (is(typeof(binaryFun!pred(r1.front, r2.front))))
{
     auto r = r1; // .save();
     while (!r2.empty && !r.empty && binaryFun!pred(r.front, r2.front))
     {
         r.popFront();
         r2.popFront();
     }
     return r2.empty ? (r1 = r, true) : false;
}

unittest
{
     auto s1 = "Hello world";
     assert(!startsWithConsume(s1, "Ha"));
     assert(s1 == "Hello world");
     assert(startsWithConsume(s1, "Hell") && s1 == "o world");
}


Andrei

On 03/16/2010 10:49 AM, Steve Schveighoffer wrote:
> You are using some new function "startsWithConsume" that isn't yet in phobos:
>
> untag.d(68): Error: undefined identifier startsWithConsume
> untag.d(68): Error: function expected before (), not startsWithConsume of type int
> untag.d(69): Error: undefined identifier startsWithConsume
> untag.d(69): Error: function expected before (), not startsWithConsume of type int
>
> -Steve
>
>
>
> ----- Original Message ----
>> From: Andrei Alexandrescu<andrei at erdani.com>
>> To: Steve Schveighoffer<schveiguy at yahoo.com>; Walter Bright<walter at digitalmars.com>; Sean Kelly<sean at invisibleduck.org>; Discuss the internals of DMD<dmd-internals at puremagic.com>
>> Sent: Tue, March 16, 2010 11:29:32 AM
>> Subject: Re: Rare and pernicious bug in string append
>>
>> Damn. Sorry.
>
> Andrei
>
>
>
>


More information about the dmd-internals mailing list