reddit.com: first Chapter of TDPL available for free
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Mon Aug 10 18:33:53 PDT 2009
Benji Smith wrote:
> 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
Great question. I plan to add a range ByCharacter and a function
byCharacter that spans a string one dchar at a time. Actually I have the
implementation already, haven't checked it in yet. That's a
bidirectional range. In that approach, if you write:
char[] haystack = "abcdefg";
auto pos = find(haystack, needle);
the search is done with the usual array semantics, but if you say:
char[] haystack = "abcdefg";
auto pos = find(byCharacter(haystack), needle);
then a dchar-at-a-time search is performed.
This idea is raw, so improvements are welcome.
Andrei
More information about the Digitalmars-d
mailing list