isAsciiString in Phobos?

Andrej Mitrovic andrej.mitrovich at gmail.com
Mon Oct 7 08:57:07 PDT 2013


On 10/7/13, Adam D. Ruppe <destructionator at gmail.com> wrote:
> If you want strict ASCII, it should be <= 127 rather than 255
> because the high bit can be all kinds of different encodings (the
> first 255 of unicode codepoints I think match latin-1
> numerically, but that's different than windows-1252 or various
> non-English extended asciis.)
>
> You could also convert utf-8 to ascii.... sort of... by just
> stripping out any byte > 127 since bytes higher than that are
> multibyte sequences in utf8.

Thanks. I got some useful info from Jakob from IRC, and ended up with this:

bool isAsciiString(string input)
{
    auto data = cast(const(ubyte)[])input;
    return data.all!(a => a <= 0x7F);
}

The cast is needed to avoid decoding by the "all" function. Also
there's isASCII that works on a dchar in std.ascii, but I was looking
for something that works on entire strings at once. So the above
function does the work for me.

Should we put something like this in Phobos?


More information about the Digitalmars-d-learn mailing list