What features of D are you using now which you thought you'd never goint to use?

Brad Anderson eco at gnuk.net
Sat Jun 22 19:13:20 PDT 2013


On Sunday, 23 June 2013 at 01:34:53 UTC, Andrei Alexandrescu 
wrote:
> On 6/22/13 2:58 PM, monarch_dodra wrote:
>> long story short: we don't have rfind. C++ does.
>
> We do, just that it's for random-access ranges. C++ offers it 
> for bidirectional ranges too. We could of course support it if 
> bidirectional ranges were required to have something like 
> r1.before(r2) that, assuming r2 is reachable from r1, returns 
> the portion in r1 that's before r2.
>
> Andrei

That sounds like exactly what I needed for a version of toUpper I 
was playing with that added support for output ranges.

The implementation is rather trivial:

Writer toUpper(S, Writer)(S s, Writer w) @trusted
     if(isSomeString!S)
{
     size_t count = 0;
     foreach (c, i; s)
     {
         if (std.uni.isLower(c))
         {
             c = std.uni.toUpper(c);
         }
         put(w, c);
         count = i;
     }

     return w;
}

Works just fine with something like Appender. The problem is that 
if you pass in dynamic or static array slice to store the result 
in you have no way of knowing how much of the target array was 
filled by the function.  You could use save() and keep track of 
how many bytes were consumed and return a slice of that size but 
that feels very clunky (and could be tricky to implement 
correctly if the input string is a different encoding than the 
output range).


More information about the Digitalmars-d mailing list