If invalid string should crash(was:string need to be robust)

Simen kjaeraas simen.kjaras at gmail.com
Mon Mar 14 14:09:53 PDT 2011


ZY Zhou <rinick at geeeemail.com> wrote:


> But for the following case, it is complete wrong if it crash at line 3:

Why? That is the point where you are actually saying 'I care about
individual characters in this string'.


>  1:  char[] c = [0xA0];
>  2:  string s = c.idup;
>  3:  foreach(dchar d; s){}
>
> The expected result is either:
>  a) crash at line 2, c is not valid utf
>     and can't be converted to string

A char[] is just as bound by the rules as is string (which is simply
immutable(char)[]). Thus the program should feel free to expect it
to contain valid utf-8 data. Validating each string upon every single
copy operation is unacceptable overhead.


> or:
>  b) don't crash, and d = 0xDCA0;

b is unacceptable in the general case. It may be good for your specific
situation, but in general, it is simply ignoring an error.

-- 
Simen


More information about the Digitalmars-d mailing list