reddit.com: first Chapter of TDPL available for free

Benji Smith dlanguage at benjismith.net
Mon Aug 10 18:25:27 PDT 2009


Andrei Alexandrescu wrote:
> Daniel Keep wrote:
>>
>> Andrei Alexandrescu wrote:
>>> Michel Fortin wrote:
>>>> On 2009-08-09 11:10:48 -0400, Andrei Alexandrescu
>>>> <SeeWebsiteForEmail at erdani.org> said:
>>>>
>>>>>> It's also arguable that all functions in std.string should take
>>>>>> const(char)[]. Or, you know, const(T)[], since D supports encodings
>>>>>> other than UTF-8, despite what std.string leads you to believe.
>>>>> Yah, I think they should all be parameterized so they can work with
>>>>> various character widths and even encodings.
>>>> But shouldn't they work with *ranges* in general, a string being only
>>>> a specific case?
>>> That's true as well! In my dreams, me and the famous actress... oh wait,
>>> wrong dream. In my dreams, I eliminate std.string and put all of its
>>> algorithms, properly generalized, in std.algorithm, to work on more than
>>> just arrays, and more than just characters.
>>>
>>> Andrei
>>
>> How do you define 'tolower' on non-characters?
> 
> That and others would remain specific for characters. I do help to be 
> able to abstract functions such as e.g. strip().
> 
> Andrei

How would you generalize the string functions into ordinary array 
functions while still taking into account the different character types?

For example...

    dchar needle = 'f';
    char[] haystack = "abcdefg";
    auto index = haystack.indexOf(needle);

That code is roughly equivalent to this code for generalized arrays, 
which seems reasonable enough...

    float needle = 2.0;
    double[] haystack = [ 1.0, 2.0, 3.0 ];
    auto index = haystack.indexOf(needle);

...since "float" is implicitly castable to "double".

But the string example has weird monkey-business going on under the 
covers, since dchar is wider than char, and therefore a single dchar 
element might consume multiple slots within the char[] array.

Are there any analogous examples of that behavior with other types, 
where you'd search for a single element striding multiple indexes within 
an array of narrower values?

--benji



More information about the Digitalmars-d mailing list