bringToFront() and arrays of char

Ali Çehreli acehreli at yahoo.com
Thu Dec 13 09:11:38 PST 2012


On 12/12/2012 07:22 PM, Jonathan M Davis wrote:
 > On Wednesday, December 12, 2012 17:34:53 Ali Çehreli wrote:
 >> (There must be an easier way of doing that. :))
 >
 > If you have a string that's really ASCII and you're _sure_ that it's only
 > ASCII, then I'd suggest simply casting it to immutable(ubyte)[] and 
operating
 > on that with all range based functions. That way, no decoding occurs, 
and you
 > don't have to dup the string. If you want, you could also easily create a
 > funtion called something like assumeASCII that did that cast in order 
to make
 > it more idiomatic (similar to std.exception.assumeUnique).
 >
 > - Jonathan M Davis

I like that. :)

Potentially, the function can check in debug compilation that a random 
selection of characters are really ASCII. Similar to what the 
constructor of std.range.SortedRange (used as assumeSorted) does:

 
https://github.com/D-Programming-Language/phobos/blob/master/std/range.d#L7015

struct SortedRange(Range, alias pred = "a < b")
if (isRandomAccessRange!Range)
{
// ...

     // Undocummented because a clearer way to invoke is by calling
     // assumeSorted.
     this(Range input)
     {
         this._input = input;
         if(!__ctfe)
         debug
         {
             import std.random;

             // Check the sortedness of the input
             if (this._input.length < 2) return;
             immutable size_t msb = bsr(this._input.length) + 1;
             assert(msb > 0 && msb <= this._input.length);
             immutable step = this._input.length / msb;
             static MinstdRand gen;
             immutable start = uniform(0, step, gen);
             auto st = stride(this._input, step);
             assert(isSorted!pred(st), text(st));
         }
     }

// ...
}

Ali



More information about the Digitalmars-d-learn mailing list