About std.ascii.toLower

monarch_dodra monarchdodra at gmail.com
Fri Sep 21 02:00:31 PDT 2012


On Thursday, 20 September 2012 at 17:32:52 UTC, bearophile wrote:
> Jonathan M Davis:
>>Functions which operate on ASCII characters. All of the 
>>functions in std.ascii accept unicode characters but 
>>effectively ignore them. All isX functions return false for 
>>unicode characters, and all toX functions do nothing to unicode 
>>characters.<
>
> So now I'd like a new set of functions designed for ASCII text, 
> with contracts to refuse not-ASCII things ;-)
>
> Thank you for the answers Jonathan.
>
> Bye,
> bearophile

What do you (you two) think of my proposition for a 
"std.strictascii" module?

The signatures would be:
char toLower(dchar c);

And the implementations be like:

----
char toLower(dchar c)
in
{
     assert(c.std.ascii.isAscii());
}
body
{
     cast(char) c.std.ascii.toLower();
}
----

The rational for taking a dchar as input is so that it's own 
input can be correctly validated, and so that it can easilly 
operate with foreach etc, doing the cast internally. The returned 
value would be pre-cast to char.

Usage:

----
import std.stdio;
import std.strictascii;

void main(){
     string s1 = "axbacxf";
     string s2 = "àxbécxf";
     char[] cs = new char[](7);

     //bearophile use case: no casts
     foreach(i, c; s1)
         cs[i] = c.toUpper();

     //illegal use case: correct input validation
     foreach(i, c; s1)
         cs[i] = c.toUpper(); //in assert
}
----

It doesn't add *much* functionality, and arguably, it is a 
specialized functionality, but there are usecases where you want 
to operate ONLY on ascii, as pointed out by bearophile.

Just curious if I should even consider investing some effort in 
this.


More information about the Digitalmars-d-learn mailing list