suggestion of type: ustring
ZY Zhou
rinick at GgMmAaIiLl.com
Sun Mar 20 05:34:54 PDT 2011
D's string is supposed to be utf8 encoded, however, the following code
compiles and runs with no error:
string s = "\xff"; // s is invalid
writeln(s);
fileStream.writeLine(s);
In order to make sure only valid utf8 string is used in the system, validating
is needed everywhere, e.g.
string cut3bytes(string s)
in {validate(s);}
out(result} {validate(result);}
body {return s.length > 3 ? s[0..3] : s;}
I think it will be better if D has a ustring type to do all the validating
job. e.g.
ustring s = "0xFF"; // compile error
char[] c = [0xFF];
ustring s = c.idup; // throw UtfException
ustring s1 = "\xc2\xa2";
ustring s2 = s1[0..1]; // throw UtfException
So the above example can be simplified to:
ustring cut3bytes(ustring s)
{return s.length > 3 ? s[0..3] : s;}
--ZY Zhou
More information about the Digitalmars-d
mailing list