About std.ascii.toLower
monarch_dodra
monarchdodra at gmail.com
Fri Sep 21 04:18:01 PDT 2012
On Friday, 21 September 2012 at 10:45:42 UTC, Jonathan M Davis
wrote:
> On Friday, September 21, 2012 12:38:07 monarch_dodra wrote:
>> On Friday, 21 September 2012 at 10:23:39 UTC, Jonathan M Davis
>>
>> wrote:
>> > On Friday, September 21, 2012 11:00:31 monarch_dodra wrote:
>> >> What do you (you two) think of my proposition for a
>> >> "std.strictascii" module?
>> >
>> > I don't think that it's at all worth it. It's just duplicate
>> > functionality in
>> > order to avoid a cast.
>>
>> (and contract)
>
> If that's what you want, it's easy enough to create a helper
> function which
> you use instead of a cast which does the contract check as
> well. e.g.
>
> char toChar(dchar c)
> {
> assert(isAscii(c));
> return cast(char)c;
> }
>
> foreach(ref char c; str)
> c = toChar(std.ascii.toLower(c));
>
> It should be completely optimized out with -release and -inline.
>
> - Jonathan M Davis
That's a real good idea. Also, I find it is these kinds of
situations where UFCS really shines (IMO):
foreach(i, c; s1)
cs[i] = c.toUpper().toChar();
I love this syntax.
Related, could "toChar" be considered for inclusion? I think it
would be a convenient tool for validation.
/*
* Casts dchar to a char.
*
* Preconditions:
* $(D c) must be representable in a single char.
*/
char toChar(dchar c)
{
assert(c < 256, "toChar: Input too large for char");
return cast(char)c;
}
That said, if we go that way, we might as well just have a more
generic safeCast in std.conv or something:
T safeCast(T, U)(U i)
if(isBasicType!T && isBasicType!U)
{
assert(cast(T)i == i, "safeCast: Cast failed");
return cast(T)i;
}
foreach(i, c; s1)
cs[i] = c.toUpper().safeCast!char();
Hum... yeah... I don't know...
I seem to be typing faster than I can really think of the
consequences of such a function.
More information about the Digitalmars-d-learn
mailing list