What is the legal range of chars?

monarch_dodra monarchdodra at gmail.com
Wed Jun 19 05:34:48 PDT 2013


I know a "binary" char can hold the values 0 to 0xFF. However, 
I'm wondering about the cases where a codepoint can fit inside a 
char. For example, 'ç' is represented by 0xe7, which technically 
fits inside a char.

This is illegal:
char c = 'ç';
But this works:
char c = cast(char)'ç';
assert(c == 'ç');

... it "works"... but is it legal?

--------
The root of the question though is actually this: If I have a 
string, and somebody asks me to find the character "char c" in 
that string. Is it legal to iterate on the string char by char, 
until I find c exactly, or do I have to take onto account that 
some troll may have decided to put a wchar inside my char...?

Basically:
string myFind(string s, char c)
{
     foreach(i, char sc ; s)
         if(sc == c)
             return s[i .. $];
     return s[$ .. $];
}
assert(myFind("aça", cast(char)'ç') == "ça");

The assert above will fail. But whose fault is it? Is it a wrong 
call, or a wrong implementation?


More information about the Digitalmars-d-learn mailing list